php Laravel 5 身份验证注销不起作用

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

Laravel 5 Auth logout not working

phplaravel

提问by mikelovelyuk

When I use the built-in Authentication and try to log the user out at /auth/logout- it does not work as hoped. It appears to keep the user logged in. But when I clear my browser cache, I can see that is has actually logged the user out.

当我使用内置身份验证并尝试将用户注销时/auth/logout- 它无法按预期工作。它似乎使用户保持登录状态。但是当我清除浏览器缓存时,我可以看到实际上已将用户注销。

I don't get any errors on the page nor errors in the log file.

我没有在页面上收到任何错误,也没有在日志文件中收到任何错误。

I am guessing that Session::flush()at the logout method would possibly solve this - but I don't know where to put it.. Can someone point me in the right direction?

我猜Session::flush()在注销方法中可能会解决这个问题 - 但我不知道把它放在哪里..有人能指出我正确的方向吗?

回答by lorey

For anyone that has problems solving it with the accepted solution: I started with Laravel 5.1 and updated to 5.2. The following fix worked for me:

对于使用公认的解决方案解决问题的任何人:我从 Laravel 5.1 开始并更新到 5.2。以下修复对我有用:

Try changing your 'logout' route to

尝试将您的“注销”路线更改为

Route::get('auth/logout', 'Auth\AuthController@logout');

or in AuthController constructor add

或在 AuthController 构造函数中添加

public function __construct()
{
    $this->middleware('guest', ['except' => ['logout', 'getLogout']]);
}

Taken from: https://stackoverflow.com/a/34667356/1275778(also check the other answers there if you're still having problems afterwards)

取自:https: //stackoverflow.com/a/34667356/1275778(如果之后仍有问题,请查看那里的其他答案)

回答by Deenadhayalan Manoharan

Try this..

尝试这个..

Put In following code "class AuthController extends Controller"

放入以下代码“ class AuthController extends Controller

public function getLogout()
    {
        $this->auth->logout();
        Session::flush();
        return redirect('/');
    }

回答by Daniel Doinov

I had the same problem. The problem was actually a simple little error in the configuration of the route and controller.

我有同样的问题。问题其实是路由和控制器配置中的一个简单的小错误。

You see the route actually specifies a method of getLogoutand the controller exception is looking for logout.

您会看到路由实际上指定了一个方法,getLogout并且控制器异常正在寻找logout

The only thing you need to do is change the exception in the controller. No need for any additional methods. The getLogoutmethod already exists and works perfectly.

您唯一需要做的就是更改控制器中的异常。不需要任何额外的方法。该getLogout方法已经存在并且可以完美运行。

Here is the actual code

这是实际的代码

app/Http/routes.php

应用程序/Http/routes.php

Route::get('auth/logout', 'Auth\AuthController@getLogout');

app/Http/Controllers/Auth/AuthController.php

应用程序/Http/Controllers/Auth/AuthController.php

public function __construct()
{
    $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}

The _constructmethod should look like that:

_construct方法应如下所示:

public function __construct()
{
    $this->middleware($this->guestMiddleware(), ['except' => 'getLogout']);
}

回答by Leo Lima

this ocurrs because the middleware is called for every route. you can add a exception to " logout route" in App\Http\Middleware\RedirectIfAuthenticated.php

发生这种情况是因为每个路由都会调用中间件。您可以在 App\Http\Middleware\RedirectIfAuthenticated.php 中为“注销路由”添加例外

class RedirectIfAuthenticated
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
    public function handle($request, Closure $next, $guard = null)
    {
        if (!$request->is('/logout') && Auth::guard($guard)->check()) {
            return redirect('/home');
        }

        return $next($request);
    }
}

回答by Kabir Hossain

For logout in laravel 5.6 :

在 laravel 5.6 中注销:

use in your view page:

在您的视图页面中使用:

    <a href="{{ url('logout') }}">Logout</a>

use this in your web.php

在你的 web.php 中使用它

Route::get('logout', 'Auth\LoginController@logout');

回答by Amarja

I had the same problem with updated laravel 5.2. I have used laravel's auth controller and I solved this problem using like,

我在更新 Laravel 5.2 时遇到了同样的问题。我使用了 laravel 的 auth 控制器,我使用类似的方法解决了这个问题,

/logoutinstead of /auth/logoutsame for /registerand /loginin instead of using /auth/registerand /auth/login.

/logout而不是/auth/logout相同的 for /registerand /loginin 而不是使用/auth/registerand /auth/login

回答by Valdir Lourenco

Laravel 5.2 the url is a little different ...

Laravel 5.2 的 url 有点不同......

Use this

用这个

php artisan make:auth

This will generate the routes for auth and some templates for login e register...

这将生成用于身份验证的路由和一些用于登录和注册的模板...

Be careful to use this with existing projects, it can make changes to your code

小心在现有项目中使用它,它可以更改您的代码

回答by Patrick Assoa Adou

Not enough on browsers that recover your tabs after crash (Chrome doesn't delete session cookies) . Also, after redirect a new session is created. Solution: in AuthController, as mentioned above, in getLogout, set a variable and pass it to redirect:

在崩溃后恢复标签的浏览器还不够(Chrome 不会删除会话 cookie)。此外,重定向后会创建一个新会话。解决方案:在AuthController中,如上所述,在getLogout中,设置一个变量并传递给redirect:

$data['logout'] = true;
return redirect('/')->with('data',$data);

In your home view do this:

在您的主视图中执行以下操作:

@if(session()->has('data') && session('data')['logout'])
{{session_unset()}}    
{{setcookie('laravel_session', "", -1, "/")}}
@endif

I believe Laravel redirect reinitialises Session. So after redirect, in view, reset delete cookie. Anybody can comment on this? Is this the right reason this works?

我相信 Laravel 重定向会重新初始化会话。所以重定向后,在视图中,重置删除cookie。任何人都可以对此发表评论吗?这是这样做的正确原因吗?

回答by Marcus Olsson

To log out a user with Laravel using the built in authentication tools, it is as simple as using Auth::logout();.

要使用内置的身份验证工具使用 Laravel 注销用户,就像使用Auth::logout();.

Please also check the various session settings in config/session.phpif the sessions behaves unpredictably.

config/session.php如果会话行为不可预测,还请检查各种会话设置。

回答by Hekmat

Solution is very very simple

解决方法非常非常简单

in Http->Middleware->Authenticate.php change "login" in else statement to "/"

在 Http->Middleware->Authenticate.php 中将 else 语句中的“登录”更改为“/”

return redirect()->guest('/');

return redirect()->guest('/');

and define following route in routes.php

并在 routes.php 中定义以下路由

Route::get('/', function () {
    return view('login');
});

for logout call following function: public function getlogout(){ \Auth::logout(); return redirect('/home'); } this is importantredirect to "/home" instead of "/" that first calls $this->middleware('auth'); and then in middleware redirect to "/"

对于注销调用以下函数: public function getlogout(){ \Auth::logout(); return redirect('/home'); } 这是非常重要的,以“/家”重定向而不是“/”,首先调用$这个- >中间件(“权威性”); 然后在中间件中重定向到“/”