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.

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改