Laravel 访客中间件和用户身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47429123/
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:59:20  来源:igfitidea点击:

Laravel Guest Middleware and user authentication

laravel

提问by BJ Langruto

I just created a simple login with guest middleware that allows the user to access one account at a time, but I am just worried if this is the right way to do it.

我刚刚使用访客中间件创建了一个简单的登录,允许用户一次访问一个帐户,但我只是担心这是否是正确的方法。

/** Routes **/        
Route::group(['middleware' => 'guest'], function () {

        Route::get('/', 'LoginController@index')->name('login');
        Route::post('/', 'LoginController@post')->name('login.post');

    });



/** login.post controller **/
    public function post(Request $request){
            $this->rules($request);

            $rules = array(
                'username' => $request->username,
                'password' => $request->password,
            );

            if(Auth::attempt($rules)) {
                if(Auth::user()->is_active == true){
                /** IF THE USER IS CURRECTLY LOGIN **/
                 if(Auth::user()->is_login == true){
                   Auth::logout();
                   Session::flash('multilog', 'Your account is log-in to another device!!');
                   return redirect('/')->withInput();
                 }
                $user = user::find(Auth::user()->id); 
                $user->is_login = true;
                $user->save();
                return redirect('admin/home');
                }
                Session::flash('unactivated', 'Your account is not activated!!');
                return redirect('/')->withInput();
            }
            Session::flash('unmatch', 'Invalid username or password!!');  
            return redirect('/')->withInput();
        }

/** **/

/** **/

回答by Adam Kozlowski

If you are not sure, you can use Laravel to create authentication. Write in command line:

如果不确定,可以使用 Laravel 来创建身份验证。在命令行中写入:

php artisan make:auth

Then just look how the logic works in files. More you can read here: https://laravel.com/docs/5.5/authentication

然后看看逻辑在文件中是如何工作的。您可以在此处阅读更多内容:https: //laravel.com/docs/5.5/authentication