22
33namespace App \Http \Controllers \Api \V1 ;
44
5- use App \Models \Access \User \User ;
65use Illuminate \Http \Request ;
76use Illuminate \Support \Facades \Auth ;
8- use Tymon \JWTAuth \Exceptions \JWTException ;
97use Validator ;
108
119class AuthController extends APIController
@@ -31,14 +29,26 @@ public function login(Request $request)
3129 $ credentials = $ request ->only (['email ' , 'password ' ]);
3230
3331 try {
34- if (!$ token = auth ( ' api ' )-> attempt ($ credentials )) {
32+ if (!Auth:: attempt ($ credentials )) {
3533 return $ this ->throwValidation (trans ('api.messages.login.failed ' ));
3634 }
37- } catch (JWTException $ e ) {
35+
36+ $ user = $ request ->user ();
37+
38+ $ passportToken = $ user ->createToken ('API Access Token ' );
39+
40+ // Save generated token
41+ $ passportToken ->token ->save ();
42+
43+ $ token = $ passportToken ->accessToken ;
44+ } catch (\Exception $ e ) {
3845 return $ this ->respondInternalError ($ e ->getMessage ());
3946 }
4047
41- return $ token ;
48+ return $ this ->respond ([
49+ 'message ' => trans ('api.messages.login.success ' ),
50+ 'token ' => $ token ,
51+ ]);
4252 }
4353
4454 /**
@@ -56,95 +66,16 @@ public function me()
5666 *
5767 * @return \Illuminate\Http\JsonResponse
5868 */
59- public function logout ()
60- {
61- $ this ->guard ()->logout ();
62-
63- return response ()->json (['message ' => 'Successfully logged out ' ]);
64- }
65-
66- /**
67- * Refresh a token.
68- *
69- * @return \Illuminate\Http\JsonResponse
70- */
71- public function refresh ()
69+ public function logout (Request $ request )
7270 {
73- return $ this ->respondWithToken ($ this ->guard ()->refresh ());
74- }
75-
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 $ token ;
71+ try {
72+ $ request ->user ()->token ()->revoke ();
73+ } catch (\Exception $ e ) {
74+ return $ this ->respondInternalError ($ e ->getMessage ());
75+ }
8676
87- return response ()->json ([
88- 'access_token ' => $ token ,
89- // 'token_type' => 'bearer',
90- // 'expires_in' => $this->guard()->factory()->getTTL() * 60
77+ return $ this ->respond ([
78+ 'message ' => trans ('api.messages.logout.success ' ),
9179 ]);
9280 }
93-
94- /**
95- * Get the guard to be used during authentication.
96- *
97- * @return \Illuminate\Contracts\Auth\Guard
98- */
99- public function guard ()
100- {
101- return Auth::guard ('api ' );
102- }
103-
104- /*
105- * Log the user out (Invalidate the token).
106- *
107- * @return \Illuminate\Http\JsonResponse
108- */
109- // public function logout()
110- // {
111- // try {
112- // $token = JWTAuth::getToken();
113-
114- // if ($token) {
115- // JWTAuth::invalidate($token);
116- // }
117- // } catch (JWTException $e) {
118- // return $this->respondInternalError($e->getMessage());
119- // }
120-
121- // return $this->respond([
122- // 'message' => trans('api.messages.logout.success'),
123- // ]);
124- // }
125-
126- /*
127- * Refresh a token.
128- *
129- * @return \Illuminate\Http\JsonResponse
130- */
131- // public function refresh()
132- // {
133- // $token = JWTAuth::getToken();
134-
135- // if (!$token) {
136- // $this->respondUnauthorized(trans('api.messages.refresh.token.not_provided'));
137- // }
138-
139- // try {
140- // $refreshedToken = JWTAuth::refresh($token);
141- // } catch (JWTException $e) {
142- // return $this->respondInternalError($e->getMessage());
143- // }
144-
145- // return $this->respond([
146- // 'status' => trans('api.messages.refresh.status'),
147- // 'token' => $refreshedToken,
148- // ]);
149- // }
15081}
0 commit comments