mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: update frontend to use cookie session
* use SessionMiddleware in socket.io server * add back jwtStrategy for commands
This commit is contained in:
1 parent
be5cd152a9
commit
34c277c886
13 files changed
+90
-116
No files matched your search
+11
-20
@@ -1,7 +1,8 @@
|
||||
import { Server } from 'socket.io'
|
||||
import { getAndStartContainer, attachContainer } from '../containers/docker.js'
|
||||
import Session from '../models/session.js'
|
||||
import { genJWT, verifyJWT } from '../utils/auth.js'
|
||||
import SessionMiddleware from '../middleware/session.js'
|
||||
import { genJWT } from '../utils/auth.js'
|
||||
|
||||
const sessions = new Map()
|
||||
|
||||
@@ -27,6 +28,13 @@ export function pushToSession(sessionId, event, ...args) {
|
||||
|
||||
async function connectClient(socket) {
|
||||
console.log('Connected to the client.')
|
||||
|
||||
const user = socket.request.session?.passport?.user
|
||||
if (!user) {
|
||||
socket.send('User not found.')
|
||||
socket.disconnect()
|
||||
return
|
||||
}
|
||||
const sessionId = socket.handshake.query.session_id
|
||||
if (!sessionId) {
|
||||
socket.send('Session ID not found.')
|
||||
@@ -36,7 +44,7 @@ async function connectClient(socket) {
|
||||
|
||||
const session = await Session.findOne({
|
||||
_id: sessionId,
|
||||
user: socket.user,
|
||||
user,
|
||||
status: 'active',
|
||||
})
|
||||
if (!session) {
|
||||
@@ -89,24 +97,7 @@ async function connectClient(socket) {
|
||||
export default (server) => {
|
||||
const io = new Server(server)
|
||||
|
||||
io.use(async (socket, next) => {
|
||||
console.log('Authenticating...')
|
||||
try {
|
||||
const { token } = socket.handshake.auth
|
||||
if (!token) {
|
||||
next(new Error('Token not found.'))
|
||||
return
|
||||
}
|
||||
const user = await verifyJWT(token)
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
console.log(user)
|
||||
socket.user = user
|
||||
next()
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
next(err)
|
||||
}
|
||||
})
|
||||
io.engine.use(SessionMiddleware)
|
||||
|
||||
io.on('connection', connectClient)
|
||||
}
|
||||
Reference in new issue
Block a user