'Laravel manual login function
I use manual login function in Laravel 5.5. Stuck in login. and check all(5 relevant ) Stack links and didn't find any clue on it.
Achievement is once user get registered, automatically sign in that user.
Error is
"Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, string given, called in Server/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 294 ◀"
if ($validator->fails()) {
// $messages = $validator->messages();
return Redirect::to('register')
->withErrors($validator)
->withInput();
} else {
$email = Input::get('email');
$user = new user;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
// $userMail = $user->find($email);
$userMail = User::where('email','=',$email)->first();
Auth::login($userMail->email, TRUE);
am i doing anything wrong. Please guide me.
Solution 1:[1]
Instead of this
Auth::login($userMail->email, TRUE);
Use this
Auth::login($user->id, TRUE);
Solution 2:[2]
$email = $request->email;
$password = md5($request->password);
if ($request->remember_me == 1) {
$cookie = Cookie::queue('username', $email, time() + 31536000);
} else {
$cookie = Cookie::queue('username', '', time() - 100);
}
$user = DB::table('tbl_adminuser')->where('email_address', $email)->where('password', $password)->first();
$request->session()->put('userData', $user);
=> You can manual login like this in laravel
Solution 3:[3]
The Auth::login() function expects an Authenticable object. If you have not messed with the User class, this will be what you need to pass in.
Auth::login($user, true);
Reference: https://laravel.com/api/5.5/Illuminate/Auth/SessionGuard.html#method_login
Solution 4:[4]
Just use Auth::login($userMail, TRUE); instead of Auth::login($userMail->email, TRUE);
For more info check: https://laravel.com/api/5.5/Illuminate/Auth/SessionGuard.html#method_login
Solution 5:[5]
You need to pass/return the $user like this
if ($validator->fails()) {
// $messages = $validator->messages();
return Redirect::to('register')
->withErrors($validator)
->withInput();
} else {
$email = Input::get('email');
$user = new user;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
//
Auth::login($user);
OR
Auth::loginUsingId($user->id, TRUE);
Solution 6:[6]
For Laravel 7.
Manual Login Use this method for login.
public function check_login(Request $request)
{
$credentials = $request->only('username', 'password');
if (Auth::attempt($credentials)) {
return redirect('/dashboard');
} else {
return redirect('/login')->with('error', 'Invalid Email address or Password');
}
}
If you are using a different model class other than the default('User'), then -> navigate to config/auth.php and edit this
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
to
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Staff::class,
],
Note: I have replaced User with my model class Staff.
Finally go to your model class and instead of extending Model, extend User. ie
From
class Staff extends Model
{
to
class Staff extends \Illuminate\Foundation\Auth\User
{
That's it.
Solution 7:[7]
According to the doc, you may need to regenerate the session after manually login an user:
$user = User::firstOrCreate([
// ...
], [
// ...
]);
Auth::login($user);
$request->session()->regenerate();
return redirect()->route('dashboard');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | parthu gajera |
| Solution 3 | mbozwood |
| Solution 4 | Jie |
| Solution 5 | |
| Solution 6 | Martin Mbae |
| Solution 7 | ohho |
