Archived
2
1
This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
2021-07-23 23:00:25 +02:00

77 lines
1.8 KiB
TypeScript

import express from 'express'
import jwt from 'jsonwebtoken'
import { Server as HttpServer } from 'http'
import { Server as HttpsServer } from 'https'
import { Server as SocketIoServer } from 'socket.io'
import enableDestroy from 'server-destroy'
import { authorize, AuthorizeOptions } from '../../index'
export interface Profile {
email: string
id: number
checkField: boolean
}
interface Socket {
io: null | SocketIoServer
init: (httpServer: HttpServer | HttpsServer) => void
}
const socket: Socket = {
io: null,
init (httpServer) {
socket.io = new SocketIoServer(httpServer)
}
}
let server: HttpServer | null = null
export const fixtureStart = async (
done: any,
options: AuthorizeOptions = { secret: 'super secret' }
): Promise<void> => {
const profile: Profile = {
email: 'john@doe.com',
id: 123,
checkField: true
}
let keySecret = ''
if (typeof options.secret === 'string') {
keySecret = options.secret
} else {
keySecret = await options.secret({ header: { alg: 'HS256' }, payload: profile })
}
const app = express()
app.use(express.json())
app.post('/login', (_req, res) => {
const token = jwt.sign(profile, keySecret, {
expiresIn: 60 * 60 * 5
})
return res.json({ token })
})
app.post('/login-wrong', (_req, res) => {
profile.checkField = false
const token = jwt.sign(profile, keySecret, {
expiresIn: 60 * 60 * 5
})
return res.json({ token })
})
server = app.listen(9000, done)
socket.init(server)
socket.io?.use(authorize(options))
enableDestroy(server)
}
export const fixtureStop = (callback: Function): void => {
socket.io?.close()
try {
server?.destroy()
} catch {}
callback()
}
export const getSocket = (): SocketIoServer | null => {
return socket.io
}