module shop client
This commit is contained in:
@ -94,7 +94,7 @@ class SecureRestController extends RestController
|
||||
}
|
||||
|
||||
$res = [
|
||||
"code" => $secretCode->code,
|
||||
"status" => "success",
|
||||
"code_expires_at" => $secretCode->code_expires_at,
|
||||
];
|
||||
|
||||
@ -105,23 +105,26 @@ class SecureRestController extends RestController
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionCodeCheck(): void
|
||||
#[NoReturn] public function actionCodeCheck(): void
|
||||
{
|
||||
$request = new Request();
|
||||
$code = $request->post("code");
|
||||
|
||||
if (isset($_COOKIE['user_email'])) {
|
||||
$user = User::where('email', $_COOKIE["user_email"])->first();
|
||||
if (!$user) {
|
||||
throw new exception("User not found.");
|
||||
}
|
||||
$code = $request->post("code");
|
||||
$secretCode = SecureService::getByField("user_id", $user->id);
|
||||
if ($secretCode->code == $code && time() <= strtotime($secretCode->code_expires_at)) {
|
||||
setcookie('user_id', $user->id, time() + 60 * 60 * 24, '/', $_SERVER['SERVER_NAME'], false);
|
||||
} else {
|
||||
throw new exception("incorrect code");
|
||||
$model = SecretCode::where("code", $code)->first();
|
||||
if (time() <= strtotime($model->code_expires_at)) {
|
||||
$user = $this->model->where("id", $model->user_id)->first();
|
||||
if ($user){
|
||||
$user->access_token_expires_at = date("Y-m-d H:i:s", strtotime(App::$secure['token_expired_time']));
|
||||
$user->access_token = SecureService::generateAccessToken();
|
||||
$user->save();
|
||||
$this->renderApi([
|
||||
"access_token" => $user->access_token,
|
||||
"access_token_expires_at" => $user->access_token_expires_at,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->renderApi(['status' => 'error', 'message' => 'incorrect code']);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user