mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-19 07:55:25 +02:00
improved authentication handling; added get-me api
This commit is contained in:
parent
330d04eca2
commit
7125c5f40d
@ -4,10 +4,16 @@ import {AxiosError} from "axios"
|
||||
import {decrypt, readMessage, readPrivateKey} from "openpgp"
|
||||
import {useNavigate} from "react-router-dom"
|
||||
|
||||
import {useMutation} from "@tanstack/react-query"
|
||||
import {useMutation, useQuery} from "@tanstack/react-query"
|
||||
|
||||
import {ServerUser, User} from "~/server-types"
|
||||
import {REFRESH_TOKEN_URL, RefreshTokenResult, logout as logoutUser, refreshToken} from "~/apis"
|
||||
import {AuthenticationDetails, ServerUser, User} from "~/server-types"
|
||||
import {
|
||||
REFRESH_TOKEN_URL,
|
||||
RefreshTokenResult,
|
||||
getMe,
|
||||
logout as logoutUser,
|
||||
refreshToken,
|
||||
} from "~/apis"
|
||||
import {client} from "~/constants/axios-client"
|
||||
import {decryptString, encryptString} from "~/utils"
|
||||
import {ExtensionKleckEvent} from "~/extension-types"
|
||||
@ -126,6 +132,12 @@ export default function AuthContextProvider({children}: AuthContextProviderProps
|
||||
[user?.encryptedPassword],
|
||||
)
|
||||
|
||||
useQuery<AuthenticationDetails, AxiosError>(["get_me"], getMe, {
|
||||
refetchOnWindowFocus: "always",
|
||||
refetchOnReconnect: "always",
|
||||
retry: 2,
|
||||
})
|
||||
|
||||
const value = useMemo<AuthContextType>(
|
||||
() => ({
|
||||
user: user ?? null,
|
||||
|
10
src/apis/get-me.ts
Normal file
10
src/apis/get-me.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import {AuthenticationDetails} from "~/server-types"
|
||||
import {client} from "~/constants/axios-client"
|
||||
|
||||
export default async function getMe(): Promise<AuthenticationDetails> {
|
||||
const {data} = await client.get(`${import.meta.env.VITE_SERVER_BASE_URL}/v1/account/me`, {
|
||||
withCredentials: true,
|
||||
})
|
||||
|
||||
return data
|
||||
}
|
@ -36,3 +36,5 @@ export * from "./update-alias"
|
||||
export {default as updateAlias} from "./update-alias"
|
||||
export * from "./delete-report"
|
||||
export {default as deleteReport} from "./delete-report"
|
||||
export * from "./get-me"
|
||||
export {default as getMe} from "./get-me"
|
||||
|
@ -9,7 +9,13 @@ export interface RefreshTokenResult {
|
||||
export const REFRESH_TOKEN_URL = `${import.meta.env.VITE_SERVER_BASE_URL}/v1/auth/refresh`
|
||||
|
||||
export default async function refreshToken(): Promise<RefreshTokenResult> {
|
||||
const {data} = await client.post(REFRESH_TOKEN_URL)
|
||||
const {data} = await client.post(
|
||||
REFRESH_TOKEN_URL,
|
||||
{},
|
||||
{
|
||||
withCredentials: true,
|
||||
},
|
||||
)
|
||||
|
||||
return data
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user