mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Chat
This commit is contained in:
parent
b0fad8d4a2
commit
aea8ad22d1
6 changed files with 115 additions and 69 deletions
|
@ -8,7 +8,7 @@ interface Options {
|
|||
exceptions: Array<string>
|
||||
}
|
||||
|
||||
const testUser = {
|
||||
export const testUser = {
|
||||
id: 19,
|
||||
avaiblePWRequests: 645,
|
||||
pwRequestCount: 19,
|
||||
|
|
|
@ -2,34 +2,44 @@ import logger from '../utils/logger'
|
|||
import dbtools from '../utils/dbtools'
|
||||
import cookie from 'cookie'
|
||||
|
||||
import { testUser } from './auth.middleware'
|
||||
|
||||
interface Options {
|
||||
userDB: any
|
||||
}
|
||||
|
||||
export default function (options: Options): any {
|
||||
export default function SocketAuth(options: Options): any {
|
||||
const { userDB } = options
|
||||
|
||||
return function (socket, next) {
|
||||
const cookies = cookie.parse(socket.handshake.headers.cookie)
|
||||
const sessionID = cookies.sessionID
|
||||
return (socket, next) => {
|
||||
try {
|
||||
const cookies = cookie.parse(socket.handshake.headers.cookie || '')
|
||||
const sessionID = cookies.sessionID
|
||||
|
||||
if (process.env.NS_NOUSER) {
|
||||
if (process.env.NS_NOUSER) {
|
||||
socket.user = testUser
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
if (!sessionID) {
|
||||
next(new Error('Not authenticated, please log in'))
|
||||
return
|
||||
}
|
||||
|
||||
const user = GetUserBySessionID(userDB, sessionID)
|
||||
|
||||
if (!user) {
|
||||
next(new Error('Not authenticated, please log in'))
|
||||
return
|
||||
}
|
||||
socket.user = user
|
||||
next()
|
||||
return
|
||||
} catch (e) {
|
||||
next(new Error('Authentication server error'))
|
||||
console.error('Authentication server error')
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
if (!sessionID) {
|
||||
next(new Error('Authentication error'))
|
||||
return
|
||||
}
|
||||
|
||||
const user = GetUserBySessionID(userDB, sessionID)
|
||||
|
||||
if (!user) {
|
||||
next(new Error('Authentication error'))
|
||||
return
|
||||
}
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue