refactor: type auth preHandlers and mirror clearCookie options
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { FastifyInstance } from 'fastify'
|
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
|
||||||
import {
|
import {
|
||||||
hashPassword,
|
hashPassword,
|
||||||
verifyPassword,
|
verifyPassword,
|
||||||
@@ -45,7 +45,7 @@ export function cookieOpts() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function requireAuth(request: any, reply: any): Promise<void> {
|
export async function requireAuth(request: FastifyRequest, reply: FastifyReply): Promise<void> {
|
||||||
const token = request.cookies[COOKIE_NAME]
|
const token = request.cookies[COOKIE_NAME]
|
||||||
if (token) {
|
if (token) {
|
||||||
const user = getUserBySession(request.server.db, token)
|
const user = getUserBySession(request.server.db, token)
|
||||||
@@ -57,7 +57,7 @@ export async function requireAuth(request: any, reply: any): Promise<void> {
|
|||||||
await reply.code(401).send({ error: 'unauthorized' })
|
await reply.code(401).send({ error: 'unauthorized' })
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function requireAdmin(request: any, reply: any): Promise<void> {
|
export async function requireAdmin(request: FastifyRequest, reply: FastifyReply): Promise<void> {
|
||||||
if (!request.user || request.user.is_admin !== 1) {
|
if (!request.user || request.user.is_admin !== 1) {
|
||||||
await reply.code(403).send({ error: 'forbidden' })
|
await reply.code(403).send({ error: 'forbidden' })
|
||||||
}
|
}
|
||||||
@@ -100,7 +100,7 @@ export async function registerAuthRoutes(app: FastifyInstance): Promise<void> {
|
|||||||
app.post('/api/logout', async (request, reply) => {
|
app.post('/api/logout', async (request, reply) => {
|
||||||
const token = request.cookies[COOKIE_NAME]
|
const token = request.cookies[COOKIE_NAME]
|
||||||
if (token) deleteSession(request.server.db, token)
|
if (token) deleteSession(request.server.db, token)
|
||||||
return reply.clearCookie(COOKIE_NAME, { path: '/' }).code(200).send({ ok: true })
|
return reply.clearCookie(COOKIE_NAME, cookieOpts()).code(200).send({ ok: true })
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/api/me', { preHandler: [requireAuth] }, async (request) => {
|
app.get('/api/me', { preHandler: [requireAuth] }, async (request) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user