Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.8 KiB

2 years ago
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
2 years ago
use App\Models\User;
use App\Models\Role;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Password;
use Illuminate\Validation\ValidationException;
11 months ago
2 years ago
class AuthController extends Controller
{
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
public function login(Request $request)
{
$username = $request->username;
$password = $request->password;
$remember = $request->remember;
$is_mobile = $request->is_mobile;
11 months ago
if (empty($username) || empty($password))
return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400);
$usernameCheck = false;
$passwordCheck = false;
11 months ago
if (User::where('username', $username)->exists())
$usernameCheck = true;
11 months ago
if (User::where('password', md5($password))->exists())
$passwordCheck = true;
11 months ago
if ($usernameCheck & $passwordCheck) {
$user = User::where('username', $username)->where('password', md5($password))->first();
11 months ago
if ($is_mobile) {
$fcm_token = $request->fcm_token;
11 months ago
if (!$fcm_token || $fcm_token == "")
return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400);
$dataUpdateFcm = array(
11 months ago
"fcm_token" => $fcm_token
);
$hr = User::find($user->id);
11 months ago
if ($hr)
$hr->update($dataUpdateFcm);
}
$dataRole = Role::find($user->role_id);
11 months ago
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id);
11 months ago
if ($dataRole)
$user->role = $dataRole;
11 months ago
if ($dataHierarchy)
$user->hierarchy = $dataHierarchy;
if (!$token = Auth::login($user))
return response()->json(['error' => 'Unauthorized'], 401);
$ttl = 60;
11 months ago
if ($remember)
$ttl = 10080;
// todo : change existing md5 hashed function to laravel's originally bcrypt
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */
return response()->json([
'code' => 200,
'data' => array(
'data_user' => $user,
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * $ttl,
),
]);
11 months ago
} else {
if (!$usernameCheck && !$passwordCheck)
return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201);
if (!$passwordCheck)
return response()->json(['code' => 201, 'message' => "password doesn't match"], 201);
if (!$usernameCheck)
return response()->json(['code' => 201, 'message' => "username doesn't match"], 201);
11 months ago
}
}
2 years ago
}