MVP for API Key authentication
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { Elysia } from 'elysia'
|
||||
import { createMerchantBody, createMerchantResponseSchema } from './schema'
|
||||
import CreateMerchantService from './commands/service'
|
||||
import { basicAuthMiddleware } from '~/middlewares/basicAuth'
|
||||
import { DataAlreadyExistsError } from '~/helpers/errors'
|
||||
|
||||
export const router = new Elysia({
|
||||
name: 'modules.auth',
|
||||
detail: { tags: ['Auth'] },
|
||||
prefix: '/auth',
|
||||
})
|
||||
.post('/get-key', async ({ body, headers, set }) => {
|
||||
basicAuthMiddleware(headers, set)
|
||||
|
||||
try {
|
||||
const result = await CreateMerchantService.createMerchant(body)
|
||||
return {
|
||||
status: 'success' as const,
|
||||
message: 'Merchant created successfully' as const,
|
||||
data: result,
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof DataAlreadyExistsError) {
|
||||
set.status = 409
|
||||
return {
|
||||
status: 'conflict' as const,
|
||||
message: 'Merchant with this email or phone already exists' as const,
|
||||
}
|
||||
}
|
||||
set.status = 500
|
||||
return {
|
||||
status: 'internal_error' as const,
|
||||
message: 'Server error' as const,
|
||||
}
|
||||
}
|
||||
}, {
|
||||
body: createMerchantBody,
|
||||
response: createMerchantResponseSchema,
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in New Issue
Block a user