30 lines
759 B
TypeScript
30 lines
759 B
TypeScript
import { hash } from 'bcrypt'
|
|
// import { User } from './models.ts'
|
|
import { TAuthUser, toUser, TUser } from '@lyrics/models.ts'
|
|
import { ulid } from '$std/ulid/mod.ts'
|
|
import { db } from '@lyrics/db.ts'
|
|
|
|
export const rpcs = {
|
|
'ping': (): 'pong' => {
|
|
console.log('received ping')
|
|
return 'pong'
|
|
},
|
|
'getVersion': () => '0.1.0',
|
|
async registerUser(
|
|
username: string,
|
|
password: string,
|
|
): Promise<TUser> {
|
|
const user: TAuthUser = {
|
|
id: ulid(),
|
|
createdAt: new Date(),
|
|
username: username,
|
|
passwordDigest: await hash(password),
|
|
}
|
|
const result = await db.user.save(user)
|
|
if (!result.ok) throw new Error('failed to save user')
|
|
return toUser(user)
|
|
},
|
|
}
|
|
|
|
export type LyricsRPCs = typeof rpcs
|
|
export const rpcNames = Object.keys(rpcs)
|