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
+22 -15
View File
@@ -1,32 +1,39 @@
import { useCallback } from 'react';
import { sessionStorageGet, sessionStorageDelete } from '@/utils/sessionStorage';
import { useCallback, useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import { useLocale } from 'next-intl';
import { COOKIE_NAMES } from '@/lib/cookies';
import { deleteClientCookie, getClientCookie } from '@/lib/cookies/client';
import { ROUTES } from '@/constants';
import { useAppStore } from '../store';
/**
* Hook to detect is current user authenticated or not
* @returns {boolean} true if user is authenticated, false otherwise
* Returns true when an access_token cookie is present on the client.
* Initialises as false (SSR-safe) and updates after mount to avoid hydration mismatches.
*/
export function useIsAuthenticated() {
const [state] = useAppStore();
let result = state.isAuthenticated;
const [isAuthenticated, setIsAuthenticated] = useState(false);
// TODO: AUTH: replace next line with access token verification
result = Boolean(sessionStorageGet('access_token', ''));
useEffect(() => {
setIsAuthenticated(Boolean(getClientCookie(COOKIE_NAMES.ACCESS_TOKEN)));
}, []);
return result;
return isAuthenticated;
}
/**
* Returns event handler to Logout current user
* @returns {function} calling this event logs out current user
* Returns a handler that logs the current user out:
* clears auth cookies, resets AppStore, and redirects to the login page.
*/
export function useEventLogout() {
const [, dispatch] = useAppStore();
const router = useRouter();
const locale = useLocale();
return useCallback(() => {
// TODO: AUTH: replace next line with access token saving
sessionStorageDelete('access_token');
deleteClientCookie(COOKIE_NAMES.ACCESS_TOKEN);
deleteClientCookie(COOKIE_NAMES.REFRESH_TOKEN);
dispatch({ type: 'LOG_OUT' });
}, [dispatch]);
router.replace(`/${locale}${ROUTES.LOGIN}`);
}, [dispatch, router, locale]);
}