fix user refresh
This commit is contained in:
parent
0b2a64e449
commit
cc3d138fd3
6 changed files with 6 additions and 13 deletions
|
@ -8,9 +8,6 @@ export function initClient(): ApiClient {
|
||||||
const client = createClient<paths>({ baseUrl: import.meta.env.VITE_API_URL })
|
const client = createClient<paths>({ baseUrl: import.meta.env.VITE_API_URL })
|
||||||
const UnauthorizedHandlerMiddleware: Middleware = {
|
const UnauthorizedHandlerMiddleware: Middleware = {
|
||||||
async onResponse({ response }) {
|
async onResponse({ response }) {
|
||||||
console.debug('on response middleware?')
|
|
||||||
console.debug(response.headers.getSetCookie())
|
|
||||||
console.debug(response.headers.get('set-cookie'))
|
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
dispatchMessage('auth:unauthorized', null)
|
dispatchMessage('auth:unauthorized', null)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { dispatchMessage } from '../messageBus/messageBus.ts'
|
import { dispatchMessage } from '../messageBus/messageBus.ts'
|
||||||
import { ProblemDetails } from '../../types'
|
import { ProblemDetails } from '../../types'
|
||||||
import { SignupCode } from './signupCode.ts'
|
import { SignupCode } from './signupCode.ts'
|
||||||
import { getCookie } from './cookies.ts'
|
|
||||||
import { ApiClient } from '../api/client.ts'
|
import { ApiClient } from '../api/client.ts'
|
||||||
|
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
|
@ -66,10 +65,6 @@ export class AuthService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async refreshUser(userId: string) {
|
async refreshUser(userId: string) {
|
||||||
if (getCookie('hasSession') !== 'true') {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.client.GET(`/auth/user/{userId}`, {
|
await this.client.GET(`/auth/user/{userId}`, {
|
||||||
params: {
|
params: {
|
||||||
path: { userId },
|
path: { userId },
|
||||||
|
|
|
@ -18,7 +18,7 @@ export default function RefreshUser({
|
||||||
if (didRefresh.current) return
|
if (didRefresh.current) return
|
||||||
if (user == null) return
|
if (user == null) return
|
||||||
didRefresh.current = true
|
didRefresh.current = true
|
||||||
await authService.refreshUser(user.userId)
|
await authService.refreshUser(user.id)
|
||||||
})
|
})
|
||||||
return () => clearTimeout(timeoutId)
|
return () => clearTimeout(timeoutId)
|
||||||
}, [authService, user])
|
}, [authService, user])
|
||||||
|
|
|
@ -52,7 +52,7 @@ export default function HomePage({ postsService, mediaService }: HomePageProps)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
const postId = await postsService.createNew(user.userId, content, media, isPublic)
|
const postId = await postsService.createNew(user.id, content, media, isPublic)
|
||||||
const post = new Post(postId, content, media, Temporal.Now.instant(), user.username)
|
const post = new Post(postId, content, media, Temporal.Now.instant(), user.username)
|
||||||
setPages((pages) => [[post], ...pages])
|
setPages((pages) => [[post], ...pages])
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -5,9 +5,9 @@ import { useState } from 'react'
|
||||||
import { setGlobal } from '../femtoApp.ts'
|
import { setGlobal } from '../femtoApp.ts'
|
||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
userId: string
|
id: string
|
||||||
username: string
|
username: string
|
||||||
isSuperUser: boolean
|
roles: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
let globalUser: User | null
|
let globalUser: User | null
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { AuthService } from './app/auth/authService.ts'
|
||||||
// Starts a loop that pings the server to keep the session alive, while also getting any updates on the user profile
|
// Starts a loop that pings the server to keep the session alive, while also getting any updates on the user profile
|
||||||
export function useRefreshSessionLoop(authService: AuthService) {
|
export function useRefreshSessionLoop(authService: AuthService) {
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const userId = user?.userId ?? null
|
const userId = user?.id ?? null
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (userId == null) {
|
if (userId == null) {
|
||||||
|
@ -17,6 +17,7 @@ export function useRefreshSessionLoop(authService: AuthService) {
|
||||||
timeouts.push(
|
timeouts.push(
|
||||||
setTimeout(async function refreshUser() {
|
setTimeout(async function refreshUser() {
|
||||||
await authService.refreshUser(userId)
|
await authService.refreshUser(userId)
|
||||||
|
|
||||||
timeouts.push(setTimeout(refreshUser, 60_000))
|
timeouts.push(setTimeout(refreshUser, 60_000))
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue