44
55use App \Models \Access \User \User ;
66use Illuminate \Http \Request ;
7- use JWTAuth ;
7+ use Illuminate \ Support \ Facades \ Auth ;
88use Tymon \JWTAuth \Exceptions \JWTException ;
99use Validator ;
1010
@@ -20,8 +20,8 @@ class AuthController extends APIController
2020 public function login (Request $ request )
2121 {
2222 $ validation = Validator::make ($ request ->all (), [
23- 'email ' => 'required|email ' ,
24- 'password ' => 'required|min:4 ' ,
23+ 'email ' => 'required|email ' ,
24+ 'password ' => 'required|min:4 ' ,
2525 ]);
2626
2727 if ($ validation ->fails ()) {
@@ -31,17 +31,24 @@ public function login(Request $request)
3131 $ credentials = $ request ->only (['email ' , 'password ' ]);
3232
3333 try {
34- if (!$ token = JWTAuth:: attempt ($ credentials )) {
34+ if (!$ token = auth ( ' api ' )-> attempt ($ credentials )) {
3535 return $ this ->throwValidation (trans ('api.messages.login.failed ' ));
3636 }
3737 } catch (JWTException $ e ) {
3838 return $ this ->respondInternalError ($ e ->getMessage ());
3939 }
4040
41- return $ this ->respond ([
42- 'message ' => trans ('api.messages.login.success ' ),
43- 'token ' => $ token ,
44- ]);
41+ return $ this ->respondWithToken ($ token );
42+ }
43+
44+ /**
45+ * Get the authenticated User.
46+ *
47+ * @return \Illuminate\Http\JsonResponse
48+ */
49+ public function me ()
50+ {
51+ return response ()->json ($ this ->guard ()->user ());
4552 }
4653
4754 /**
@@ -51,19 +58,9 @@ public function login(Request $request)
5158 */
5259 public function logout ()
5360 {
54- try {
55- $ token = JWTAuth::getToken ();
61+ $ this ->guard ()->logout ();
5662
57- if ($ token ) {
58- JWTAuth::invalidate ($ token );
59- }
60- } catch (JWTException $ e ) {
61- return $ this ->respondInternalError ($ e ->getMessage ());
62- }
63-
64- return $ this ->respond ([
65- 'message ' => trans ('api.messages.logout.success ' ),
66- ]);
63+ return response ()->json (['message ' => 'Successfully logged out ' ]);
6764 }
6865
6966 /**
@@ -73,21 +70,79 @@ public function logout()
7370 */
7471 public function refresh ()
7572 {
76- $ token = JWTAuth::getToken ();
77-
78- if (!$ token ) {
79- $ this ->respondUnauthorized (trans ('api.messages.refresh.token.not_provided ' ));
80- }
81-
82- try {
83- $ refreshedToken = JWTAuth::refresh ($ token );
84- } catch (JWTException $ e ) {
85- return $ this ->respondInternalError ($ e ->getMessage ());
86- }
73+ return $ this ->respondWithToken ($ this ->guard ()->refresh ());
74+ }
8775
88- return $ this ->respond ([
89- 'status ' => trans ('api.messages.refresh.status ' ),
90- 'token ' => $ refreshedToken ,
76+ /**
77+ * Get the token array structure.
78+ *
79+ * @param string $token
80+ *
81+ * @return \Illuminate\Http\JsonResponse
82+ */
83+ protected function respondWithToken ($ token )
84+ {
85+ return response ()->json ([
86+ 'access_token ' => $ token ,
87+ // 'token_type' => 'bearer',
88+ // 'expires_in' => $this->guard()->factory()->getTTL() * 60
9189 ]);
9290 }
91+
92+ /**
93+ * Get the guard to be used during authentication.
94+ *
95+ * @return \Illuminate\Contracts\Auth\Guard
96+ */
97+ public function guard ()
98+ {
99+ return Auth::guard ('api ' );
100+ }
101+
102+ /*
103+ * Log the user out (Invalidate the token).
104+ *
105+ * @return \Illuminate\Http\JsonResponse
106+ */
107+ // public function logout()
108+ // {
109+ // try {
110+ // $token = JWTAuth::getToken();
111+
112+ // if ($token) {
113+ // JWTAuth::invalidate($token);
114+ // }
115+ // } catch (JWTException $e) {
116+ // return $this->respondInternalError($e->getMessage());
117+ // }
118+
119+ // return $this->respond([
120+ // 'message' => trans('api.messages.logout.success'),
121+ // ]);
122+ // }
123+
124+ /*
125+ * Refresh a token.
126+ *
127+ * @return \Illuminate\Http\JsonResponse
128+ */
129+ // public function refresh()
130+ // {
131+ // $token = JWTAuth::getToken();
132+
133+ // if (!$token) {
134+ // $this->respondUnauthorized(trans('api.messages.refresh.token.not_provided'));
135+ // }
136+
137+ // try {
138+ // $refreshedToken = JWTAuth::refresh($token);
139+ // } catch (JWTException $e) {
140+ // return $this->respondInternalError($e->getMessage());
141+ // }
142+
143+ // return $this->respond([
144+ // 'status' => trans('api.messages.refresh.status'),
145+ // 'token' => $refreshedToken,
146+ // ]);
147+ // }
93148}
0 commit comments