another step constructing base project

This commit is contained in:
hamid
2026-06-18 01:19:23 +03:30
parent 5388bea320
commit e135b0b919
27 changed files with 1022 additions and 355 deletions
@@ -0,0 +1,16 @@
import { clientFetch } from '@/lib/api/client';
import type { AuthTokens, LoginDto, User } from '../types';
export const AuthClientApi = {
login: (dto: LoginDto) =>
clientFetch<AuthTokens>('/auth/login', {
method: 'POST',
body: JSON.stringify(dto),
}),
logout: () =>
clientFetch<void>('/auth/logout', { method: 'POST' }),
getCurrentUser: () =>
clientFetch<User>('/auth/me'),
};
@@ -0,0 +1,11 @@
import { useQuery } from '@tanstack/react-query';
import { AuthClientApi } from '../apis/clientApi';
import { authKeys } from '../keys';
export function useCurrentUser() {
return useQuery({
queryKey: authKeys.currentUser(),
queryFn: () => AuthClientApi.getCurrentUser(),
});
}
@@ -0,0 +1,22 @@
import { useMutation } from '@tanstack/react-query';
import { AUTH_ACCESS_COOKIE_OPTIONS, AUTH_REFRESH_COOKIE_OPTIONS, COOKIE_NAMES } from '@/lib/cookies';
import { setClientCookie } from '@/lib/cookies/client';
import { dispatchToast } from '@/lib/toast/dispatchToast';
import type { ApiError } from '@/lib/api/errors';
import { AuthClientApi } from '../apis/clientApi';
import type { LoginDto } from '../types';
export function useLogin() {
return useMutation({
mutationFn: (dto: LoginDto) => AuthClientApi.login(dto),
onSuccess: (data) => {
setClientCookie(COOKIE_NAMES.ACCESS_TOKEN, data.accessToken, AUTH_ACCESS_COOKIE_OPTIONS);
setClientCookie(COOKIE_NAMES.REFRESH_TOKEN, data.refreshToken, AUTH_REFRESH_COOKIE_OPTIONS);
},
onError: (error: ApiError) => {
dispatchToast(error.message, 'error');
},
});
}
@@ -0,0 +1,26 @@
import { useMutation } from '@tanstack/react-query';
import { useRouter } from 'next/navigation';
import { useLocale } from 'next-intl';
import { COOKIE_NAMES } from '@/lib/cookies';
import { deleteClientCookie } from '@/lib/cookies/client';
import { ROUTES } from '@/constants';
import { useAppStore } from '@/store';
import { AuthClientApi } from '../apis/clientApi';
export function useLogout() {
const [, dispatch] = useAppStore();
const router = useRouter();
const locale = useLocale();
return useMutation({
mutationFn: () => AuthClientApi.logout(),
onSettled: () => {
deleteClientCookie(COOKIE_NAMES.ACCESS_TOKEN);
deleteClientCookie(COOKIE_NAMES.REFRESH_TOKEN);
dispatch({ type: 'LOG_OUT' });
router.replace(`/${locale}${ROUTES.LOGIN}`);
},
});
}
+3
View File
@@ -0,0 +1,3 @@
export { useLogin } from './hooks/useLogin';
export { useLogout } from './hooks/useLogout';
export { useCurrentUser } from './hooks/useCurrentUser';
+4
View File
@@ -0,0 +1,4 @@
export const authKeys = {
all: ['auth'] as const,
currentUser: () => [...authKeys.all, 'me'] as const,
};
+14
View File
@@ -0,0 +1,14 @@
export interface LoginDto {
username: string;
password: string;
}
export interface AuthTokens {
accessToken: string;
refreshToken: string;
}
export interface User {
id: string;
username: string;
}