Browse Source

implement remember me

pull/3/head
Muhammad Sulaiman Yusuf 2 years ago
parent
commit
dbc5421318
  1. 129
      app/Http/Controllers/AuthController.php

129
app/Http/Controllers/AuthController.php

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

Loading…
Cancel
Save