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.

图片转代码服务由CSDN问答提供 功能建议

我遇到了问题和疑问: 首先这里是我的代码: < p> AuthMiddleware.php

 公共函数句柄($ request,Closure $ next)
 {
 if if(auth() - &gt; check()){
  return $ next($ request); 
} else {
 if(auth() - &gt; viaRemember()){
 return $ next($ request); 
} 
} 
 
 
  return redirect() - &gt; route('auth.index'); 
 
} 
   
 
 

AuthController.php < pre> if(auth() - &gt; attempt(compact('email','password'),true)){ 返回redirect() - &gt; route('dashboard'); }

Kernel.php

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

routes.php

  Route  :: group(['prefix'=&gt;'auth'],function(){
 Route :: get('/','AuthController @ index') - &gt; name('auth.index'); \  n Route :: post('login','AuthController @ login'); 
}); 
 
Route :: group(['middleware'=&gt;'auth.check'],function(){
  Route :: get('dashboard',function(){
 return view('front.welcome'); 
}) - &gt; name('dashboard'); 
}); 
    
 
 

以下是问题所在:

  1. 每次关闭浏览器时(使用 expire_on_close:true < / code> on session file)页面将重定向到 / login 而不是 / auth 。 我找不到代码为什么它将我重定向到 / login 。 任何解决方案?

  2. 我想应用rememberMe功能。 但每次我关闭浏览器时,它总是会将我注销。 我试过设置 expire_on_close:false 。 是的,它正在运行,但每次我运行 auth() - &gt; viaRemember()时,它总是返回false。 有什么建议吗?

    我的会话使用文件。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题