use user from session

This commit is contained in:
john 2025-05-20 10:06:18 +02:00
parent 5f47162a50
commit 700eaf3eb2
16 changed files with 148 additions and 107 deletions

View file

@ -2,7 +2,7 @@ import { dispatchMessage } from '../messageBus/messageBus.ts'
import client from '../api/client.ts'
import { ProblemDetails } from '../../types'
import { SignupCode } from './signupCode.ts'
import { User } from '../user/userStore.ts'
import { getCookie } from './cookies.ts'
export class AuthService {
constructor() {}
@ -17,7 +17,7 @@ export class AuthService {
throw new Error('invalid credentials')
}
dispatchMessage('auth:logged-in', { ...res.data })
dispatchMessage('auth:logged-in', null)
}
async signup(username: string, password: string, signupCode: string) {
@ -31,7 +31,7 @@ export class AuthService {
throw new Error((res.error as ProblemDetails)?.detail ?? 'invalid credentials')
}
dispatchMessage('auth:registered', { ...res.data })
dispatchMessage('auth:registered', null)
}
async logout() {
@ -65,30 +65,18 @@ export class AuthService {
return res.data.signupCodes.map(SignupCode.fromDto)
}
async refreshUser(userId: string): Promise<User | null> {
if (this.getCookie('hasSession') !== 'true') {
async refreshUser(userId: string) {
if (getCookie('hasSession') !== 'true') {
return null
}
const res = await client.GET(`/auth/user/{userId}`, {
await client.GET(`/auth/user/{userId}`, {
params: {
path: { userId },
},
credentials: 'include',
})
return res.data ?? null
}
private getCookie(cookieName: string): string | undefined {
const cookie = document.cookie
.split('; ')
.map((c) => {
const [name, value] = c.split('=')
return { name, value }
})
.find((c) => c.name === cookieName)
return cookie?.value
dispatchMessage('auth:refreshed', null)
}
}