简体   繁体   English

登录后密码保护页面 Laravel

[英]Password Protect a Page after login Laravel

After a user registers and logs in, I have an unlisted page/secret page that I need to protect with another password.用户注册并登录后,我有一个未列出的页面/秘密页面,我需要用另一个密码保护它。

I'm trying to get spatie/laravel-littlegatekeeper to help me do this, but running into issues getting it working.我正在尝试让spatie/laravel-littlegatekeeper帮助我做到这一点,但遇到了让它工作的问题。

What I'm doing:我在做什么:

littlegatekeeper.config: littlegatekeeper.config:

<?php

return [
    // Login credentials
    'username' => env('GATEKEEPER_USERNAME', 'default_username'),
    'password' => env('GATEKEEPER_PASSWORD', 'default_password'),

    // The key as which the littlegatekeeper session is stored
    'sessionKey' => 'littlegatekeeper.loggedin',

    // The route to which the middleware redirects if a user isn't authenticated
    // 'authRoute' => url('login'),
];

Routes:路线:

Route::get('/secretapage', ['middleware' => 'littlegatekeeper', function () {
    return view('dir.secretapage.index');
}]);

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

Route::post('/secretapage/login/addCredentials', 'SecretController@addCredentials')->name('addCredentials');

SecretController:秘密控制器:

After I log in my user.在我登录我的用户后。 I then try to access the URL /secretpage I get redirected back to the homepage rather the /secretpage/login然后我尝试访问 URL /secretpage 我被重定向回主页而不是 /secretpage/login

    public function index(Request $request)
    {
        $auth = resolve('littlegatekeeper');

        if($auth->isAuthenticated())
        {
            return view('dir.secretpage.index');
        } 

        return view('dir.secretpage.login');

    }

///// FOR LOGING IN

        public function addCredentials(Request $request)
    {

        $auth = resolve('littlegatekeeper');

        $loginSuccess = $auth->attempt($request->only([
            'username',
            'password'
        ]));

        if ($loginSuccess) {
            return redirect('/secretapage')->with('success', 'Thank You for authorizing. Please proceed.');
        }
        else{
            return back()->with('error', 'You entered the wrong credentials');
        }

    }

Blade login file:刀片登录文件:

<form method="POST" action="{{ route('addCredentials') }}">
...
</form>

If I access secretpage/login 1st, I'm able to add the username and password.如果我访问 secretpage/login 1st,我可以添加用户名和密码。 Then I can get into /secretpage with no issues....然后我可以毫无问题地进入 /secretpage....

But I really need to have the users go to /secretpage 1st then if not logged in with the secret username/pass get redirected to /secretpage/login.但我真的需要让用户 go 到 /secretpage 1st,然后如果没有使用秘密用户名/密码登录,则重定向到 /secretpage/login。

I found some help on Laracasts and this ended up working.我在Laracasts上找到了一些帮助,这最终奏效了。

Change the authRoute in the littlegatekeeper config file to the followinglittlegatekeeper config file to the following authRoute

'authRoute' => '/secretpage/login',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM