|
| 1 | +import { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox' |
| 2 | +import { Type } from '@sinclair/typebox' |
| 3 | +import { QBCreateUserWithLogin } from 'quickblox' |
| 4 | +import { randomBytes } from 'crypto' |
| 5 | + |
| 6 | +import { QBSession, QBUser, QCClient } from '@/models' |
| 7 | +import { qbCreateSession, qbLogin } from '@/services/quickblox/auth' |
| 8 | +import { qbCreateUser } from '@/services/quickblox/users' |
| 9 | + |
| 10 | +export const createGuestClientSchema = { |
| 11 | + tags: ['Users', 'Client'], |
| 12 | + summary: 'Create Guest client', |
| 13 | + body: Type.Pick(QCClient, ['full_name']), |
| 14 | + response: { |
| 15 | + 200: Type.Object({ |
| 16 | + session: Type.Ref(QBSession), |
| 17 | + user: Type.Ref(QBUser), |
| 18 | + }), |
| 19 | + }, |
| 20 | + security: [{ apiKey: [] }, { providerSession: [] }] as Security, |
| 21 | +} |
| 22 | + |
| 23 | +const createGuestClient: FastifyPluginAsyncTypebox = async (fastify) => { |
| 24 | + fastify.post('', { schema: createGuestClientSchema }, async (request) => { |
| 25 | + const { full_name } = request.body |
| 26 | + const login = Date.now().toString() |
| 27 | + const password = randomBytes(8).toString('hex') |
| 28 | + const session = await qbCreateSession() |
| 29 | + |
| 30 | + await qbCreateUser<QBCreateUserWithLogin>({ |
| 31 | + login, |
| 32 | + password, |
| 33 | + full_name, |
| 34 | + tag_list: 'guest', |
| 35 | + }) |
| 36 | + const user = await qbLogin({ login, password }) |
| 37 | + |
| 38 | + return { session, user } |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +export const autoload = JSON.parse<boolean>(process.env.ENABLE_GUEST_CLIENT!) |
| 43 | + |
| 44 | +export default createGuestClient |
0 commit comments