问题
If a user tries to log into my Laravel application and the password won't match with the "password" column in the database, I want to check it against another password column ("old_system_password") too.
I'm using the default Laravel authentication system, and as far as I understand I should be able to create a new "AuthController" and override the built in authentication methods. But I don't find any methods dealing with password matching in the vendor folder, and neither do I know how to tell Laravel to use my method instead of the default.
I've searched the web for this, but any solutions I find seems to be only for older versions of Laravel.
Any ideas?
回答1:
Probably try using the Middleware of the authentication looking like this by default:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
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 (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
Try to check if the user has been authenticated then if not call a function that will take your password and check in your database.
回答2:
First Solution: Just override public function authenticate()
in LoginController.php
public function authenticate()
{
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return redirect()->intended('dashboard');
} else if(Auth::attempt(['email' => $email, 'password' => $old_password]) {
return redirect()->intended('dashboard');
}
}
Check :https://laravel.com/docs/5.4/authentication#authenticating-users
Second Solution:
1:Run php artisan event:generate
to generate laravel events.
2:Run php artisan make:event CheckOldPassword
3:Add Event in EventServiceProvider.php 'Illuminate\Auth\Events\Failed' => ['App\Listeners\CheckOldPassword'],
4:Create function public function handle(Failed $event){}
5:Manually check logins in event.
来源:https://stackoverflow.com/questions/43171513/custom-login-password-check-with-laravel-5-4