feat: add optional onAuthentication option to add user property in socket object (#62)

This commit is contained in:
Sayak Mukhopadhyay
2021-03-08 18:15:39 +05:30
committed by divlo
parent 89518a45d5
commit c7f64a6312
4 changed files with 136 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import axios from 'axios'
import { io } from 'socket.io-client'
import { fixtureStart, fixtureStop } from './fixture'
import { fixtureStart, fixtureStop, getSocket, Profile } from './fixture'
describe('authorize - with secret as string in options', () => {
let token: string = ''
@ -101,3 +101,76 @@ describe('authorize - with secret as callback in options', () => {
})
})
})
describe('authorize - with onAuthentication callback in options', () => {
let token: string = ''
let wrongToken: string = ''
beforeEach(async (done) => {
jest.setTimeout(15_000)
await fixtureStart(
async () => {
const response = await axios.post('http://localhost:9000/login')
token = response.data.token
const responseWrong = await axios.post('http://localhost:9000/login-wrong')
wrongToken = responseWrong.data.token
done()
},
{
secret: secretCallback,
onAuthentication: (decodedToken: Profile) => {
if (!decodedToken.checkField) {
throw new Error('Check Field validation failed')
}
return {
email: decodedToken.email
}
}
}
)
})
afterEach((done) => {
fixtureStop(done)
})
it('should connect the user', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: `Bearer ${token}` }
})
socket.on('connect', () => {
socket.close()
done()
})
})
it('should contain user property', (done) => {
const socketServer = getSocket()
socketServer?.on('connection', (client: any) => {
expect(client.user.email).toEqual('john@doe.com')
})
const socket = io('http://localhost:9000', {
auth: { token: `Bearer ${token}` }
})
socket.on('connect', () => {
socket.close()
done()
})
})
it('should emit error when user validation fails', (done) => {
const socket = io('http://localhost:9000', {
auth: { token: `Bearer ${wrongToken}` }
})
socket.on('connect_error', (err: any) => {
try {
expect(err.message).toEqual('Check Field validation failed')
} catch (err) {
socket.close()
done(err)
}
socket.close()
done()
})
})
})

View File

@ -7,6 +7,12 @@ 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
@ -34,9 +40,21 @@ export const fixtureStart = async (
keySecret = await options.secret({ header: { alg: 'RS256' }, payload: '' })
}
app.post('/login', (_req, res) => {
const profile = {
const profile: Profile = {
email: 'john@doe.com',
id: 123
id: 123,
checkField: true
}
const token = jwt.sign(profile, keySecret, {
expiresIn: 60 * 60 * 5
})
return res.json({ token })
})
app.post('/login-wrong', (_req, res) => {
const profile: Profile = {
email: 'john@doe.com',
id: 123,
checkField: false
}
const token = jwt.sign(profile, keySecret, {
expiresIn: 60 * 60 * 5
@ -56,3 +74,7 @@ export const fixtureStop = (callback: Function): void => {
} catch (err) {}
callback()
}
export const getSocket = (): SocketIoServer | null => {
return socket.io
}