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