refactor: standardize import quotes and improve cookie management in auth storage

This commit is contained in:
Estifo77
2026-07-22 14:46:38 +03:00
parent a76549b018
commit 7191bc6791
3 changed files with 109 additions and 73 deletions

View File

@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState } from "react";
import {
Alert,
Anchor,
@@ -11,29 +11,35 @@ import {
Text,
TextInput,
Title,
} from '@mantine/core';
} from "@mantine/core";
import {
IconArrowRight,
IconDeviceMobile,
IconLock,
IconMail,
} from '@tabler/icons-react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { useNavigate, Link } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { useApiMutation } from '@ema-platform/api';
import { notify } from '@ema-platform/ui';
import { AuthShell } from '../components/AuthShell';
import { loginSuccess, setUser, setCurrentProfile } from '../store/auth.slice';
import type { LoginPayload, AuthUser, CurrentProfile } from '../types/auth.types';
import { useAuthConfig } from '../AuthConfig';
import { authStorage } from '../utils/auth-storage';
} from "@tabler/icons-react";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { useNavigate, Link } from "react-router-dom";
import { useDispatch } from "react-redux";
import { useApiMutation } from "@ema-platform/api";
import { notify } from "@ema-platform/ui";
import { AuthShell } from "../components/AuthShell";
import { loginSuccess, setUser, setCurrentProfile } from "../store/auth.slice";
import type {
LoginPayload,
AuthUser,
CurrentProfile,
} from "../types/auth.types";
import { useAuthConfig } from "../AuthConfig";
import { authStorage } from "../utils/auth-storage";
const schema = z.object({
email: z.string().email({ message: 'Enter a valid email' }),
password: z.string().min(5, { message: 'Password must be at least 6 characters' }),
email: z.string().email({ message: "Enter a valid email" }),
password: z
.string()
.min(5, { message: "Password must be at least 6 characters" }),
});
type FormValues = z.infer<typeof schema>;
@@ -48,7 +54,10 @@ export function LoginPage() {
const [serverError, setServerError] = useState<string | null>(null);
const [loginTrigger] = useApiMutation<LoginPayload>();
const [meTrigger] = useApiMutation<AuthUser>();
const [profileCheckTrigger] = useApiMutation<{ total: number; items: CurrentProfile[] }>();
const [profileCheckTrigger] = useApiMutation<{
total: number;
items: CurrentProfile[];
}>();
const {
register,
@@ -62,15 +71,15 @@ export function LoginPage() {
setIsLoading(true);
try {
const data = await loginTrigger({
url: '/auth/login',
method: 'POST',
url: "/auth/login",
method: "POST",
body: values,
}).unwrap();
dispatch(loginSuccess(data));
const me = await meTrigger({
url: '/auth/me',
method: 'GET',
url: "/auth/me",
method: "GET",
}).unwrap();
dispatch(setUser(me));
@@ -79,7 +88,7 @@ export function LoginPage() {
const q = `w=user_id:=:${me.id}&i=user,address,profession`;
const result = await profileCheckTrigger({
url: `/profiles?q=${encodeURIComponent(q)}`,
method: 'GET',
method: "GET",
}).unwrap();
if (result.total > 0 && result.items.length > 0) {
const profile = result.items[0];
@@ -92,7 +101,7 @@ export function LoginPage() {
}
if (!me.isPhoneNumberVerified) {
navigate('/otp-verify', {
navigate("/otp-verify", {
state: {
email: me.email,
phoneNumber: me.phoneNumber,
@@ -103,7 +112,7 @@ export function LoginPage() {
}
if (!hasProfile) {
navigate('/profile-setup');
navigate("/profile-setup");
return;
}
@@ -111,10 +120,11 @@ export function LoginPage() {
} catch (err: unknown) {
const msg =
(err as { data?: { message?: string } })?.data?.message ??
(err instanceof Error ? err.message : 'Something went wrong');
(err instanceof Error ? err.message : "Something went wrong");
setServerError(msg);
notify.error(msg);
} finally {
localStorage.setItem("rememberMe", String(rememberMe));
setIsLoading(false);
}
};
@@ -132,7 +142,12 @@ export function LoginPage() {
</div>
{serverError && (
<Alert variant="light" color="red" withCloseButton onClose={() => setServerError(null)}>
<Alert
variant="light"
color="red"
withCloseButton
onClose={() => setServerError(null)}
>
{serverError}
</Alert>
)}
@@ -145,7 +160,7 @@ export function LoginPage() {
size="md"
leftSection={<IconMail size={18} />}
error={errors.email?.message}
{...register('email')}
{...register("email")}
/>
<PasswordInput
label="Password"
@@ -153,7 +168,7 @@ export function LoginPage() {
size="md"
leftSection={<IconLock size={18} />}
error={errors.password?.message}
{...register('password')}
{...register("password")}
/>
<Group justify="space-between">
@@ -194,14 +209,14 @@ export function LoginPage() {
fullWidth
size="md"
leftSection={<IconDeviceMobile size={18} />}
onClick={() => notify.info('Phone sign-in is coming soon.')}
onClick={() => notify.info("Phone sign-in is coming soon.")}
>
Sign in with phone number
</Button>
{enableSignup && (
<Text ta="center" size="sm" c="dimmed">
Don&apos;t have an account?{' '}
Don&apos;t have an account?{" "}
<Anchor component={Link} to="/signup" fw={700}>
Create one
</Anchor>