Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/AuthController.php
Show All 11 Lines | |||||
{ | { | ||||
/** | /** | ||||
* Get the authenticated User | * Get the authenticated User | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function info() | public function info() | ||||
{ | { | ||||
$user = $this->guard()->user(); | $user = Auth::guard()->user(); | ||||
$response = V4\UsersController::userResponse($user); | $response = V4\UsersController::userResponse($user); | ||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
* Helper method for other controllers with user auto-logon | * Helper method for other controllers with user auto-logon | ||||
* functionality | * functionality | ||||
* | * | ||||
* @param \App\User $user User model object | * @param \App\User $user User model object | ||||
*/ | */ | ||||
public static function logonResponse(User $user) | public static function logonResponse(User $user) | ||||
{ | { | ||||
$token = auth()->login($user); | $token = Auth::guard()->login($user); | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'access_token' => $token, | 'access_token' => $token, | ||||
'token_type' => 'bearer', | 'token_type' => 'bearer', | ||||
// @phpstan-ignore-next-line | |||||
'expires_in' => Auth::guard()->factory()->getTTL() * 60, | 'expires_in' => Auth::guard()->factory()->getTTL() * 60, | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Get a JWT token via given credentials. | * Get a JWT token via given credentials. | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request The API request. | * @param \Illuminate\Http\Request $request The API request. | ||||
Show All 12 Lines | public function login(Request $request) | ||||
); | ); | ||||
if ($v->fails()) { | if ($v->fails()) { | ||||
return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | ||||
} | } | ||||
$credentials = $request->only('email', 'password'); | $credentials = $request->only('email', 'password'); | ||||
if ($token = $this->guard()->attempt($credentials)) { | if ($token = Auth::guard()->attempt($credentials)) { | ||||
$sf = new \App\Auth\SecondFactor($this->guard()->user()); | $sf = new \App\Auth\SecondFactor(Auth::guard()->user()); | ||||
if ($response = $sf->requestHandler($request)) { | if ($response = $sf->requestHandler($request)) { | ||||
return $response; | return $response; | ||||
} | } | ||||
return $this->respondWithToken($token); | return $this->respondWithToken($token); | ||||
} | } | ||||
return response()->json(['status' => 'error', 'message' => __('auth.failed')], 401); | return response()->json(['status' => 'error', 'message' => __('auth.failed')], 401); | ||||
} | } | ||||
/** | /** | ||||
* Log the user out (Invalidate the token) | * Log the user out (Invalidate the token) | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function logout() | public function logout() | ||||
{ | { | ||||
$this->guard()->logout(); | Auth::guard()->logout(); | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'message' => __('auth.logoutsuccess') | 'message' => __('auth.logoutsuccess') | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Refresh a token. | * Refresh a token. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function refresh() | public function refresh() | ||||
{ | { | ||||
return $this->respondWithToken($this->guard()->refresh()); | return $this->respondWithToken(Auth::guard()->refresh()); | ||||
} | } | ||||
/** | /** | ||||
* Get the token array structure. | * Get the token array structure. | ||||
* | * | ||||
* @param string $token Respond with this token. | * @param string $token Respond with this token. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
protected function respondWithToken($token) | protected function respondWithToken($token) | ||||
{ | { | ||||
return response()->json( | return response()->json( | ||||
[ | [ | ||||
'access_token' => $token, | 'access_token' => $token, | ||||
'token_type' => 'bearer', | 'token_type' => 'bearer', | ||||
'expires_in' => $this->guard()->factory()->getTTL() * 60 | // @phpstan-ignore-next-line | ||||
'expires_in' => Auth::guard()->factory()->getTTL() * 60 | |||||
] | ] | ||||
); | ); | ||||
} | } | ||||
/** | |||||
* Get the guard to be used during authentication. | |||||
* | |||||
* @return \Illuminate\Contracts\Auth\Guard | |||||
*/ | |||||
public function guard() | |||||
{ | |||||
return Auth::guard(); | |||||
} | |||||
} | } |