fix: authorization / registration
This commit is contained in:
@@ -7,6 +7,10 @@ interface CsrfResponse {
|
||||
|
||||
let cachedToken: string | null = null
|
||||
|
||||
export function clearCsrfCache(): void {
|
||||
cachedToken = null
|
||||
}
|
||||
|
||||
export async function getCsrfToken(): Promise<string> {
|
||||
if (cachedToken) return cachedToken
|
||||
const res = await fetch(`${API_URL}/csrf/token`, {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useNavigate, useLocation } from 'react-router-dom'
|
||||
import { loginStart, loginComplete, AUTH_QUERY_KEY } from '@features/auth'
|
||||
import { tokenStore } from '@shared/api/tokenStore'
|
||||
import { clearCsrfCache } from '@shared/api/csrf'
|
||||
import { ROUTES } from '@shared/config/routes'
|
||||
import type { ApiErrorResponse } from '@shared/api/types'
|
||||
|
||||
@@ -29,6 +30,7 @@ export function useLoginForm() {
|
||||
const completeMutation = useMutation({
|
||||
mutationFn: loginComplete,
|
||||
onSuccess: ({ access_token }) => {
|
||||
clearCsrfCache()
|
||||
tokenStore.set(access_token)
|
||||
queryClient.setQueryData(AUTH_QUERY_KEY, access_token)
|
||||
navigate(from, { replace: true })
|
||||
|
||||
@@ -3,6 +3,7 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { registrationStart, registrationComplete, AUTH_QUERY_KEY } from '@features/auth'
|
||||
import { tokenStore } from '@shared/api/tokenStore'
|
||||
import { clearCsrfCache } from '@shared/api/csrf'
|
||||
import { ROUTES } from '@shared/config/routes'
|
||||
import type { ApiErrorResponse } from '@shared/api/types'
|
||||
|
||||
@@ -29,6 +30,7 @@ export function useRegisterForm() {
|
||||
const completeMutation = useMutation({
|
||||
mutationFn: registrationComplete,
|
||||
onSuccess: ({ access_token }) => {
|
||||
clearCsrfCache()
|
||||
tokenStore.set(access_token)
|
||||
queryClient.setQueryData(AUTH_QUERY_KEY, access_token)
|
||||
navigate(ROUTES.WALLET)
|
||||
|
||||
Reference in New Issue
Block a user