diff --git a/src/features/auth/api/profileApi.ts b/src/features/auth/api/profileApi.ts index fc947dc..23e7177 100644 --- a/src/features/auth/api/profileApi.ts +++ b/src/features/auth/api/profileApi.ts @@ -1,17 +1,33 @@ import { getCsrfToken } from '@shared/api/csrf' -import { tokenStore } from '@shared/api/tokenStore' const USERS_API_URL = 'https://app.users.elcsa.ru' -export async function getMe(): Promise { +export interface MeResponse { + id: string + email: string + first_name: string + middle_name: string + last_name: string + birth_date: string + crypto_wallet: string | null + phone: string + passport_data: string | null + inn: string | null + erc20: string | null + kyc_verified: boolean + is_deleted: boolean + created_at: string + updated_at: string + kyc_verified_at: string | null +} + +export async function getMe(): Promise { const csrf = await getCsrfToken() - const bearer = tokenStore.get() const res = await fetch(`${USERS_API_URL}/me/`, { credentials: 'include', headers: { 'X-CSRF-Token': csrf, - ...(bearer ? { Authorization: `Bearer ${bearer}` } : {}), }, }) diff --git a/src/features/auth/api/registrationApi.ts b/src/features/auth/api/registrationApi.ts index 3bf1721..eea6d1a 100644 --- a/src/features/auth/api/registrationApi.ts +++ b/src/features/auth/api/registrationApi.ts @@ -44,5 +44,5 @@ export function loginComplete(payload: LoginCompletePayload): Promise { const csrfToken = await getCsrfToken() - return api.post('/logout', { _csrf: csrfToken }) + return api.post('/auth/logout', { _csrf: csrfToken }) } diff --git a/src/features/auth/hooks/useMe.ts b/src/features/auth/hooks/useMe.ts new file mode 100644 index 0000000..fe73361 --- /dev/null +++ b/src/features/auth/hooks/useMe.ts @@ -0,0 +1,13 @@ +import { useQuery } from '@tanstack/react-query' +import { getMe } from '../api/profileApi' +import type { MeResponse } from '../api/profileApi' + +export function useMe() { + return useQuery({ + queryKey: ['me'], + queryFn: getMe, + staleTime: Infinity, + gcTime: Infinity, + retry: false, + }) +} diff --git a/src/features/auth/index.ts b/src/features/auth/index.ts index d1d2229..4f9ffa5 100644 --- a/src/features/auth/index.ts +++ b/src/features/auth/index.ts @@ -1,5 +1,7 @@ export { registrationStart, registrationComplete, loginStart, loginComplete } from './api/registrationApi' export { getMe } from './api/profileApi' +export type { MeResponse } from './api/profileApi' +export { useMe } from './hooks/useMe' export type { RegistrationStartPayload, RegistrationCompletePayload, LoginStartPayload, LoginCompletePayload, AuthResponse } from './api/registrationApi' export { useIsAuthenticated } from './hooks/useIsAuthenticated' export { useAuth, AUTH_QUERY_KEY } from './hooks/useAuth' diff --git a/src/pages/profile/ui/ProfilePage.tsx b/src/pages/profile/ui/ProfilePage.tsx index 0b9ffbf..aeda25e 100644 --- a/src/pages/profile/ui/ProfilePage.tsx +++ b/src/pages/profile/ui/ProfilePage.tsx @@ -1,16 +1,15 @@ -import { useEffect } from 'react' -import { getMe } from '@features/auth' +import { useMe } from '@features/auth' import { Button, FormField } from '@shared/ui' import { WalletHeader } from '@widgets/wallet-header' import { ProfileAvatar, ProfileSection } from '@widgets/profile' import styles from './ProfilePage.module.css' export function ProfilePage() { - useEffect(() => { - getMe() - .then((data) => console.log('[/me]', data)) - .catch((err) => console.error('[/me] error:', err)) - }, []) + const { data } = useMe() + + const fullName = data + ? [data.last_name, data.first_name, data.middle_name].filter(Boolean).join(' ') + : '' return (
@@ -19,7 +18,7 @@ export function ProfilePage() {
- Иванов Иван Иванович + {fullName} $245.00 ≈ 22 340,50 ₽
@@ -28,17 +27,17 @@ export function ProfilePage() {
- - - - + + + +
- - + +
@@ -52,7 +51,7 @@ export function ProfilePage() { } >
- +