AuthController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\Auth;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Http\Request;
  6. use App\User;
  7. use JWTAuth;
  8. use Tymon\JWTAuth\Exceptions\JWTException;
  9. class AuthController extends Controller
  10. {
  11. /**
  12. * Create a new AuthController instance.
  13. *
  14. * @return void
  15. */
  16. public function __construct()
  17. {
  18. // auth:api == jwt
  19. $this->middleware('jwt', ['except' => ['authenticate']]);
  20. }
  21. /**
  22. * Get a JWT via given credentials.
  23. *
  24. * @return \Illuminate\Http\JsonResponse
  25. */
  26. public function authenticate(Request $request)
  27. {
  28. $credentials = $request->only('email', 'password');
  29. $user = User::where('email',$request->email)->first();
  30. try {
  31. if (! $token = JWTAuth::attempt($credentials)) {
  32. return response()->json(['error' => 'invalid credentials'], 401);
  33. }
  34. } catch (JWTException $e) {
  35. return response()->json(['error' => 'could_not_create_token'], 500);
  36. }
  37. return response()->json(compact('token','user'),200);
  38. // if(isset($user) && !empty($user)){
  39. // if($user->status == 1){
  40. // return response()->json(compact('token','user'),200);
  41. // }else{
  42. // return response()->json(['error' => 'Account not yet activated'], 401);
  43. // }
  44. // }
  45. }
  46. //profile
  47. public function profile()
  48. {
  49. try {
  50. if (! $user = JWTAuth::parseToken()->authenticate()) {
  51. return response()->json(['user_not_found'], 404);
  52. }
  53. } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
  54. return response()->json(['token_expired'], $e->getStatusCode());
  55. } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
  56. return response()->json(['token_invalid'], $e->getStatusCode());
  57. } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {
  58. return response()->json(['token_absent'], $e->getStatusCode());
  59. }
  60. return response()->json(compact('user'));
  61. }
  62. /**
  63. * Get the authenticated User.
  64. *
  65. * @return \Illuminate\Http\JsonResponse
  66. */
  67. public function me()
  68. {
  69. return response()->json(auth()->user());
  70. }
  71. /**
  72. * Log the user out (Invalidate the token).
  73. *
  74. * @return \Illuminate\Http\JsonResponse
  75. */
  76. public function logout()
  77. {
  78. auth()->logout();
  79. return response()->json(['message' => 'Successfully logged out']);
  80. }
  81. /**
  82. * Refresh a token.
  83. *
  84. * @return \Illuminate\Http\JsonResponse
  85. */
  86. public function refresh()
  87. {
  88. return $this->respondWithToken(auth()->refresh());
  89. }
  90. /**
  91. * Get the token array structure.
  92. *
  93. * @param string $token
  94. *
  95. * @return \Illuminate\Http\JsonResponse
  96. */
  97. protected function respondWithToken($token)
  98. {
  99. return response()->json([
  100. 'access_token' => $token,
  101. 'token_type' => 'bearer',
  102. 'expires_in' => auth()->factory()->getTTL() * 60
  103. ]);
  104. }
  105. }