login page update

This commit is contained in:
Samandar Turgunboyev
2025-12-19 16:32:33 +05:00
parent a5baccf106
commit 45e5aa3af2
4 changed files with 24 additions and 28 deletions

View File

@@ -6,12 +6,8 @@ export interface Loginbody {
}
const AuthApi = {
getMe: async () => {
const data = await httpClient.get('auth/me/');
return data.data;
},
login: async (body: Loginbody) => {
const data = await httpClient.post('auth/token/', body);
const data = await httpClient.post('auth/admin/token/', body);
return data.data;
},
};

View File

@@ -28,8 +28,7 @@ httpClient.interceptors.response.use(
(response) => response,
async (error) => {
if (error.response?.status === 401) {
Cookies.remove('panda_admin'); // tokenni tozalash
window.location.href = '/login'; // login pagega redirect
Cookies.remove('panda_admin');
}
return Promise.reject(error);

View File

@@ -0,0 +1,6 @@
const onlyNumber = (digits: string | number) => {
const phone = digits.toString();
return phone.replace(/\D/g, "");
};
export default onlyNumber;

View File

@@ -1,6 +1,8 @@
'use client';
import AuthApi, { type Loginbody } from '@/shared/config/api/auth/authApi';
import formatPhone from '@/shared/lib/formatPhone';
import onlyNumber from '@/shared/lib/onlyNumber';
import { Button } from '@/shared/ui/button';
import {
Form,
@@ -12,8 +14,8 @@ import {
import { Input } from '@/shared/ui/input';
import { zodResolver } from '@hookform/resolvers/zod';
import { useMutation } from '@tanstack/react-query';
import type { AxiosError } from 'axios';
import Cookies from 'js-cookie';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { toast } from 'sonner';
@@ -21,45 +23,37 @@ import type z from 'zod';
import { formSchema } from '../lib/form';
const Login = () => {
const [error, setError] = useState<string>();
const navigation = useNavigate();
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
password: '',
phone: '',
phone: '+998',
},
});
const { mutate: login } = useMutation({
mutationFn: (data: Loginbody) => AuthApi.login(data),
onSuccess: async (res) => {
console.log(res);
Cookies.set('panda_admin', res.data.access, {
expires: 1,
sameSite: 'Strict',
});
try {
const me = await AuthApi.getMe();
console.log('Current user:', me);
if (
// me.data.role == 'ADMIN'
me.data.id == 1
) {
navigation('/');
} else {
toast.error("Kirishga ruxsat yo'q");
}
} catch (err) {
console.error('GetMe error:', err);
}
navigation('/');
},
onError: (err: AxiosError) => {
const errorMessage =
(err.response?.data as { message: string }).message ||
'Login yoki parol xato kiritildi';
toast.error(errorMessage, { richColors: true, position: 'top-center' });
},
});
const onSubmit = (values: z.infer<typeof formSchema>) => {
login(values);
login({
password: values.password,
phone: onlyNumber(values.phone),
});
};
return (
@@ -80,6 +74,7 @@ const Login = () => {
<Input
placeholder="phone"
{...field}
value={formatPhone(field.value)}
className="h-[50px] !text-lg !border-none !outline-none focus:!outline-none focus:!ring-0 !bg-background"
/>
</FormControl>