简体   繁体   中英

Laravel restrict site by password

I need to provide authentication my Laravel 5 site by password (no username, just only password). Access to site will possible only if user type correct password. It can't be basic server authorization, because I have to implement some design.

I know it is possible to do it with multiple authentication in Laravel, but I have no idea how to make it working with my standard (user and password) authentication in my application.

Can someone help how to do it, please?

built a manual authentication function to check for only password.

    public function authenticate(Request $request)
    {
        $password=$request->get('password');
        if (Auth::attempt(['password' => $password]) )
        {     
            return redirect()->intended('/home');   
        }
        else 
        {
            return redirect('/login');
        }
     }

You could do something like:

Route::get('/', function () {
    if (session('password_entered')) {
        return view('homepage');
    }

    return view('enter_password_page');
});

Route::post('/', function () {
    $password_to_website = 'RedMonkey';
    $password = request('password_field');

    if ($password && $password === $password_to_website) {
        session(['password_entered' => true]);
        return view('homepage');
    }

    return back()->withInput()->withErrors(['password' => 'Password is incorect']);
});

I have not tried this but the idea is there. You can make middleware out of this or just use it straight up in route/controller.

If I've understood your question correctly, you want to have just password authentication on your site without the need for a username. To accomplish this I would do the following:

  • Create a user with the email 'passwordlogin@yoursite.com' and your desired password

  • Update your login form to only require a password

     <form method="POST" action="/login"> {!! csrf_field() !!} <input type="password" id="fieldPassword" name="password" required> <label for="fieldPassword" class="label">Password</label> 
  • Change your POST '/login' route to trigger a method called 'passwordLogin' in the Auth\\AuthController class

     <?php Route::post('/login', 'Auth\\AuthController@passwordLogin'); 
  • Create a method called 'passwordLogin' in the Auth\\AuthController class that uses Auth::attempt() to verify a password. Hardcode the email address to the same one you set in step 1

     public function passwordLogin(Request $request){ if($request->has('password')){ if(Auth::attempt(['email' => 'passwordlogin@yoursite.com', 'password' => $request->input('password')])) { // User has the correct password return redirect()->('/home'); } } // Login failed return redirect()->intended('/login'); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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