|
|
@@ -12,6 +12,7 @@ use App\Models\User;
|
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Hash;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
@@ -23,17 +24,14 @@ class AuthService
|
|
|
|
|
|
public function login(string $email, string $password): ?array
|
|
|
{
|
|
|
- $user = User::where('email', $email)->first();
|
|
|
-
|
|
|
- if (! $user || ! in_array($user->type, [UserTypeEnum::ADMIN, UserTypeEnum::USER])) {
|
|
|
- return null;
|
|
|
- }
|
|
|
+ $user = User::where('email', $email)
|
|
|
+ ->whereIn('type', [UserTypeEnum::ADMIN->value, UserTypeEnum::USER->value])
|
|
|
+ ->first();
|
|
|
|
|
|
- if (! Auth::attempt(['email' => $email, 'password' => $password])) {
|
|
|
+ if (! $user || ! $user->password || ! Hash::check($password, $user->password)) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- // $user = User::where('email', $email)->first();
|
|
|
$deviceId = Str::uuid()->toString();
|
|
|
|
|
|
$accessToken = $user->createAccessToken($deviceId);
|
|
|
@@ -140,17 +138,12 @@ class AuthService
|
|
|
$q->where('phone', $data['phone']);
|
|
|
});
|
|
|
})
|
|
|
+ ->where('type', UserTypeEnum::CLIENT->value)
|
|
|
->first();
|
|
|
|
|
|
$isLogin = false;
|
|
|
|
|
|
if ($user) {
|
|
|
- if ($user->type->value !== UserTypeEnum::CLIENT->value) {
|
|
|
- DB::rollBack();
|
|
|
-
|
|
|
- return ['error' => 'wrong_user_type'];
|
|
|
- }
|
|
|
-
|
|
|
$user->code = $code;
|
|
|
|
|
|
$user->validated_code = false;
|
|
|
@@ -218,15 +211,10 @@ class AuthService
|
|
|
$q->where('phone', $data['phone']);
|
|
|
});
|
|
|
})
|
|
|
+ ->where('type', UserTypeEnum::PROVIDER->value)
|
|
|
->first();
|
|
|
$isLogin = false;
|
|
|
if ($user) {
|
|
|
- if ($user->type->value !== UserTypeEnum::PROVIDER->value) {
|
|
|
- DB::rollBack();
|
|
|
-
|
|
|
- return ['error' => 'wrong_user_type'];
|
|
|
- }
|
|
|
-
|
|
|
// if ($user->registration_complete) {
|
|
|
$provider = Provider::where('user_id', $user->id)->first();
|
|
|
if ($provider && $provider->approval_status->value !== ApprovalStatusEnum::ACCEPTED->value) {
|
|
|
@@ -285,6 +273,7 @@ class AuthService
|
|
|
$query->when($email, fn ($q) => $q->where('email', $email))
|
|
|
->when($phone, fn ($q) => $q->where('phone', $phone));
|
|
|
})
|
|
|
+ ->where('type', UserTypeEnum::CLIENT->value)
|
|
|
->where('code', $code)
|
|
|
->first();
|
|
|
|
|
|
@@ -303,7 +292,7 @@ class AuthService
|
|
|
return ['error' => 'registration_incomplete'];
|
|
|
}
|
|
|
|
|
|
- return $this->loginWithEmail($user->email, $code);
|
|
|
+ return $this->loginWithEmail($user->email, $code, UserTypeEnum::CLIENT->value);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
@@ -319,6 +308,7 @@ class AuthService
|
|
|
$query->when($email, fn ($q) => $q->where('email', $email))
|
|
|
->when($phone, fn ($q) => $q->where('phone', $phone));
|
|
|
})
|
|
|
+ ->where('type', UserTypeEnum::PROVIDER->value)
|
|
|
->where('code', $code)
|
|
|
->first();
|
|
|
|
|
|
@@ -342,7 +332,7 @@ class AuthService
|
|
|
return ['error' => 'provider_rejected'];
|
|
|
}
|
|
|
|
|
|
- return $this->loginWithEmail($user->email, $code);
|
|
|
+ return $this->loginWithEmail($user->email, $code, UserTypeEnum::PROVIDER->value);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
@@ -370,7 +360,7 @@ class AuthService
|
|
|
}
|
|
|
|
|
|
if ($isLogin) {
|
|
|
- $resultLogin = $this->loginWithEmail($user->email, $code);
|
|
|
+ $resultLogin = $this->loginWithEmail($user->email, $code, $user->type->value);
|
|
|
|
|
|
return $resultLogin;
|
|
|
}
|
|
|
@@ -378,10 +368,11 @@ class AuthService
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- public function loginWithEmail(string $email, string $code): ?array
|
|
|
+ public function loginWithEmail(string $email, string $code, ?string $type = null): ?array
|
|
|
{
|
|
|
$user = User::where('email', $email)
|
|
|
->where('code', $code)
|
|
|
+ ->when($type, fn ($q) => $q->where('type', $type))
|
|
|
->first();
|
|
|
|
|
|
if (! $user) {
|