login page update
This commit is contained in:
@@ -6,12 +6,8 @@ export interface Loginbody {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const AuthApi = {
|
const AuthApi = {
|
||||||
getMe: async () => {
|
|
||||||
const data = await httpClient.get('auth/me/');
|
|
||||||
return data.data;
|
|
||||||
},
|
|
||||||
login: async (body: Loginbody) => {
|
login: async (body: Loginbody) => {
|
||||||
const data = await httpClient.post('auth/token/', body);
|
const data = await httpClient.post('auth/admin/token/', body);
|
||||||
return data.data;
|
return data.data;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -28,8 +28,7 @@ httpClient.interceptors.response.use(
|
|||||||
(response) => response,
|
(response) => response,
|
||||||
async (error) => {
|
async (error) => {
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
Cookies.remove('panda_admin'); // tokenni tozalash
|
Cookies.remove('panda_admin');
|
||||||
window.location.href = '/login'; // login pagega redirect
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
|
|||||||
6
src/shared/lib/onlyNumber.ts
Normal file
6
src/shared/lib/onlyNumber.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
const onlyNumber = (digits: string | number) => {
|
||||||
|
const phone = digits.toString();
|
||||||
|
return phone.replace(/\D/g, "");
|
||||||
|
};
|
||||||
|
|
||||||
|
export default onlyNumber;
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import AuthApi, { type Loginbody } from '@/shared/config/api/auth/authApi';
|
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 { Button } from '@/shared/ui/button';
|
||||||
import {
|
import {
|
||||||
Form,
|
Form,
|
||||||
@@ -12,8 +14,8 @@ import {
|
|||||||
import { Input } from '@/shared/ui/input';
|
import { Input } from '@/shared/ui/input';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { useMutation } from '@tanstack/react-query';
|
import { useMutation } from '@tanstack/react-query';
|
||||||
|
import type { AxiosError } from 'axios';
|
||||||
import Cookies from 'js-cookie';
|
import Cookies from 'js-cookie';
|
||||||
import { useState } from 'react';
|
|
||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
@@ -21,45 +23,37 @@ import type z from 'zod';
|
|||||||
import { formSchema } from '../lib/form';
|
import { formSchema } from '../lib/form';
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
const [error, setError] = useState<string>();
|
|
||||||
const navigation = useNavigate();
|
const navigation = useNavigate();
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
const form = useForm<z.infer<typeof formSchema>>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
password: '',
|
password: '',
|
||||||
phone: '',
|
phone: '+998',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const { mutate: login } = useMutation({
|
const { mutate: login } = useMutation({
|
||||||
mutationFn: (data: Loginbody) => AuthApi.login(data),
|
mutationFn: (data: Loginbody) => AuthApi.login(data),
|
||||||
onSuccess: async (res) => {
|
onSuccess: async (res) => {
|
||||||
console.log(res);
|
|
||||||
|
|
||||||
Cookies.set('panda_admin', res.data.access, {
|
Cookies.set('panda_admin', res.data.access, {
|
||||||
expires: 1,
|
expires: 1,
|
||||||
sameSite: 'Strict',
|
sameSite: 'Strict',
|
||||||
});
|
});
|
||||||
|
navigation('/');
|
||||||
try {
|
},
|
||||||
const me = await AuthApi.getMe();
|
onError: (err: AxiosError) => {
|
||||||
console.log('Current user:', me);
|
const errorMessage =
|
||||||
if (
|
(err.response?.data as { message: string }).message ||
|
||||||
// me.data.role == 'ADMIN'
|
'Login yoki parol xato kiritildi';
|
||||||
me.data.id == 1
|
toast.error(errorMessage, { richColors: true, position: 'top-center' });
|
||||||
) {
|
|
||||||
navigation('/');
|
|
||||||
} else {
|
|
||||||
toast.error("Kirishga ruxsat yo'q");
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('GetMe error:', err);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit = (values: z.infer<typeof formSchema>) => {
|
const onSubmit = (values: z.infer<typeof formSchema>) => {
|
||||||
login(values);
|
login({
|
||||||
|
password: values.password,
|
||||||
|
phone: onlyNumber(values.phone),
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -80,6 +74,7 @@ const Login = () => {
|
|||||||
<Input
|
<Input
|
||||||
placeholder="phone"
|
placeholder="phone"
|
||||||
{...field}
|
{...field}
|
||||||
|
value={formatPhone(field.value)}
|
||||||
className="h-[50px] !text-lg !border-none !outline-none focus:!outline-none focus:!ring-0 !bg-background"
|
className="h-[50px] !text-lg !border-none !outline-none focus:!outline-none focus:!ring-0 !bg-background"
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
|
|||||||
Reference in New Issue
Block a user