123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Support\Facades\Auth;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use App\User;
- use JWTAuth;
- use Tymon\JWTAuth\Exceptions\JWTException;
- class AuthController extends Controller
- {
- /**
- * Create a new AuthController instance.
- *
- * @return void
- */
- public function __construct()
- {
- // auth:api == jwt
- $this->middleware('jwt', ['except' => ['authenticate']]);
- }
- /**
- * Get a JWT via given credentials.
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function authenticate(Request $request)
- {
- $credentials = $request->only('email', 'password');
- $user = User::where('email',$request->email)->first();
- try {
- if (! $token = JWTAuth::attempt($credentials)) {
- return response()->json(['error' => 'invalid credentials'], 401);
- }
- } catch (JWTException $e) {
- return response()->json(['error' => 'could_not_create_token'], 500);
- }
- return response()->json(compact('token','user'),200);
- // if(isset($user) && !empty($user)){
- // if($user->status == 1){
- // return response()->json(compact('token','user'),200);
- // }else{
- // return response()->json(['error' => 'Account not yet activated'], 401);
- // }
- // }
- }
- //profile
- public function profile()
- {
- try {
- if (! $user = JWTAuth::parseToken()->authenticate()) {
- return response()->json(['user_not_found'], 404);
- }
- } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
- return response()->json(['token_expired'], $e->getStatusCode());
- } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
- return response()->json(['token_invalid'], $e->getStatusCode());
- } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
- return response()->json(['token_absent'], $e->getStatusCode());
- }
- return response()->json(compact('user'));
- }
- /**
- * Get the authenticated User.
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function me()
- {
- return response()->json(auth()->user());
- }
- /**
- * Log the user out (Invalidate the token).
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function logout()
- {
- auth()->logout();
- return response()->json(['message' => 'Successfully logged out']);
- }
- /**
- * Refresh a token.
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function refresh()
- {
- return $this->respondWithToken(auth()->refresh());
- }
- /**
- * Get the token array structure.
- *
- * @param string $token
- *
- * @return \Illuminate\Http\JsonResponse
- */
- protected function respondWithToken($token)
- {
- return response()->json([
- 'access_token' => $token,
- 'token_type' => 'bearer',
- 'expires_in' => auth()->factory()->getTTL() * 60
- ]);
- }
- }
|