middleware('auth:api', ['except' => ['login']]); } public function login(Request $request) { $username = $request->username; $password = $request->password; $is_mobile = $request->is_mobile; if(empty($username) || empty($password)){ return response()->json(['status'=>'error','message'=>'You must fill all the fields'], 400); } $credentials = array( "username"=> $username, "password"=> md5($password) ); $user = User::where('username', $username)->where('password', md5($password)) ->first(); if($is_mobile){ $fcm_token = $request->fcm_token; if(!$fcm_token || $fcm_token==""){ return response()->json(['status'=>'error','message'=>'FCM Token is required'], 400); die(); } $dataUpdateFcm = array( "fcm_token"=>$fcm_token ); $hr = User::find($user->id); if($hr){ $result = $hr->update($dataUpdateFcm); } } $dataRole = Role::find($user->role_id); if($dataRole){ $user->role = $dataRole; } if (! $token =Auth::login($user)) { return response()->json(['error' => 'Unauthorized'], 401); } return $this->respondWithToken($token, $user); } protected function respondWithToken($token, $user) { return response()->json([ 'code' => 200, 'data' => array( 'data_user' => $user, 'access_token' => $token, 'token_type' => 'bearer', 'expires_in' => auth()->factory()->getTTL() * 60 ), ]); } }