Browse Source

Merge branch 'master' of https://git.oslog.id/ordo/adw-backend

pull/3/head
ardhi 2 years ago
parent
commit
4e5ed3e131
  1. 107
      app/Http/Controllers/AuthController.php

107
app/Http/Controllers/AuthController.php

@ -8,7 +8,9 @@ use Illuminate\Support\Facades\Hash;
use App\Models\User; use App\Models\User;
use App\Models\Role; use App\Models\Role;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Password;
use Illuminate\Validation\ValidationException;
class AuthController extends Controller class AuthController extends Controller
{ {
public function __construct() public function __construct()
@ -26,48 +28,65 @@ class AuthController extends Controller
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);
$user = User::where('username', $username)->where('password', md5($password))->first(); $usernameCheck = false;
$passwordCheck = false;
if($is_mobile){
$fcm_token = $request->fcm_token; if (User::where('username', $username)->exist())
$usernameCheck = true;
if(!$fcm_token || $fcm_token=="")
return response()->json(['status'=>'error','message'=>'FCM Token is required'], 400); if (User::where('password', md5($password))->exists())
$passwordCheck = true;
$dataUpdateFcm = array(
"fcm_token"=>$fcm_token if ($usernameCheck & $passwordCheck){
); $user = User::where('username', $username)->where('password', md5($password))->first();
if($is_mobile){
$hr = User::find($user->id); $fcm_token = $request->fcm_token;
if($hr) if(!$fcm_token || $fcm_token=="")
$hr->update($dataUpdateFcm); return response()->json(['status'=>'error','message'=>'FCM Token is required'], 400);
}
$dataUpdateFcm = array(
$dataRole = Role::find($user->role_id); "fcm_token"=>$fcm_token
);
if($dataRole)
$user->role = $dataRole; $hr = User::find($user->id);
if (! $token =Auth::login($user)) if($hr)
return response()->json(['error' => 'Unauthorized'], 401); $hr->update($dataUpdateFcm);
}
$ttl = 60;
if($remember) $dataRole = Role::find($user->role_id);
$ttl = 10080;
if($dataRole)
// todo : change existing md5 hashed function to laravel's originally bcrypt $user->role = $dataRole;
/* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */
/* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */ if (! $token =Auth::login($user))
return response()->json(['error' => 'Unauthorized'], 401);
return response()->json([
'code' => 200, $ttl = 60;
'data' => array( if($remember)
'data_user' => $user, $ttl = 10080;
'access_token' => $token,
'token_type' => 'bearer', // todo : change existing md5 hashed function to laravel's originally bcrypt
'expires_in' => auth()->factory()->getTTL() * $ttl, /* $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,
),
]);
}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);
}
} }
} }

Loading…
Cancel
Save