mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-10-31 10:16:52 +01:00 
			
		
		
		
	 70c31eef8f
			
		
	
	
		70c31eef8f
		
			
		
	
	
	
	
		
			
			* remove AccountTransformer and update UserTransformer (client api) to match UserTransformer (application api) * rename "toVueObject" * fix tests * forgot to rename this * backwards compat * fix tests
		
			
				
	
	
		
			107 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Http\Controllers\Auth;
 | |
| 
 | |
| use Illuminate\Http\Request;
 | |
| use App\Models\User;
 | |
| use Illuminate\Auth\AuthManager;
 | |
| use Illuminate\Http\JsonResponse;
 | |
| use Illuminate\Auth\Events\Failed;
 | |
| use Illuminate\Container\Container;
 | |
| use Illuminate\Support\Facades\Event;
 | |
| use App\Events\Auth\DirectLogin;
 | |
| use App\Exceptions\DisplayException;
 | |
| use App\Http\Controllers\Controller;
 | |
| use Illuminate\Contracts\Auth\Authenticatable;
 | |
| use Illuminate\Foundation\Auth\AuthenticatesUsers;
 | |
| 
 | |
| abstract class AbstractLoginController extends Controller
 | |
| {
 | |
|     use AuthenticatesUsers;
 | |
| 
 | |
|     protected AuthManager $auth;
 | |
| 
 | |
|     /**
 | |
|      * Lockout time for failed login requests.
 | |
|      */
 | |
|     protected int $lockoutTime;
 | |
| 
 | |
|     /**
 | |
|      * After how many attempts should logins be throttled and locked.
 | |
|      */
 | |
|     protected int $maxLoginAttempts;
 | |
| 
 | |
|     /**
 | |
|      * Where to redirect users after login / registration.
 | |
|      */
 | |
|     protected string $redirectTo = '/';
 | |
| 
 | |
|     /**
 | |
|      * LoginController constructor.
 | |
|      */
 | |
|     public function __construct()
 | |
|     {
 | |
|         $this->lockoutTime = config('auth.lockout.time');
 | |
|         $this->maxLoginAttempts = config('auth.lockout.attempts');
 | |
|         $this->auth = Container::getInstance()->make(AuthManager::class);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Get the failed login response instance.
 | |
|      *
 | |
|      * @throws \App\Exceptions\DisplayException
 | |
|      */
 | |
|     protected function sendFailedLoginResponse(Request $request, Authenticatable $user = null, string $message = null)
 | |
|     {
 | |
|         $this->incrementLoginAttempts($request);
 | |
|         $this->fireFailedLoginEvent($user, [
 | |
|             $this->getField($request->input('user')) => $request->input('user'),
 | |
|         ]);
 | |
| 
 | |
|         if ($request->route()->named('auth.login-checkpoint')) {
 | |
|             throw new DisplayException($message ?? trans('auth.two_factor.checkpoint_failed'));
 | |
|         }
 | |
| 
 | |
|         throw new DisplayException(trans('auth.failed'));
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Send the response after the user was authenticated.
 | |
|      */
 | |
|     protected function sendLoginResponse(User $user, Request $request): JsonResponse
 | |
|     {
 | |
|         $request->session()->remove('auth_confirmation_token');
 | |
|         $request->session()->regenerate();
 | |
| 
 | |
|         $this->clearLoginAttempts($request);
 | |
| 
 | |
|         $this->auth->guard()->login($user, true);
 | |
| 
 | |
|         Event::dispatch(new DirectLogin($user, true));
 | |
| 
 | |
|         return new JsonResponse([
 | |
|             'data' => [
 | |
|                 'complete' => true,
 | |
|                 'intended' => $this->redirectPath(),
 | |
|                 'user' => $user->toReactObject(),
 | |
|             ],
 | |
|         ]);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Determine if the user is logging in using an email or username.
 | |
|      */
 | |
|     protected function getField(string $input = null): string
 | |
|     {
 | |
|         return ($input && str_contains($input, '@')) ? 'email' : 'username';
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Fire a failed login event.
 | |
|      */
 | |
|     protected function fireFailedLoginEvent(Authenticatable $user = null, array $credentials = [])
 | |
|     {
 | |
|         Event::dispatch(new Failed('auth', $user, $credentials));
 | |
|     }
 | |
| }
 |