safriansah

ravel-auth-controller-checkToken

Oct 23rd, 2020
978
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function checkToken(Request $request){
  2.     try {
  3.         //code...
  4.         $validator = Validator::make($request->all(), [
  5.             'token' => 'required',
  6.         ]);
  7.         if ($validator->fails()) {
  8.             return $this->getResponse(406, $validator->errors()->first());
  9.         }
  10.  
  11.         $token = Token::where('code', $request->token)->where('status', 1)->first();
  12.         if (!$token) {
  13.             return $this->getResponse(401, 'Token undefined');
  14.         }
  15.        
  16.         $data = Crypt::decryptString($request->token);
  17.         $data = json_decode($data);
  18.         $diff = Carbon::createFromTimestamp($data->createdAt)->diffInHours(Carbon::now());
  19.         if ($diff >= (int)env('TOKEN_DURATION_HOUR', '24')) {
  20.             # code...
  21.            return $this->getResponse(401, 'Token expired');
  22.         }
  23.        
  24.         return $this->getResponse(200, false, $data);
  25.     } catch (\Throwable $th) {
  26.         //throw $th;
  27.         return $this->getResponse(500);
  28.     }
  29. }
RAW Paste Data