fix: authorization / registration
This commit is contained in:
@@ -12,12 +12,25 @@ export interface RegistrationCompletePayload {
|
||||
code: string
|
||||
}
|
||||
|
||||
export function registrationStart(payload: RegistrationStartPayload): Promise<string> {
|
||||
return api.post<string>('/auth/registration/start', payload)
|
||||
export interface LoginPayload {
|
||||
email: string
|
||||
password: string
|
||||
}
|
||||
|
||||
export function registrationComplete(payload: RegistrationCompletePayload): Promise<string> {
|
||||
return api.post<string>('/auth/registration/complete', payload)
|
||||
export interface AuthResponse {
|
||||
access_token: string
|
||||
}
|
||||
|
||||
export function registrationStart(payload: RegistrationStartPayload): Promise<void> {
|
||||
return api.post<void>('/auth/registration/start', payload)
|
||||
}
|
||||
|
||||
export function registrationComplete(payload: RegistrationCompletePayload): Promise<AuthResponse> {
|
||||
return api.post<AuthResponse>('/auth/registration/complete', payload)
|
||||
}
|
||||
|
||||
export function login(payload: LoginPayload): Promise<AuthResponse> {
|
||||
return api.post<AuthResponse>('/auth/login', payload)
|
||||
}
|
||||
|
||||
export async function logout(): Promise<void> {
|
||||
|
||||
13
src/features/auth/hooks/useAuth.ts
Normal file
13
src/features/auth/hooks/useAuth.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { refreshAccessToken } from '@shared/api/tokenStore'
|
||||
|
||||
export const AUTH_QUERY_KEY = ['auth']
|
||||
|
||||
export function useAuth() {
|
||||
return useQuery({
|
||||
queryKey: AUTH_QUERY_KEY,
|
||||
queryFn: refreshAccessToken,
|
||||
retry: false,
|
||||
staleTime: Infinity,
|
||||
})
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
export function useIsAuthenticated(): boolean {
|
||||
return localStorage.getItem('isAuthenticated') === 'true'
|
||||
import { useAuth } from './useAuth'
|
||||
|
||||
export function useIsAuthenticated(): { isAuthenticated: boolean; isLoading: boolean } {
|
||||
const { data, isLoading, isError } = useAuth()
|
||||
return { isAuthenticated: !!data && !isError, isLoading }
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export { registrationStart, registrationComplete } from './api/registrationApi'
|
||||
export type { RegistrationStartPayload, RegistrationCompletePayload } from './api/registrationApi'
|
||||
export { registrationStart, registrationComplete, login } from './api/registrationApi'
|
||||
export type { RegistrationStartPayload, RegistrationCompletePayload, LoginPayload, AuthResponse } from './api/registrationApi'
|
||||
export { useIsAuthenticated } from './hooks/useIsAuthenticated'
|
||||
export { useAuth, AUTH_QUERY_KEY } from './hooks/useAuth'
|
||||
|
||||
Reference in New Issue
Block a user