phplaravelauthenticationlaravel-5laravel-5.5

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

  • 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);