|
@@ -109,19 +109,37 @@ class AuthService
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function sendCode(array $data): void
|
|
|
|
|
|
|
+ public function sendCode(array $data): ?bool
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
|
$code = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
|
$code = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
|
|
-
|
|
|
|
|
- $user = new User();
|
|
|
|
|
- $user->fill($data);
|
|
|
|
|
- $user->code = $code;
|
|
|
|
|
- $user->name = $data['name'] ?? 'Usuário';
|
|
|
|
|
- $user->type = $data['type'] ?? 'USER';
|
|
|
|
|
- $user->save();
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $user = User::where(function ($query) use ($data) {
|
|
|
|
|
+ $query->when(!empty($data['email']), function ($q) use ($data) {
|
|
|
|
|
+ $q->where('email', $data['email']);
|
|
|
|
|
+ })
|
|
|
|
|
+ ->when(!empty($data['phone']), function ($q) use ($data) {
|
|
|
|
|
+ $q->where('phone', $data['phone']);
|
|
|
|
|
+ });
|
|
|
|
|
+ })
|
|
|
|
|
+ ->first();
|
|
|
|
|
+
|
|
|
|
|
+ $isLogin = false;
|
|
|
|
|
+ if ($user) {
|
|
|
|
|
+ $user->code = $code;
|
|
|
|
|
+ $user->validated_code = false;
|
|
|
|
|
+ $user->save();
|
|
|
|
|
+ $isLogin = true;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $user = new User();
|
|
|
|
|
+ $user->fill($data);
|
|
|
|
|
+ $user->code = $code;
|
|
|
|
|
+ $user->name = $data['name'] ?? 'Usuário';
|
|
|
|
|
+ $user->type = $data['type'] ?? 'USER';
|
|
|
|
|
+ $user->save();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (!empty($data['email'])) {
|
|
if (!empty($data['email'])) {
|
|
|
$this->emailService->sendVerificationCode(
|
|
$this->emailService->sendVerificationCode(
|
|
|
email: $data['email'],
|
|
email: $data['email'],
|
|
@@ -133,43 +151,44 @@ class AuthService
|
|
|
'phone' => $data['phone'],
|
|
'phone' => $data['phone'],
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
DB::commit();
|
|
DB::commit();
|
|
|
- return;
|
|
|
|
|
|
|
+ return $isLogin;
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
DB::rollBack();
|
|
DB::rollBack();
|
|
|
Log::error('Erro ao enviar código de verificação.', [
|
|
Log::error('Erro ao enviar código de verificação.', [
|
|
|
'error' => $e->getMessage(),
|
|
'error' => $e->getMessage(),
|
|
|
'data' => $data,
|
|
'data' => $data,
|
|
|
]);
|
|
]);
|
|
|
- return;
|
|
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function validateCode(array $data): Bool
|
|
|
|
|
|
|
+ public function validateCode(array $data, bool $isLogin): bool|array
|
|
|
{
|
|
{
|
|
|
$email = $data['email'] ?? null;
|
|
$email = $data['email'] ?? null;
|
|
|
$phone = $data['phone'] ?? null;
|
|
$phone = $data['phone'] ?? null;
|
|
|
$code = $data['code'] ?? '';
|
|
$code = $data['code'] ?? '';
|
|
|
|
|
|
|
|
$user = User::where(function ($query) use ($email, $phone) {
|
|
$user = User::where(function ($query) use ($email, $phone) {
|
|
|
- $query->when($email, function ($q) use ($email) {
|
|
|
|
|
- $q->where('email', $email);
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ $query->when($email, function ($q) use ($email) {
|
|
|
|
|
+ $q->where('email', $email);
|
|
|
|
|
+ })
|
|
|
->when($phone, function ($q) use ($phone) {
|
|
->when($phone, function ($q) use ($phone) {
|
|
|
$q->where('phone', $phone);
|
|
$q->where('phone', $phone);
|
|
|
});
|
|
});
|
|
|
- })
|
|
|
|
|
|
|
+ })
|
|
|
->where('code', $code)
|
|
->where('code', $code)
|
|
|
->first();
|
|
->first();
|
|
|
- Log::info($user);
|
|
|
|
|
|
|
+
|
|
|
if (!$user) {
|
|
if (!$user) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // $user->code = null;
|
|
|
|
|
- // $user->validated_code = true;
|
|
|
|
|
- // $user->save();
|
|
|
|
|
|
|
+ if($isLogin) {
|
|
|
|
|
+ $resultLogin = $this->loginWithEmail($user->email, $code);
|
|
|
|
|
+ return $resultLogin;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|