DefinitelyTyped/types/socketio-jwt/socketio-jwt-tests.ts
Eric Hallander 4fa0a09b77 Add types for socketio-jwt package. It may be unsupported, but it is still used. (#27088)
* Types file for socketio-jwt

I am tired of not having a types file for this package

* Should have left the names alone

* fixed lint findings

Ran the lint test.
2018-07-06 09:48:52 -07:00

48 lines
1.2 KiB
TypeScript

import * as fs from 'fs';
import * as http from 'http';
import * as SocketIo from 'socket.io';
import { authorize, JwtSecretFuncCallback } from 'socketio-jwt';
const app = http.createServer((req: any, rsp: any) => {
fs.readFile(__dirname + '/index.html',
(err: Error, data: any) => {
if (err) {
rsp.writeHead(500);
return rsp.end('Error loading index.html');
}
rsp.writeHead(200);
rsp.end(data);
});
});
const io = SocketIo(app);
// This example test code is using the Node Http Server
io.on('connection', authorize({
secret: 'Your Secret Here',
decodedPropertyName: 'anyNameYouWant'
}));
io.on('authenticated', (socket: SocketIo.Socket) => {
console.log('authenticated!!');
console.log(JSON.stringify((<any> socket).anyNameYouWant));
});
const secrets: any = {
user1: 'secret 1',
user2: 'secret 2'
};
// Assume a claim name of userId
function secretFunc(request: any, payload: any, callback: JwtSecretFuncCallback): void {
callback(null, secrets[payload.userId]);
}
// This example test code provides a callback function to get the secret
io.on('connection', authorize({
secret: secretFunc,
decodedPropertyName: 'anyNameYouWant'
}));