sander3 /
laravel-magic-auth
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Soved\Laravel\Magic\Auth; |
||
| 4 | |||
| 5 | use Illuminate\Http\Request; |
||
| 6 | use Illuminate\Support\Facades\Auth; |
||
| 7 | use Illuminate\Foundation\Auth\RedirectsUsers; |
||
| 8 | use Soved\Laravel\Magic\Auth\Links\LinkBroker; |
||
| 9 | |||
| 10 | trait AuthenticatesUsers |
||
| 11 | { |
||
| 12 | use RedirectsUsers; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Handle a magic authentication request to the application. |
||
| 16 | * |
||
| 17 | * @param \Illuminate\Http\Request $request |
||
| 18 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response |
||
| 19 | */ |
||
| 20 | public function login(Request $request) |
||
| 21 | { |
||
| 22 | $this->validateLogin($request); |
||
| 23 | |||
| 24 | $response = $this->broker()->login( |
||
| 25 | $request, function ($user) { |
||
| 26 | $this->authenticateUser($user); |
||
| 27 | } |
||
| 28 | ); |
||
| 29 | |||
| 30 | return $response == LinkBroker::USER_AUTHENTICATED |
||
| 31 | ? $this->sendLoginResponse($request) |
||
| 32 | : $this->sendFailedLoginResponse($response); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Validate the user login request. |
||
| 37 | * |
||
| 38 | * @param \Illuminate\Http\Request $request |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | protected function validateLogin(Request $request) |
||
| 42 | { |
||
| 43 | $this->validate($request, ['email' => 'required|email']); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Authenticate the given user. |
||
| 48 | * |
||
| 49 | * @param \Soved\Laravel\Magic\Auth\Traits\CanMagicallyLogin $user |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | protected function authenticateUser($user) |
||
| 53 | { |
||
| 54 | $this->guard()->login($user, config('magic-auth.remember')); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Send the response after the user was authenticated. |
||
| 59 | * |
||
| 60 | * @param \Illuminate\Http\Request $request |
||
| 61 | * @return \Illuminate\Http\Response |
||
| 62 | */ |
||
| 63 | protected function sendLoginResponse(Request $request) |
||
| 64 | { |
||
| 65 | $request->session()->regenerate(); |
||
| 66 | |||
| 67 | return $this->authenticated($request, $this->guard()->user()) |
||
|
0 ignored issues
–
show
Are you sure the usage of
$this->authenticated($re...$this->guard()->user()) targeting Soved\Laravel\Magic\Auth...sUsers::authenticated() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 68 | ?: redirect()->intended($this->redirectPath()); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The user has been authenticated. |
||
| 73 | * |
||
| 74 | * @param \Illuminate\Http\Request $request |
||
| 75 | * @param mixed $user |
||
| 76 | * @return mixed |
||
| 77 | */ |
||
| 78 | protected function authenticated( |
||
| 79 | Request $request, |
||
| 80 | $user |
||
| 81 | ) { |
||
| 82 | $request->session()->put('viaMagicLink', true); |
||
| 83 | |||
| 84 | // Invalidating all unexpired magic links |
||
| 85 | $user->touch(); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get the failed login response instance. |
||
| 90 | * |
||
| 91 | * @param string $response |
||
| 92 | * @return \Illuminate\Http\RedirectResponse |
||
| 93 | */ |
||
| 94 | protected function sendFailedLoginResponse(string $response) |
||
| 95 | { |
||
| 96 | return redirect($this->redirectPath()) |
||
| 97 | ->withErrors(['email' => __($response)]); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get the broker to be used during magic authentication. |
||
| 102 | * |
||
| 103 | * @return \Soved\Laravel\Magic\Auth\Links\LinkBroker |
||
| 104 | */ |
||
| 105 | public function broker() |
||
| 106 | { |
||
| 107 | $userProvider = Auth::getProvider(); |
||
| 108 | |||
| 109 | return new LinkBroker($userProvider); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the guard to be used during magic authentication. |
||
| 114 | * |
||
| 115 | * @return \Illuminate\Contracts\Auth\StatefulGuard |
||
| 116 | */ |
||
| 117 | protected function guard() |
||
| 118 | { |
||
| 119 | return Auth::guard(); |
||
| 120 | } |
||
| 121 | } |
||
| 122 |