some change
This commit is contained in:
parent
d4a1492d56
commit
313f1def49
38 changed files with 475 additions and 401 deletions
51
src/app/auth/authService.ts
Normal file
51
src/app/auth/authService.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import { User } from '../user/userStore.ts'
|
||||
import { dispatchMessage } from '../messageBus/messageBus.ts'
|
||||
import client from '../api/client.ts'
|
||||
|
||||
export class AuthService {
|
||||
constructor(private readonly user: User | null) {}
|
||||
|
||||
async login(username: string, password: string) {
|
||||
if (this.user != null) {
|
||||
throw new Error('already logged in')
|
||||
}
|
||||
|
||||
const res = await client.POST('/auth/login', {
|
||||
body: { username, password },
|
||||
credentials: 'include',
|
||||
})
|
||||
|
||||
if (!res.data) {
|
||||
throw new Error('invalid credentials')
|
||||
}
|
||||
|
||||
dispatchMessage('auth:logged-in', { ...res.data })
|
||||
}
|
||||
|
||||
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', {
|
||||
body: { username, password, signupCode, email: null },
|
||||
credentials: 'include',
|
||||
})
|
||||
|
||||
if (!res.data) {
|
||||
throw new Error('invalid credentials')
|
||||
}
|
||||
|
||||
dispatchMessage('auth:registered', { ...res.data })
|
||||
}
|
||||
|
||||
async logout() {
|
||||
if (this.user == null) {
|
||||
return
|
||||
}
|
||||
|
||||
await client.DELETE('/auth/session', { credentials: 'include' })
|
||||
|
||||
dispatchMessage('auth:logged-out', null)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue