@@ -2,31 +2,37 @@ import fastifyPlugin from 'fastify-plugin'
22import { EventDao } from './dao/eventDao'
33import { FastifyInstance , FastifyReply , FastifyRequest } from 'fastify'
44import { FastifyAuthFunction } from '@fastify/auth'
5-
5+ import { Static , Type } from '@sinclair/typebox'
66const verifyRequest = async ( fastify : FastifyInstance , request : FastifyRequest , reply : FastifyReply ) => {
77 // @ts -ignore
88 const eventId = request . params ?. eventId
99 // @ts -ignore
1010 const apiKey = request . query ?. apiKey
1111
1212 if ( ! eventId || eventId . length === 0 ) {
13- reply . code ( 400 ) . send ( { error : "Bad Request! Missing eventId, hophop let's get to work !" } )
13+ reply . code ( 400 ) . send ( { error : "Bad Request! Missing eventId, hophop let's get to work !" , success : false } )
1414 return
1515 }
1616
1717 if ( ! apiKey || apiKey . length === 0 ) {
18- reply . code ( 401 ) . send ( { error : 'Unauthorized! Du balai !' } )
18+ reply . code ( 401 ) . send ( { error : 'Unauthorized! Du balai !' , success : false } )
1919 return
2020 }
2121
2222 const event = await EventDao . getEvent ( fastify . firebase , eventId )
2323
2424 if ( event . apiKey !== apiKey ) {
25- reply . code ( 401 ) . send ( { error : 'Unauthorized! Du balai !' } )
25+ reply . code ( 401 ) . send ( { error : 'Unauthorized! Du balai !' , success : false } )
2626 return
2727 }
2828}
2929
30+ export const Error400_401_VerifyRequest = Type . Object ( {
31+ success : Type . Boolean ( ) ,
32+ error : Type . String ( ) ,
33+ } )
34+ export type Error400_401_VerifyRequestType = Static < typeof Error400_401_VerifyRequest >
35+
3036export const apiKeyPlugin = fastifyPlugin (
3137 ( fastify : FastifyInstance , options : any , next : ( ) => void ) => {
3238 fastify . decorate < FastifyAuthFunction > ( 'verifyApiKey' , async ( request : FastifyRequest , reply : FastifyReply ) => {
0 commit comments