Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e3d0106d8f | ||
|
|
e2068dd89a | ||
|
|
a2f845f228 | ||
|
|
1a497d402d | ||
|
|
924bbd4285 | ||
|
|
58d37ee02b | ||
|
|
ba12d413dc | ||
|
|
f525d1afe5 | ||
|
|
75fe5b0b8c |
@ -17,6 +17,11 @@ interface DatabaseUserAttributes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const auth = new Lucia(new PrismaAdapter(prisma.session, prisma.user), {
|
export const auth = new Lucia(new PrismaAdapter(prisma.session, prisma.user), {
|
||||||
|
sessionCookie: {
|
||||||
|
attributes: {
|
||||||
|
sameSite: 'none',
|
||||||
|
},
|
||||||
|
},
|
||||||
getUserAttributes: ({ id, displayName, username }) => {
|
getUserAttributes: ({ id, displayName, username }) => {
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
|
|||||||
@ -1,47 +0,0 @@
|
|||||||
import { createServer as createHttpServer } from 'node:http'
|
|
||||||
import { consola } from 'consola'
|
|
||||||
import cors from 'cors'
|
|
||||||
import express from 'express'
|
|
||||||
import * as mediasoup from 'mediasoup'
|
|
||||||
import { Server as SocketServer } from 'socket.io'
|
|
||||||
import { webrtcSocket } from './sockets'
|
|
||||||
|
|
||||||
(async () => {
|
|
||||||
const app = express()
|
|
||||||
|
|
||||||
app.use(cors())
|
|
||||||
|
|
||||||
const server = createHttpServer(app)
|
|
||||||
|
|
||||||
const worker = await mediasoup.createWorker()
|
|
||||||
worker.on('died', () => {
|
|
||||||
consola.error('[Mediasoup]', 'Worker died, exiting...')
|
|
||||||
|
|
||||||
process.exit(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
const router = await worker.createRouter({
|
|
||||||
mediaCodecs: [
|
|
||||||
{
|
|
||||||
kind: 'audio',
|
|
||||||
mimeType: 'audio/opus',
|
|
||||||
clockRate: 48000,
|
|
||||||
channels: 2,
|
|
||||||
parameters: { useinbandfec: 1, stereo: 1 },
|
|
||||||
},
|
|
||||||
],
|
|
||||||
})
|
|
||||||
|
|
||||||
const io = new SocketServer(server, {
|
|
||||||
path: '/chad/ws',
|
|
||||||
cors: {
|
|
||||||
origin: process.env.CORS_ORIGIN || '*',
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
webrtcSocket(io, router)
|
|
||||||
|
|
||||||
server.listen(process.env.PORT || 4000, () => {
|
|
||||||
consola.success('[Server]', 'Server started!')
|
|
||||||
})
|
|
||||||
})()
|
|
||||||
@ -17,15 +17,20 @@ export default fp(async (fastify) => {
|
|||||||
try {
|
try {
|
||||||
const sessionId = auth.readSessionCookie(req.headers.cookie ?? '')
|
const sessionId = auth.readSessionCookie(req.headers.cookie ?? '')
|
||||||
|
|
||||||
|
if (!sessionId)
|
||||||
|
return
|
||||||
|
|
||||||
const { session, user } = await auth.validateSession(sessionId ?? '')
|
const { session, user } = await auth.validateSession(sessionId ?? '')
|
||||||
|
|
||||||
if (session && session.fresh) {
|
if (session && session.fresh) {
|
||||||
const cookie = auth.createSessionCookie(session.id)
|
const cookie = auth.createSessionCookie(session.id)
|
||||||
|
|
||||||
reply.setCookie(cookie.name, cookie.value, cookie.attributes)
|
reply.setCookie(cookie.name, cookie.value, cookie.attributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
const blank = auth.createBlankSessionCookie()
|
const blank = auth.createBlankSessionCookie()
|
||||||
|
|
||||||
reply.setCookie(blank.name, blank.value, blank.attributes)
|
reply.setCookie(blank.name, blank.value, blank.attributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import type { FastifyInstance } from 'fastify'
|
import type { FastifyInstance } from 'fastify'
|
||||||
import type { RegisterOptions } from 'fastify/types/register.js'
|
|
||||||
import bcrypt from 'bcrypt'
|
import bcrypt from 'bcrypt'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { auth } from '../auth/lucia.ts'
|
import { auth } from '../auth/lucia.ts'
|
||||||
@ -71,8 +70,6 @@ export default function (fastify: FastifyInstance) {
|
|||||||
const session = await auth.createSession(user.id, {})
|
const session = await auth.createSession(user.id, {})
|
||||||
const cookie = auth.createSessionCookie(session.id)
|
const cookie = auth.createSessionCookie(session.id)
|
||||||
|
|
||||||
cookie.attributes.secure = false
|
|
||||||
|
|
||||||
reply.setCookie(cookie.name, cookie.value, cookie.attributes)
|
reply.setCookie(cookie.name, cookie.value, cookie.attributes)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -119,7 +116,3 @@ export default function (fastify: FastifyInstance) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const autoConfig: RegisterOptions = {
|
|
||||||
prefix: '/chad',
|
|
||||||
}
|
|
||||||
|
|||||||
@ -13,7 +13,13 @@ const fastify = Fastify({
|
|||||||
logger: true,
|
logger: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
fastify.register(FastifyCors)
|
fastify.register(FastifyCors, {
|
||||||
|
origin: [
|
||||||
|
'http://localhost:3000',
|
||||||
|
'http://tauri.localhost',
|
||||||
|
],
|
||||||
|
credentials: true,
|
||||||
|
})
|
||||||
|
|
||||||
fastify.register(FastifyCookie)
|
fastify.register(FastifyCookie)
|
||||||
|
|
||||||
@ -23,13 +29,14 @@ fastify.register(FastifyAutoLoad, {
|
|||||||
|
|
||||||
fastify.register(FastifyAutoLoad, {
|
fastify.register(FastifyAutoLoad, {
|
||||||
dir: join(__dirname, 'routes'),
|
dir: join(__dirname, 'routes'),
|
||||||
|
options: { prefix: 'chad' },
|
||||||
})
|
})
|
||||||
|
|
||||||
;(async () => {
|
;(async () => {
|
||||||
const port = process.env.PORT ? Number(process.env.PORT) : 4000
|
const port = process.env.PORT ? Number(process.env.PORT) : 4000
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fastify.listen({ port })
|
await fastify.listen({ port, host: '0.0.0.0' })
|
||||||
|
|
||||||
await prisma.$connect()
|
await prisma.$connect()
|
||||||
fastify.log.info('Testing DB Connection. OK')
|
fastify.log.info('Testing DB Connection. OK')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user