Skip to content

Instantly share code, notes, and snippets.

@ayatmaulana
Created November 24, 2017 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayatmaulana/f42c13ef84c5736ea6d3f74209b50716 to your computer and use it in GitHub Desktop.
Save ayatmaulana/f42c13ef84c5736ea6d3f74209b50716 to your computer and use it in GitHub Desktop.
<?php
function login(Request $req, $is_admin = 0){
$cred = $req->email_or_username;
if($is_admin == 1){
$user = User::where('userlevel', 5)->where('email', $cred)->orWhere('username', $cred)->first();
}
else {
$user = User::where('userlevel', '<>', 5)->where('email', $cred)->orWhere('username', $cred)->first();
}
if(empty($user)) {
// save log
$this->save_log(0, $this->__gs->__current_route, '__m_login_user_notfound');
return $this->__gs->response_builder(NULL, '__m_login_user_notfound');
}
else if($user->user_status == 0) {
// save log
$this->save_log(0, $this->__gs->__current_route, '__m_user_not_active');
return $this->__gs->response_builder(NULL, '__m_user_not_active');
}
else {
$pass_check = Hash::check($req->password, $user->password);
if($pass_check == false) {
// save log
$this->save_log(0, $this->__gs->__current_route, '__m_login_invalid_pass');
return $this->__gs->response_builder(NULL, '__m_login_invalid_pass');
}
else {
$device_type = (new ApiKey)->get_device_type_from_key($req->header('apikey'));
// if login from mobile device, invalidate all other mobile device token
// update UserDevices data as invalid
if($device_type == "IOS" || $device_type == "ANDROID") {
UserDevices::whereIn('devicetype', array('IOS','ANDROID'))
->where('user_id', $user->id)
->where('remark','')
->update(['remark'=>'forced_logout']);
//INVALIDATE ALL FCM TOKEN ASSOCIATED WITH THIS USER
PushNotifToken::where("user_email",$user->email)->update(['user_email'=>'']);
// also, we need to delete related oauth_access_tokens data
OauthAccessTokens::where('user_id', $user->id)
->whereIn('name', array('token-IOS','token-ANDROID'))->delete();
}
$userDevice = UserDevices::where('devicetype', $device_type)
->where('user_id', $user->id)
->where('remark', '')
->orderBy('created_at','desc')
->first();
//->value('accesstoken');
if(empty($userDevice)) {
// step 1 : implement passport when valid token not found on DB
$token = $user->createToken('token-'.$device_type)->accessToken;
// step 2 : save log into UserDevices
$ud = new UserDevices;
$ud->devicetype = $device_type;
$ud->deviceid = json_encode(
array("ip" => $_SERVER['REMOTE_ADDR'], "agent" => $_SERVER['HTTP_USER_AGENT'])
);
$ud->accesstoken = $token;
$ud->user_id = $user->id;
$ud->created_by = $user->id;
$ud->created_at = date("Y-m-d H:i:s");
$ud->save();
} else {
//kalo token ada , cek juga di table passport
$oauthTokenInfo = OauthAccessTokens::where('user_id', $user->id)->where('name', 'token-'.$device_type)->first();
if(empty($oauthTokenInfo)) {
$token = $user->createToken('token-'.$device_type)->accessToken;
$userDevice->accesstoken = $token;
$userDevice->save();
} else {
$token = $userDevice->accesstoken;
}
}
$uc = UserCompanies::where('user_id', $user->id)->first();
//get forum url for this user
// try
// {
// $client = new Client(); //GuzzleHttp\Client
//
// $endpoint = env('FORUM_URL') . "/custom/vbapiafi.php";
// $postParam = array(
// "act" => "check",
// "email" => $user->email,
// "userlevel" => $user->userlevel,
// "username" => $user->username
// );
//
// $response = $client->request('POST',
// $endpoint,
// [
// 'form_params' => $postParam,
// 'headers' => ['apikey' => env('FORUM_APIKEY')]
// ]);
//
//
// $results = $response->getBody();
// $results = json_decode($results, true);
// $forumurl = $results["forumurl"];
// } catch (\Exception $ex) {
// $forumurl = "";
// }
// generate forum url from .env
$forumurl = 't/'.$token;
// save log
$this->save_log($user->id, $this->__gs->__current_route, '__m_login_success');
$data = array_merge(json_decode(json_encode($user), TRUE),
array(
"token" => $token,
"my_provinsi"=>Province::where("id", $user['prov_id'])->select("id","name")->first(),
"my_kabupaten"=>Regency::where("id", $user['kab_id'])->select("id","name")->first(),
"my_kecamatan"=>District::where("id", $user['kec_id'])->select("id","name")->first(),
"my_company"=>$uc,
"upload_path"=>$this->__gs->get_upload_path(),
"forumurl" => $forumurl
)
);
return $this->__gs->response_builder($data, '__m_login_success');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment