Getting this error continuously: MethodNotAllowedHttpException in RouteCollection.php line 218: - laravel

When I tried to log in, on my website. It responded with the message:
FatalErrorException in UserController.php line 37:
Class 'App\Http\Controllers\Auth' not found
But, the file there is controllers folder in Http containing Auth where there are four files.
UserController:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
//use Illuminate\Support\Facades\Flash;
use InvalidConfirmationCodeException;
use Flash;
//use Mail;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class UserController extends Controller
{
public function getDashboard()
{
return view('dashboard');
}
public function postSignUp(Request $request)
{
$email = $request['email'];
$name = $request['name'];
$password = bcrypt($request['password']);
$user = new User();
$user -> email = $email;
$user -> name = $name;
$user -> password = $password;
Auth::login('$user');
$user->save();
return redirect()->route('dashboard')
}
public function postSignIn(Request $request)
{
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
}
also, I am getting an error while signup:
MethodNotAllowedHttpException in RouteCollection.php line 218:
And I am trying to solve this error from a very long time but no success till now.
Please help me with both the errors.

change Auth::login($user) --> \Auth::login($user)
and let me know if you are getting anything in database after signup . :)

To get rid off
FatalErrorException in UserController.php line 37: Class
'App\Http\Controllers\Auth' not found
Add use Auth at the top
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Auth;

Change Auth::login($User) to \Auth::login($user), your $user variable is incorrect as well.
Check your signup route, is it doing a POST (most likely it is) and you're sending a GET request?

You need add
use Auth;
to your Controller.

I highly doubt that the Auth class is inside your Controllers directory. The Auth you are looking for, is probably the Auth facade, can be included this way:
use Illuminate\Support\Facades\Auth;

Related

If logged with jwt-auth looks in controller Auth::user() is empty

In my Laravel 5.8/vuejs 2.6 app I use
"tymon/jwt-auth": "^1.0.0",
and my app/Http/Controllers/AuthController.php has method:
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if ($token = $this->guard('api')->attempt($credentials)) {
return $this->respondWithToken($token);
}
return response()->json(['error' => 'Unauthorized'], 401);
}
and I keep token on client side. It works but I want to add more checks on the server's side, when I save data and to make in control's method :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use App\Settings;
use App\Http\Traits\funcsTrait;
use App\Forum;
use App\ForumCategory;
use App\ForumThread;
use App\ForumPost;
use Illuminate\Routing\Controller as BaseController;
use App\User;
use App\library\CheckValueType;
use App\Http\Requests\ForumThreadRequest;
use JavaScript;
class ForumController extends BaseController
{
use funcsTrait;
public function __construct()
{
$this->middleware('auth');
}
public function add_new_thread(ForumThreadRequest $request)
{
$loggedUser = Auth::user();
if ( empty($loggedUser->id) ) {
return response()->json(['error_code'=> 1, 'message'=> "You must be logged!", 'forumThreadRow'=>null],HTTP_RESPONSE_INTERNAL_SERVER_ERROR);
}
try {
But even if I have logged into the system it looks like that in add_new_thread method $loggedUser is empty.
Have I to make some additive actions in login method of AuthController.php or in which way ?
As I use api guard decision is to use :
$user = Auth::guard('api')->user();
This is a late answer, but maybe could help someone.
I had the same issue and it was fixed by adding $table property to the user model User.php
/**
* Specify table name otherwise Auth::user() will return null
*
* #var string
*/
protected $table = 'users';
see here

Laravel test Passport::actingAs($user) use routes?

I have custom passport user login validation (i made it following this) so i make my custom /oauth/token with this route:
/routes/auth.php
Route::post('/oauth/token', [
'uses' => 'Auth\CustomAccessTokenController#issueUserToken'
]);
/app/controllers/auth/CustomAccessTokenController.php
namespace App\Http\Controllers\Auth;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Psr\Http\Message\ServerRequestInterface;
class CustomAccessTokenController extends Controller
{
public function issueUserToken(ServerRequestInterface $request)
{
$httpRequest = request();
if ($httpRequest->grant_type == 'password') {
$user = User::where('email', $httpRequest->username)->first();
return $this->issueToken($request);
}
}
}
If i make a manual POST request to domain.com/oauth/token is correctly handled by the custom controller but when i use Passport::actingAs($user); in a phpunit test not. This Passport::actingAs(); use the routes or have other way to get the authentication token?
You should be able to get the authentication token using
$this->actingAs($user, 'api');

Laravel - Attempting auth

I'm trying to manually auth my user like this :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
use Log;
use Hash;
class UsersController extends Controller
{
public function authenticate(Request $request){
$email = $request->input('email');
$password = $request->input('password');
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return response()->json(User::find($email));
}
return response()->json(null);
}
}
I verify my request data, my database data, all is ok. I've also done a Hash::check with my user password and my request password, all is ok.
But the attempt always returning false.
Thanks !
Finally, I found it.
This was because I was attending a result but
return response()->json(User::find($email));
didn't find a user. So I change it for the following
return response()->json(User::where('email', $email)->first());
Thanks !

ErrorException in registerController.php line 18: Undefined variable: name

I made a signup form with laravel 5 and im getting this error
ErrorException in registerController.php line 18:
Undefined variable: name
<?php
namespace App\Http\Controllers;
use Hash;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class registerController extends Controller
{
public function register()
{
$request = new Request;
$user = new User;
$user->$name = $request->input('nameInputSignup');
$user->$email = $request->input('emailInputSignup');
$user->$password = HASH::make($request->input('passwordInputSignup'));
$user->save();
return view('index');
}
}
Route::post('/register' , 'registerController#register');
here is my code, can anyone help with this error.
Drop the extra $ from the members of your 'user' object:
$user->name
$user->email
$user->password

Laravel 5.1 social Authentication is not working properly

Recently i am working on laravel 5.1 framework and trying to make user login by Social Authentication like facebook login ..
i followed this tutorial from youtube Laravel 5.1 Socialite Authentication
but get this Exception
InvalidStateException in AbstractProvider.php line 191:
in AbstractProvider.php line 191
at AbstractProvider->user() in SocialAuthController.php line 61
at SocialAuthController->github_Callback()
at call_user_func_array(array(object(SocialAuthController), 'github_Callback'), array()) in Controller.php line 256
at Controller->callAction('github_Callback', array()) in ControllerDispatcher.php line 164
Here is my code
// facebook Sociolite for routing
Route::get('/auth/facebook', 'Auth\SocialAuthController#redirectToProvider');
Route::get('callback_facebook', 'Auth\SocialAuthController#handleProviderCallback');
// AuthController
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Socialite;
use Auth;
use redirect;
class AuthController extends Controller
{
class SocialAuthController extends Controller{
// For Facebook
public function redirectToProvider(){
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback(){
$curl = curl_init();
curl_setopt($curl , CURLOPT_SSL_VERIFYPEER, false);
$user = Socialite::driver('facebook')->user();
$data = ['name'=>$user->name, 'email'=>$user->email, 'password'=>$user->token];
$userDB = User::where('email',$user->email)->first();
if (!is_null($userDB)){
Auth::login($userDB);
}
else{
Auth::login($this->create($data));
}
return redirect('/pages/profile');
}
}
}
config\service.php
'facebook' => [
'client_id' => '513845902100524',
'client_secret' => '135699237e16cd3a50d2cbfec3a5e58c',
'redirect' => 'http://localhost:8000/callback_facebook',
],
You can get detail documentation from laravel official site. Socialite tutorial here.

Resources