17
votes

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.

7

7 Answers

34
votes

Login function needs user of type Authenticatable and you just given email which is string thats why you get this error, Either use Auth::loginUsingId($id);

 $user = User::where('email','=',$email)->first();
 Auth::loginUsingId($user->id, TRUE);

Or just

Auth::login($user);
4
votes

Instead of this

Auth::login($userMail->email, TRUE);

Use this

Auth::login($user->id, TRUE);

3
votes
$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

2
votes

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

1
votes

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

0
votes

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);
0
votes

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.