safriansah

ravel-auth-controller-generateToken

Oct 23rd, 2020
1,638
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function generateToken($user){
  2.     try {
  3.         //code...
  4.         $time = Carbon::now();
  5.         $data = [
  6.             "id" => $user->id,
  7.             "username" => $user->username,
  8.             "fullname" => $user->profile->fullname,
  9.             "email" => $user->profile->email,
  10.             "phone" => $user->profile->phone,
  11.             "createdAt" => $time->timestamp
  12.         ];
  13.         $code = Crypt::encryptString(json_encode($data));
  14.  
  15.         $token = new Token;
  16.         $token->id_user = $user->id;
  17.         $token->code = $code;
  18.         $token->status = 1;
  19.         $token->save();
  20.  
  21.         return $code;
  22.     } catch (\Throwable $th) {
  23.         //throw $th;
  24.         return false;
  25.     }
  26. }
RAW Paste Data