safriansah

ravel-auth-controller-login

Oct 23rd, 2020
1,169
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function login(Request $request){
  2.     try {
  3.         //code...
  4.         $validator = Validator::make($request->all(), [
  5.             'username' => 'required|max:255',
  6.             'password' => 'required|max:255',
  7.         ]);
  8.         if ($validator->fails()) {
  9.             return $this->getResponse(406, $validator->errors()->first());
  10.         }
  11.  
  12.         $username = $request->username;
  13.         $user = Users::where('username', $username)->orWhereHas('profile', function ($query) use ($username) {
  14.             $query->where('email', $username)->orWhere('phone', $username);
  15.         })->where('status', 1)->first();
  16.         // dd($user);
  17.         if (!$user) {
  18.             # code...
  19.            return $this->getResponse(401, 'Username doesnt match');
  20.         }
  21.        
  22.         if (!Hash::check($request->password, $user->password) ) {
  23.             # code...
  24.            return $this->getResponse(401, 'Password doesnt match');
  25.         }
  26.        
  27.         $token = $this->generateToken($user);
  28.         if (!$token) {
  29.             # code...
  30.            return $this->getResponse(500);
  31.         }
  32.  
  33.         return $this->getResponse(200, false, [
  34.             'token' => $token
  35.         ]);
  36.     } catch (\Throwable $th) {
  37.         // throw $th;
  38.         return $this->getResponse(500);
  39.     }
  40. }
RAW Paste Data