вложения, канальчики, бим-бим + бам-бам
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'],
|
||||
})
|
||||
|
||||
27
server/plugins/event-bus.ts
Normal file
27
server/plugins/event-bus.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { FastifyPluginAsync } from 'fastify'
|
||||
import type { Type } from 'typebox'
|
||||
import type { UserSchema } from '../schemas/auth.ts'
|
||||
import type { ChatMessageSchema } from '../schemas/chat.ts'
|
||||
import { EventEmitter } from 'node:events'
|
||||
import fp from 'fastify-plugin'
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
bus: EventEmitter
|
||||
}
|
||||
}
|
||||
|
||||
interface EventMap {
|
||||
'chat:new-message': [Type.Static<typeof ChatMessageSchema>]
|
||||
'user:profile-updated': [Type.Static<typeof UserSchema>]
|
||||
}
|
||||
|
||||
const plugin: FastifyPluginAsync = fp(async (fastify) => {
|
||||
const bus = new EventEmitter<EventMap>()
|
||||
|
||||
fastify.decorate('bus', bus)
|
||||
}, {
|
||||
name: 'event-bus',
|
||||
})
|
||||
|
||||
export default plugin
|
||||
32
server/plugins/prisma.ts
Normal file
32
server/plugins/prisma.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { FastifyPluginAsync } from 'fastify'
|
||||
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
|
||||
import fp from 'fastify-plugin'
|
||||
import { PrismaClient } from '../prisma/generated-client/client.ts'
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyInstance {
|
||||
prisma: PrismaClient
|
||||
}
|
||||
}
|
||||
|
||||
const plugin: FastifyPluginAsync = fp(async (fastify) => {
|
||||
const prisma = new PrismaClient({
|
||||
log: ['query', 'error', 'warn'],
|
||||
adapter: new PrismaBetterSqlite3({
|
||||
url: process.env.DATABASE_URL!,
|
||||
}),
|
||||
})
|
||||
|
||||
await prisma.$connect()
|
||||
|
||||
fastify.log.info('Testing DB Connection. OK')
|
||||
|
||||
fastify.decorate('prisma', prisma)
|
||||
fastify.addHook('onClose', async (fastify) => {
|
||||
await fastify.prisma.$disconnect()
|
||||
})
|
||||
}, {
|
||||
name: 'prisma',
|
||||
})
|
||||
|
||||
export default plugin
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { FastifyInstance } from 'fastify'
|
||||
import type { ServerOptions } from 'socket.io'
|
||||
import type { MessageSelect } from '../prisma/generated-client/models/Message.ts'
|
||||
import fp from 'fastify-plugin'
|
||||
import { Server } from 'socket.io'
|
||||
import registerChatSocket from '../socket/chat.ts'
|
||||
@@ -23,9 +24,11 @@ export default fp<Partial<ServerOptions>>(
|
||||
await fastify.io.close()
|
||||
})
|
||||
|
||||
fastify.ready(async () => {
|
||||
await registerWebrtcSocket(fastify.io, fastify.mediasoupRouter)
|
||||
await registerChatSocket(fastify.io)
|
||||
await registerWebrtcSocket(fastify.io, fastify.mediasoupRouter, fastify.prisma)
|
||||
await registerChatSocket(fastify.io)
|
||||
|
||||
fastify.bus.on('chat:new-message', async (message: MessageSelect) => {
|
||||
fastify.io.emit('chat:new-message', message)
|
||||
})
|
||||
},
|
||||
{ name: 'socket-io', dependencies: ['mediasoup-worker', 'mediasoup-router'] },
|
||||
|
||||
Reference in New Issue
Block a user