вложения, канальчики, бим-бим + бам-бам
This commit is contained in:
@@ -1,40 +1,76 @@
|
||||
import type { Session, User } from 'lucia'
|
||||
import type { Session } from 'lucia'
|
||||
import { PrismaAdapter } from '@lucia-auth/adapter-prisma'
|
||||
import fp from 'fastify-plugin'
|
||||
import { auth } from '../auth/lucia.ts'
|
||||
import { Lucia } from 'lucia'
|
||||
|
||||
interface DatabaseUserAttributes {
|
||||
id: string
|
||||
displayName: string
|
||||
username: string
|
||||
createdAt: Date
|
||||
}
|
||||
|
||||
declare module 'lucia' {
|
||||
interface Register {
|
||||
Lucia: Lucia
|
||||
UserId: string
|
||||
DatabaseUserAttributes: DatabaseUserAttributes
|
||||
}
|
||||
}
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
lucia: Lucia
|
||||
}
|
||||
|
||||
interface FastifyRequest {
|
||||
user: User | null
|
||||
user: DatabaseUserAttributes | null
|
||||
session: Session | null
|
||||
}
|
||||
|
||||
interface FastifyContextConfig {
|
||||
skipAuth: boolean
|
||||
}
|
||||
}
|
||||
|
||||
export default fp(async (fastify) => {
|
||||
const lucia = new Lucia<any, DatabaseUserAttributes>(new PrismaAdapter(fastify.prisma.session, fastify.prisma.user), {
|
||||
sessionCookie: {
|
||||
attributes: {
|
||||
sameSite: 'none',
|
||||
},
|
||||
},
|
||||
getUserAttributes: (attrs) => {
|
||||
return attrs
|
||||
},
|
||||
})
|
||||
|
||||
fastify.decorate('lucia', lucia)
|
||||
fastify.decorateRequest('user', null)
|
||||
fastify.decorateRequest('session', null)
|
||||
|
||||
fastify.addHook('preHandler', async (req, reply) => {
|
||||
fastify.addHook('onRequest', async (req, reply) => {
|
||||
try {
|
||||
const sessionId = auth.readSessionCookie(req.headers.cookie ?? '')
|
||||
const sessionId = lucia.readSessionCookie(req.headers.cookie ?? '')
|
||||
|
||||
if (!sessionId)
|
||||
return
|
||||
|
||||
const { session, user } = await auth.validateSession(sessionId ?? '')
|
||||
const { session, user } = await lucia.validateSession(sessionId ?? '')
|
||||
|
||||
if (session && session.fresh) {
|
||||
const cookie = auth.createSessionCookie(session.id)
|
||||
const cookie = lucia.createSessionCookie(session.id)
|
||||
|
||||
reply.setCookie(cookie.name, cookie.value, cookie.attributes)
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
const blank = auth.createBlankSessionCookie()
|
||||
const blank = lucia.createBlankSessionCookie()
|
||||
|
||||
reply.setCookie(blank.name, blank.value, blank.attributes)
|
||||
}
|
||||
|
||||
req.user = user
|
||||
req.user = user as DatabaseUserAttributes
|
||||
req.session = session
|
||||
}
|
||||
catch {
|
||||
@@ -42,4 +78,21 @@ export default fp(async (fastify) => {
|
||||
req.session = null
|
||||
}
|
||||
})
|
||||
|
||||
fastify.addHook('onRequest', (req, reply, done) => {
|
||||
if (req.is404 || req.routeOptions.schema?.hide || req.routeOptions.config.skipAuth) {
|
||||
done()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (!req.user) {
|
||||
reply.unauthorized()
|
||||
}
|
||||
|
||||
done()
|
||||
})
|
||||
}, {
|
||||
name: 'auth',
|
||||
dependencies: ['prisma'],
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user