Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel manual login function

Tags:

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.

like image 702
Stack User Avatar asked Sep 22 '17 10:09

Stack User


2 Answers

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);
like image 192
Niklesh Raut Avatar answered Sep 30 '22 07:09

Niklesh Raut


Instead of this

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

Use this

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

like image 38
Siva Ganesh Avatar answered Sep 30 '22 07:09

Siva Ganesh