update authservice

This commit is contained in:
john 2025-05-18 21:20:32 +02:00
parent ca0a6b2950
commit 27098ec9fa
3 changed files with 108 additions and 19 deletions

View file

@ -6,15 +6,13 @@ import { MediaService } from './app/media/mediaService.ts'
import SignupPage from './app/auth/pages/SignupPage.tsx' import SignupPage from './app/auth/pages/SignupPage.tsx'
import LoginPage from './app/auth/pages/LoginPage.tsx' import LoginPage from './app/auth/pages/LoginPage.tsx'
import { AuthService } from './app/auth/authService.ts' import { AuthService } from './app/auth/authService.ts'
import { useUser } from './app/user/userStore.ts'
import LogoutPage from './app/auth/pages/LogoutPage.tsx' import LogoutPage from './app/auth/pages/LogoutPage.tsx'
import UnauthorizedHandler from './app/auth/components/UnauthorizedHandler.tsx' import UnauthorizedHandler from './app/auth/components/UnauthorizedHandler.tsx'
function App() { function App() {
const { user } = useUser()
const postService = new PostsService() const postService = new PostsService()
const mediaService = new MediaService() const mediaService = new MediaService()
const authService = new AuthService(user) const authService = new AuthService()
return ( return (
<BrowserRouter> <BrowserRouter>

View file

@ -87,8 +87,7 @@ export interface paths {
requestBody: { requestBody: {
content: { content: {
'multipart/form-data': { 'multipart/form-data': {
/** Format: binary */ file?: components['schemas']['IFormFile']
file?: string
} }
} }
} }
@ -266,6 +265,66 @@ export interface paths {
patch?: never patch?: never
trace?: never trace?: never
} }
'/auth/signup-codes': {
parameters: {
query?: never
header?: never
path?: never
cookie?: never
}
get: {
parameters: {
query?: never
header?: never
path?: never
cookie?: never
}
requestBody?: never
responses: {
/** @description OK */
200: {
headers: {
[name: string]: unknown
}
content: {
'text/plain': components['schemas']['ListSignupCodesResult']
'application/json': components['schemas']['ListSignupCodesResult']
'text/json': components['schemas']['ListSignupCodesResult']
}
}
}
}
put?: never
post: {
parameters: {
query?: never
header?: never
path?: never
cookie?: never
}
requestBody: {
content: {
'application/json': components['schemas']['CreateSignupCodeRequest']
'text/json': components['schemas']['CreateSignupCodeRequest']
'application/*+json': components['schemas']['CreateSignupCodeRequest']
}
}
responses: {
/** @description OK */
200: {
headers: {
[name: string]: unknown
}
content?: never
}
}
}
delete?: never
options?: never
head?: never
patch?: never
trace?: never
}
} }
export type webhooks = Record<string, never> export type webhooks = Record<string, never>
export interface components { export interface components {
@ -292,11 +351,21 @@ export interface components {
/** Format: uuid */ /** Format: uuid */
postId: string postId: string
} }
CreateSignupCodeRequest: {
code: string
email: string
name: string
}
GetAllPublicPostsResponse: { GetAllPublicPostsResponse: {
posts: components['schemas']['PostDto'][] posts: components['schemas']['PostDto'][]
/** Format: uuid */ /** Format: uuid */
next: string | null next: string | null
} }
/** Format: binary */
IFormFile: string
ListSignupCodesResult: {
signupCodes: components['schemas']['SignupCodeDto'][]
}
LoginRequest: { LoginRequest: {
username: string username: string
password: string password: string
@ -339,6 +408,16 @@ export interface components {
userId: string userId: string
username: string username: string
} }
SignupCodeDto: {
code: string
email: string
name: string
/** Format: uuid */
redeemingUserId: string | null
redeemingUsername: string | null
/** Format: date-time */
expiresOn: string | null
}
UploadMediaResponse: { UploadMediaResponse: {
/** Format: uuid */ /** Format: uuid */
mediaId: string mediaId: string

View file

@ -1,16 +1,11 @@
import { User } from '../user/userStore.ts'
import { dispatchMessage } from '../messageBus/messageBus.ts' import { dispatchMessage } from '../messageBus/messageBus.ts'
import client from '../api/client.ts' import client from '../api/client.ts'
import { ProblemDetails } from '../../types' import { ProblemDetails } from '../../types'
export class AuthService { export class AuthService {
constructor(private readonly user: User | null) {} constructor() {}
async login(username: string, password: string) { async login(username: string, password: string) {
if (this.user != null) {
throw new Error('already logged in')
}
const res = await client.POST('/auth/login', { const res = await client.POST('/auth/login', {
body: { username, password }, body: { username, password },
credentials: 'include', credentials: 'include',
@ -24,10 +19,6 @@ export class AuthService {
} }
async signup(username: string, password: string, signupCode: string) { async signup(username: string, password: string, signupCode: string) {
if (this.user != null) {
throw new Error('already logged in')
}
const res = await client.POST('/auth/register', { const res = await client.POST('/auth/register', {
body: { username, password, signupCode, email: null }, body: { username, password, signupCode, email: null },
credentials: 'include', credentials: 'include',
@ -42,12 +33,33 @@ export class AuthService {
} }
async logout() { async logout() {
if (this.user == null) {
return
}
await client.DELETE('/auth/session', { credentials: 'include' }) await client.DELETE('/auth/session', { credentials: 'include' })
dispatchMessage('auth:logged-out', null) dispatchMessage('auth:logged-out', null)
} }
async createSignupCode(code: string, email: string, name: string) {
const res = await client.POST('/auth/signup-codes', {
body: { code, email, name },
credentials: 'include',
})
if (!res.data) {
console.error(res.error)
throw new Error('failed to create signup code')
}
}
async listSignupCodes() {
const res = await client.GET('/auth/signup-codes', {
credentials: 'include',
})
if (!res.data) {
console.error(res.error)
throw new Error('error')
}
return res.data.signupCodes
}
} }