Laravel 手动登录功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46362505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:41:47  来源:igfitidea点击:

Laravel manual login function

phplaravelauthenticationlaravel-5laravel-5.5

提问by Stack User

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.

我在 Laravel 5.5 中使用手动登录功能。卡在登录中。并检查所有(5 个相关)堆栈链接,但没有找到任何线索。

Achievement isonce 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 ?"

“类型错误:传递给 Illuminate\Auth\SessionGuard::login() 的参数 1 必须实现接口 Illuminate\Contracts\Auth\Authenticatable,给出的字符串,在 Server/vendor/laravel/framework/src/Illuminate/Auth/AuthManager 中调用。 php 在第 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.

我做错了什么吗?请指导我。

回答by Niklesh Raut

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

登录功能需要类型的用户,Authenticatable而您刚刚给出的email是字符串,这就是您收到此错误的原因,要么使用Auth::loginUsingId($id);

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

Or just

要不就

Auth::login($user);

回答by parthu gajera

$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

=>您可以在laravel中像这样手动登录

回答by Siva Ganesh

Instead of this

而不是这个

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

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

Use this

用这个

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

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

回答by Jonjie

Just use Auth::login($userMail, TRUE);instead of Auth::login($userMail->email, TRUE);

只需使用Auth::login($userMail, TRUE);代替Auth::login($userMail->email, TRUE);

For more info check: https://laravel.com/api/5.5/Illuminate/Auth/SessionGuard.html#method_login

更多信息请查看:https: //laravel.com/api/5.5/Illuminate/Auth/SessionGuard.html#method_login

回答by mbozwood

The Auth::login()function expects an Authenticable object. If you have not messed with the Userclass, this will be what you need to pass in.

Auth::login()函数需要一个 Authenticable 对象。如果你没有搞乱这个User类,这将是你需要传入的。

Auth::login($user, true);

Auth::login($user, true);

Reference: https://laravel.com/api/5.5/Illuminate/Auth/SessionGuard.html#method_login

参考:https: //laravel.com/api/5.5/Illuminate/Auth/SessionGuard.html#method_login

回答by Mr. Pyramid

You need to pass/return the $userlike this

你需要$user像这样传递/返回

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