如何在laravel 5.5控制器中完全注销?

时间:2018-04-29 01:03:33

标签: php laravel authentication laravel-5

在我退出我的laravel应用程序后,在浏览器中我按下按钮向后(返回)然后我看到仪表板。

我想要消除这个"会话"如果我回去的那个laravel mantein。

任何人都可以帮助我吗?

编辑:我有两个登录文件,一个在Controllers / Auth中,另一个在Controller /中。我确定这不是一个好习惯,但它可以让我的系统保持正常运行。怎么解决这个?

控制器/认证/ LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;


class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */

    private $user;

}

我的登录控制器/ LoginController.php - &gt;

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;


class LoginController extends Controller
{
    private $user;


    public function logout(){
        Auth::logout();
        \Session::flash('success',"logout");
        return redirect()->route('login');
    }

}

我的DashboardController - &gt;

use App\Authorization;
use App\BackLog;
use App\Key;
use App\isKeyInUse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

class DashboardController extends Controller
{

    public function index() {

        return view('dashboard');
    }


}

我的web.php - &gt;

<?php

Route::get('/', 'LoginController@login')->name('login');
Route::get('auth/logout', 'Auth\LoginController@logout')->name('logout');

Route::get('/dashboard', 'DashboardController@index')->name('dashboard')->middleware('auth');
Route::post('/dashboard/getKey', 'DashboardController@getKey')->name('dashboard.key')->middleware('auth');

5 个答案:

答案 0 :(得分:2)

发生这种情况是因为缓存。为了防止我们创建一个拦截每个请求的middleware并将val xmlFile = File("Users.xml") val doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile) var list = doc.getElementsByTagName("user") for (i in 0 until list.length) { var current = list.item(i) if (current.attributes.getNamedItem("id").nodeValue == username) { println(current.childNodes.length) for (j in 0 until current.childNodes.length) { if (current.childNodes.item(j).nodeName == "password") { return current.childNodes.item(j).textContent == password } } } } return false 设置为在val doc = SAXReader().read(File("users.xml")) val users = doc.rootElement.elements("user") for (user in users) { if (user.attribute("id").value == username) { return user.element("password").text == password } } return false 时间到期,因此当用户按下后退按钮时会强制页面重新加载创建中间件的步骤:

第一

创建一个中间件,我将其称为cache

0

第二

MyAuth

中注册中间件
php artisan make:middleware MyAuth

第三

在新创建的中间件app/Http/kernel.php

 protected $routeMiddleware = [
      ...,
      'my_auth' => \App\Http\Middleware\MyAuth::class,
   ];

然后

您可以像这样添加中间件:

app/Http/Middleware/MyAuth.php

此代码源自此video

答案 1 :(得分:0)

将这些行插入仪表板控制器,然后检查:

public function __contruct() 
{
    $this->middleware('auth');
}

这将检查用户是否已登录?如果用户已注销,则它将发送到您在auth中间件中定义的特定登录页面。

答案 2 :(得分:0)

  

您在登出功能中缺少请求

    public function logout(Request $request){
        Auth::logout();
        \Session::flash('success',"logout");
        return redirect()->route('login');
    }
  

并在仪表板控制器中写下

public function __construct() 
{
    $this->middleware('auth');
}

答案 3 :(得分:0)

按浏览器的“后退”按钮将加载以前加载的文档。它只是可见但不会确定。为此,您只需要覆盖来自javascript的back press事件。

请参阅链接How to Detect Browser Back Button event - Cross Browser

答案 4 :(得分:0)

在Laravel 7.x中,您可以使用以下命令从控制器注销:

Auth::logout()