doucong1992 2017-02-22 03:00
浏览 34
已采纳

Laravel 5.4认证

I've got questions and problems: First here is my code:

AuthMiddleware.php

public function handle($request, Closure $next)
{
    if (auth()->check()) {
        return $next($request);
    } else {
        if (auth()->viaRemember()) {
            return $next($request);
        }
    }


    return redirect()->route('auth.index');

}

AuthController.php

if (auth()->attempt(compact('email', 'password'), true)) {

        return redirect()->route('dashboard');
}

Kernel.php

protected $routeMiddleware = [
    'auth.check'       => \App\Http\Middleware\AuthMiddleware::class,
 ]

routes.php

 Route::group(['prefix' => 'auth'], function () {
    Route::get('/', 'AuthController@index')->name('auth.index');
    Route::post('login', 'AuthController@login');
});

Route::group(['middleware' => 'auth.check'], function () {
    Route::get('dashboard', function () {
        return view('front.welcome');
    })->name('dashboard');
});

Here is the problem:

  1. Every time I close my browser (with expire_on_close:true on session file) the page will redirect me to /login instead of /auth. I can't find the code why it redirect me to /login. Any solution?

  2. I want to apply rememberMe function. But every time I close my browser it always log me out. I've tried set expire_on_close:false. Yes, it's working but everytime I run auth()->viaRemember() it always return me false. Any suggestion?

My Session using file.

  • 写回答

1条回答 默认 最新

  • doubeng9407 2017-02-22 03:11
    关注

    1- Check app/Exceptions/Handler.php That's where the redirect to login is. Function name is unauthenticated();

    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
    
        return redirect()->guest('login'); //HERE
    }
    

    2- When you remember, just doing Auth::check() or Auth::viaRemember() should work when the user comes back to the website.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建