/** * Decode token request header * * @return object */ public static function getToken() { $jwt = \JWTAuth::getToken(); if (!$jwt) { return null; } return \JWTAuth::decode($jwt)->get(); }
public function info() { try { $token = \JWTAuth::getToken(); $payload = \JWTAuth::getPayload($token); } catch (JWTException $e) { return ['error' => 'no auth token provided']; } $user = User::where('id', $payload['user_id'])->first(); return $user; }
public function logout() { //获取当前用户token $token = \JWTAuth::getToken(); //让token失效 try { \JWTAuth::invalidate($token); return return_rest('1', '', '已退出登录'); } catch (\Exception $e) { return return_rest('0', '', $e->getMessage()); } }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if (!\JWTAuth::getToken()) { return response()->json(['status' => 401, 'error' => 'token_absent'], 200); } else { try { $authenticate = \JWTAuth::parseToken()->authenticate(); if (!$authenticate) { return response()->json(['status' => 401, 'error' => 'user_not_found'], 200); } } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) { return response()->json(['status' => 401, 'error' => 'token_expired'], 200); } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) { return response()->json(['status' => 401, 'error' => 'token_invalid'], 200); } } return $next($request); }
if ($validator->fails()) { return response()->json(['error' => 'Validation Error', 'validation_errors' => $validator->errors()], 400); } $credentials = $request->only('email', 'password'); try { $user = User::where('email', $request->email)->firstOrFail(); } catch (ModelNotFoundException $e) { return response()->json(['error' => 'invalid_credentials'], 401); } if (!($token = JWTAuth::attempt($credentials, ['name' => $user->name]))) { return response()->json(['error' => 'invalid_credentials'], 401); } return response()->json(compact('token')); }); // Test route for restricted data (to be removed) Route::get('restricted', ['middleware' => 'jwt.auth', function () { $token = JWTAuth::getToken(); $user = JWTAuth::toUser($token); return response()->json(['data' => ['email' => $user->email, 'registered_at' => $user->created_at->toDateTimeString()]]); }]); Route::get('user/{id}', function ($id) { $user = User::find($id); return response()->json(['user' => $user]); }); Route::get('test', function () { $users = User::paginate(10); return $users; }); Route::resource('topic/{id}/posts', 'TopicController@showPosts'); Route::resource('topic', 'TopicController'); Route::resource('post', 'PostController');
| | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::resource('delegation', 'DelegationController'); Route::post('authenticate', 'AuthenticateController@authenticate'); Route::post('authenticate/resetpassword', 'AuthenticateController@resetPassword'); Route::get('authenticate/{remember_token}', 'AuthenticateController@noPassword'); Route::get('background_image', 'BackgroundImageController@index'); // access across site because users need it to see what's going on, maybe?? Route::resource('setting', 'SettingController'); Route::get('/settings', function () { $user = null; if ($token = JWTAuth::getToken()) { $user = JWTAuth::parseToken()->authenticate(); } // return only particular keys like themename and background image i guess // question is: can this be handled in the controller // // need to run settings/create onload... // Setting::get('background_image', null); // Setting::get('theme', []); return Setting::all(); }); Route::get('/', function () { return view('index'); }); Route::group(array('middleware' => 'testing', 'prefix' => 'api'), function () { Route::resource('user', 'UserController');
public function refresh() { $oldToken = JWTAuth::getToken(); // try { $newToken = JWTAuth::refresh($oldToken); // } catch (Exception $e) { // return $this->response->errorBadRequest($e); // } return $newToken; }