简体   繁体   English

如何使用Auth管理laravel中的登录/注销

[英]How to manage Login/Logout in laravel using Auth

I have create one login module in laravel,what i have done yet is i have manually authenticated the user successfully and redirect on dashboard page but my issue is when user logged out from the application and again if they try to open that dashboard url then it is showing error MethodNotAllowedHttpException in RouteCollection at this time what i want is if user is not authenticated then it will directly redirect on our login page.I have also tried to put some logic in my LoginController Constructor but it is also not working.Below is my code with file path. 我已经在laravel中创建了一个登录模块,但我已经完成了手动验证用户身份并在仪表板页面上重定向的问题,但是我的问题是当用户从应用程序注销时,如果他们再次尝试打开该仪表板URL,那么它会这时在RouteCollection中显示错误MethodNotAllowedHttpException,我想要的是如果用户未通过身份验证,那么它将直接重定向到我们的登录页面上。我也试图在我的LoginController构造函数中放入一些逻辑,但它也不起作用。以下是我的文件路径的代码。

laravelproject\\app\\Http\\routes.php laravelproject \\ app \\ Http \\ routes.php

Route::auth();
Route::post('/login-submit', 'LoginController@loginSubmit');
Route::get('/log-out',[
    'uses'=>'LoginController@logOut',
    ]);

laravelproject\\app\\Http\\Controllers\\LoginController.php laravelproject \\ app \\ Http \\ Controllers \\ LoginController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;

//to take input from user
use App\Http\Requests;
use Illuminate\Http\Request;
//end

class LoginController extends Controller
{


    public function loginSubmit(Request $request)
    {

         $email=$request->email;
         $password=$request->password;

         //var_dump($credentials);die;
        if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
            // Authentication passed...

             return view('dashboard');
        }
        else
        {
             return view('auth/login');
        }    
    }

    public function logOut() {


        Auth::logout();

        return view('auth/login');

    }
}

Error after getting logout and trying to accessing dashboard url MethodNotAllowedHttpException in RouteCollection 注销并尝试访问RouteCollection中的仪表板URL MethodNotAllowedHttpException后出现错误

You need to utilize middleware here. 您需要在这里使用中间件。 Create a middleware in App\\Http\\middleware 在App \\ Http \\ middleware中创建一个中间件

class administrator
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ( Auth::check() && Auth::user()->isAdmin() )
        {
            return $next($request);
        }

        return redirect('signin');
    }
}

And now in the web.php you assign this middleware to the dashboard route so that dashboard is only accessible by adminisitrator 现在,在web.php中,您可以将此中间件分配给仪表板路由,以便仅管理员可以访问仪表板

Route::get('/', 'Dashboard@dashboard')->middleware(['administrator']);

And for Auth::user()->isAdmin() define a boolean field in users table as 'admin' then define method isAdmin in User model like this: 对于Auth :: user()-> isAdmin(),在users表中将布尔字段定义为'admin',然后在User模型中定义isAdmin方法,如下所示:

public function isAdmin()
{
    return $this->admin;
}

now the process will be stream lined. 现在,该过程将被简化。 whichever route you assign middleware administrator it will authenticate that route for admin. 无论您分配了哪个中间件管理员路由,都将为管理员验证该路由。 on authentication failure it will send the user to login page 验证失败时,它将把用户发送到登录页面

maintain a session for your user, and in loginsubmit() function first check if use session exist then return to dashboard view. 维护您的用户的会话,并在loginsubmit()函数中首先检查是否存在使用会话,然后返回到仪表板视图。 in logout action flush the user session as 在注销操作中,将用户会话刷新为

public function loginSubmit(Request $request)
{
     if (Session::has('user')) {
        return view('dashboard');
     }
     $email=$request->email;
     $password=$request->password;

     //var_dump($credentials);die;
    if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
        // Authentication passed...
        $authData = Auth::user();
        $userData = $authData['original'];
        Session::put('user', $authData['original']);
        return view('dashboard');
    }
    else
    {
         return view('auth/login');
    }    
}

public function logOut() {


    Auth::logout();
    Session::forget('user');
    return view('auth/login');

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM