Browse Source

hierarcy

pull/3/head
farhantock 10 months ago
parent
commit
aae342f731
  1. 29
      app/Http/Controllers/AuthController.php

29
app/Http/Controllers/AuthController.php

@ -11,6 +11,7 @@ use App\Models\Role;
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;
class AuthController extends Controller class AuthController extends Controller
{ {
public function __construct() public function __construct()
@ -25,8 +26,8 @@ class AuthController extends Controller
$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;
@ -37,34 +38,38 @@ class AuthController extends Controller
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);
if($dataRole) if ($dataRole)
$user->role = $dataRole; $user->role = $dataRole;
if (! $token =Auth::login($user)) if ($dataHierarchy)
$user->hierarchy = $dataHierarchy;
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
@ -80,7 +85,7 @@ class AuthController extends Controller
'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)

Loading…
Cancel
Save