Laravel manual login function

谁说我不能喝 提交于 2019-12-19 02:51:53

问题


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.


回答1:


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



回答2:


Instead of this

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

Use this

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




回答3:


$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




回答4:


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




回答5:


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




回答6:


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


来源:https://stackoverflow.com/questions/46362505/laravel-manual-login-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!