farhantock
11 months ago
6 changed files with 278 additions and 176 deletions
@ -1,174 +1,174 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
namespace App\Http\Controllers; |
namespace App\Http\Controllers; |
||||||
|
|
||||||
use Illuminate\Support\Facades\Auth; |
use Illuminate\Support\Facades\Auth; |
||||||
use Illuminate\Http\Request; |
use Illuminate\Http\Request; |
||||||
use Illuminate\Support\Facades\Hash; |
use Illuminate\Support\Facades\Hash; |
||||||
|
|
||||||
use App\Models\User; |
use App\Models\User; |
||||||
use App\Models\Role; |
use App\Models\Role; |
||||||
use App\Models\Company; |
use App\Models\Company; |
||||||
use Illuminate\Http\JsonResponse; |
use Illuminate\Http\JsonResponse; |
||||||
use Illuminate\Support\Facades\Password; |
use Illuminate\Support\Facades\Password; |
||||||
use Illuminate\Validation\ValidationException; |
use Illuminate\Validation\ValidationException; |
||||||
|
|
||||||
const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php'; |
const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php'; |
||||||
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; |
||||||
$remember = $request->remember; |
$remember = $request->remember; |
||||||
$is_mobile = $request->is_mobile; |
$is_mobile = $request->is_mobile; |
||||||
|
|
||||||
if (empty($username) || empty($password)) |
if (empty($username) || empty($password)) |
||||||
return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400); |
return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400); |
||||||
|
|
||||||
$usernameCheck = false; |
$usernameCheck = false; |
||||||
$passwordCheck = false; |
$passwordCheck = false; |
||||||
|
|
||||||
if (User::where('username', $username)->exists()) |
if (User::where('username', $username)->exists()) |
||||||
$usernameCheck = true; |
$usernameCheck = true; |
||||||
|
|
||||||
if (User::where('password', md5($password))->exists()) |
if (User::where('password', md5($password))->exists()) |
||||||
$passwordCheck = true; |
$passwordCheck = true; |
||||||
|
|
||||||
if ($usernameCheck & $passwordCheck) { |
if ($usernameCheck & $passwordCheck) { |
||||||
$user = User::where('username', $username)->where('password', md5($password))->first(); |
$user = User::where('username', $username)->where('password', md5($password))->first(); |
||||||
if ($is_mobile) { |
if ($is_mobile) { |
||||||
$fcm_token = $request->fcm_token; |
$fcm_token = $request->fcm_token; |
||||||
|
|
||||||
if (!$fcm_token || $fcm_token == "") |
if (!$fcm_token || $fcm_token == "") |
||||||
return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400); |
return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400); |
||||||
|
|
||||||
$dataUpdateFcm = array( |
$dataUpdateFcm = array( |
||||||
"fcm_token" => $fcm_token |
"fcm_token" => $fcm_token |
||||||
); |
); |
||||||
|
|
||||||
$hr = User::find($user->id); |
$hr = User::find($user->id); |
||||||
|
|
||||||
if ($hr) |
if ($hr) |
||||||
$hr->update($dataUpdateFcm); |
$hr->update($dataUpdateFcm); |
||||||
} |
} |
||||||
|
|
||||||
$dataRole = Role::find($user->role_id); |
$dataRole = Role::find($user->role_id); |
||||||
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id); |
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id); |
||||||
$configApp = Company::where('id', $user->company_id)->first(); |
$configApp = Company::where('id', $user->company_id)->first(); |
||||||
if ($configApp) { |
if ($configApp) { |
||||||
$logoLogin = json_decode($configApp->logo_login, true); |
$logoLogin = json_decode($configApp->logo_login, true); |
||||||
$favicon = json_decode($configApp->favicon_image, true); |
$favicon = json_decode($configApp->favicon_image, true); |
||||||
$logoHeader = json_decode($configApp->logo_header, true); |
$logoHeader = json_decode($configApp->logo_header, true); |
||||||
$configApp->logo_login = $logoLogin; |
$configApp->logo_login = $logoLogin; |
||||||
$configApp->favicon_image = $favicon; |
$configApp->favicon_image = $favicon; |
||||||
$configApp->logo_header = $logoHeader; |
$configApp->logo_header = $logoHeader; |
||||||
} |
} |
||||||
|
|
||||||
if ($configApp) |
if ($configApp) |
||||||
$user->configApp = $configApp; |
$user->configApp = $configApp; |
||||||
|
|
||||||
if ($dataRole) |
if ($dataRole) |
||||||
$user->role = $dataRole; |
$user->role = $dataRole; |
||||||
|
|
||||||
if ($dataHierarchy) |
if ($dataHierarchy) |
||||||
$user->hierarchy = $dataHierarchy; |
$user->hierarchy = $dataHierarchy; |
||||||
|
|
||||||
if (!$token = Auth::login($user)) |
if (!$token = Auth::login($user)) |
||||||
return response()->json(['error' => 'Unauthorized'], 401); |
return response()->json(['error' => 'Unauthorized'], 401); |
||||||
|
|
||||||
$ttl = 60; |
$ttl = 60; |
||||||
if ($remember) |
if ($remember) |
||||||
$ttl = 10080; |
$ttl = 10080; |
||||||
|
|
||||||
// todo : change existing md5 hashed function to laravel's originally bcrypt |
// todo : change existing md5 hashed function to laravel's originally bcrypt |
||||||
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */ |
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */ |
||||||
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */ |
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */ |
||||||
|
|
||||||
return response()->json([ |
return response()->json([ |
||||||
'code' => 200, |
'code' => 200, |
||||||
'data' => array( |
'data' => array( |
||||||
'data_user' => $user, |
'data_user' => $user, |
||||||
'access_token' => $token, |
'access_token' => $token, |
||||||
'token_type' => 'bearer', |
'token_type' => 'bearer', |
||||||
'expires_in' => auth()->factory()->getTTL() * $ttl, |
'expires_in' => auth()->factory()->getTTL() * $ttl, |
||||||
), |
), |
||||||
]); |
]); |
||||||
} else { |
} else { |
||||||
if (!$usernameCheck && !$passwordCheck) |
if (!$usernameCheck && !$passwordCheck) |
||||||
return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201); |
return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201); |
||||||
if (!$passwordCheck) |
if (!$passwordCheck) |
||||||
return response()->json(['code' => 201, 'message' => "password doesn't match"], 201); |
return response()->json(['code' => 201, 'message' => "password doesn't match"], 201); |
||||||
if (!$usernameCheck) |
if (!$usernameCheck) |
||||||
return response()->json(['code' => 201, 'message' => "username doesn't match"], 201); |
return response()->json(['code' => 201, 'message' => "username doesn't match"], 201); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
|
|
||||||
public function sendEmail(Request $request) |
public function sendEmail(Request $request) |
||||||
{ |
{ |
||||||
$hashed = Str::random(15); |
$hashed = Str::random(15); |
||||||
$email = $request->email; |
$email = $request->email; |
||||||
$user = User::select('email', 'name')->where('email', $email)->first(); |
$user = User::select('email', 'name')->where('email', $email)->first(); |
||||||
|
|
||||||
if (!$user) { |
if (!$user) { |
||||||
return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400); |
return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400); |
||||||
} else { |
} else { |
||||||
$this->reqHttpCurl($email, $hashed, $user->username, $user->name); |
$this->reqHttpCurl($email, $hashed, $user->username, $user->name); |
||||||
// $updateUser = User::where('email', $email)->update(['password'=> $hashed]); |
// $updateUser = User::where('email', $email)->update(['password'=> $hashed]); |
||||||
if (User::where('email', $email)->update(['password' => md5($hashed)])) |
if (User::where('email', $email)->update(['password' => md5($hashed)])) |
||||||
return response()->json(['status' => 'success', 'code' => 200, 'message' => 'Password already sent to mail'], 200); |
return response()->json(['status' => 'success', 'code' => 200, 'message' => 'Password already sent to mail'], 200); |
||||||
|
|
||||||
return response()->json(['status' => 'error', 'code' => 400, 'message' => 'e-mail not found '], 400); |
return response()->json(['status' => 'error', 'code' => 400, 'message' => 'e-mail not found '], 400); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
private function reqHttpCurl($email, $password, $username, $name) |
private function reqHttpCurl($email, $password, $username, $name) |
||||||
{ |
{ |
||||||
$postData = [ |
$postData = [ |
||||||
"to" => $email, |
"to" => $email, |
||||||
"username" => $name, |
"username" => $name, |
||||||
"username" => $username, |
"username" => $username, |
||||||
"password" => $password, |
"password" => $password, |
||||||
"from" => "app.integrasia@integrasiautama.com", |
"from" => "app.integrasia@integrasiautama.com", |
||||||
"alias_from" => "OSPRO", |
"alias_from" => "OSPRO", |
||||||
"subject" => "Registration OSPRO", |
"subject" => "Registration OSPRO", |
||||||
"body" => "registration-ospro" |
"body" => "registration-ospro" |
||||||
]; |
]; |
||||||
|
|
||||||
$curl = curl_init(); |
$curl = curl_init(); |
||||||
|
|
||||||
curl_setopt_array($curl, array( |
curl_setopt_array($curl, array( |
||||||
CURLOPT_URL => URL_EMAIL, // your preferred url |
CURLOPT_URL => URL_EMAIL, // your preferred url |
||||||
CURLOPT_RETURNTRANSFER => true, |
CURLOPT_RETURNTRANSFER => true, |
||||||
CURLOPT_ENCODING => "", |
CURLOPT_ENCODING => "", |
||||||
CURLOPT_MAXREDIRS => 10, |
CURLOPT_MAXREDIRS => 10, |
||||||
CURLOPT_TIMEOUT => 30000, |
CURLOPT_TIMEOUT => 30000, |
||||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||||||
CURLOPT_CUSTOMREQUEST => "POST", |
CURLOPT_CUSTOMREQUEST => "POST", |
||||||
CURLOPT_POSTFIELDS => json_encode($postData), |
CURLOPT_POSTFIELDS => json_encode($postData), |
||||||
CURLOPT_HTTPHEADER => array( |
CURLOPT_HTTPHEADER => array( |
||||||
// Set here requred headers |
// Set here requred headers |
||||||
"accept: */*", |
"accept: */*", |
||||||
"accept-language: en-US,en;q=0.8", |
"accept-language: en-US,en;q=0.8", |
||||||
"content-type: application/json", |
"content-type: application/json", |
||||||
), |
), |
||||||
)); |
)); |
||||||
|
|
||||||
$response = curl_exec($curl); |
$response = curl_exec($curl); |
||||||
$err = curl_error($curl); |
$err = curl_error($curl); |
||||||
|
|
||||||
curl_close($curl); |
curl_close($curl); |
||||||
|
|
||||||
if ($err) { |
if ($err) { |
||||||
echo "cURL Error #:" . $err; |
echo "cURL Error #:" . $err; |
||||||
} else { |
} else { |
||||||
print_r(json_decode($response)); |
print_r(json_decode($response)); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue