update authservice
This commit is contained in:
parent
ca0a6b2950
commit
27098ec9fa
3 changed files with 108 additions and 19 deletions
|
@ -1,16 +1,11 @@
|
|||
import { User } from '../user/userStore.ts'
|
||||
import { dispatchMessage } from '../messageBus/messageBus.ts'
|
||||
import client from '../api/client.ts'
|
||||
import { ProblemDetails } from '../../types'
|
||||
|
||||
export class AuthService {
|
||||
constructor(private readonly user: User | null) {}
|
||||
constructor() {}
|
||||
|
||||
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',
|
||||
|
@ -24,10 +19,6 @@ export class AuthService {
|
|||
}
|
||||
|
||||
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',
|
||||
|
@ -42,12 +33,33 @@ export class AuthService {
|
|||
}
|
||||
|
||||
async logout() {
|
||||
if (this.user == null) {
|
||||
return
|
||||
}
|
||||
|
||||
await client.DELETE('/auth/session', { credentials: 'include' })
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue