a373626e24
To do this, I used generic-type-guard package which generates both an interface AND a valid type guard from code. With this, we are 100% sure that the messages we receive are validated at runtime! The client cannot pass us an object that is invalid! \o/
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import {Application, Request, Response} from "express";
|
|
import Jwt from "jsonwebtoken";
|
|
import {BAD_REQUEST, OK} from "http-status-codes";
|
|
import {SECRET_KEY, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
|
|
import { uuid } from 'uuidv4';
|
|
|
|
export interface TokenInterface {
|
|
name: string,
|
|
userId: string
|
|
}
|
|
|
|
export class AuthenticateController {
|
|
App : Application;
|
|
|
|
constructor(App : Application) {
|
|
this.App = App;
|
|
this.login();
|
|
}
|
|
|
|
//permit to login on application. Return token to connect on Websocket IO.
|
|
login(){
|
|
// For now, let's completely forget the /login route.
|
|
this.App.post("/login", (req: Request, res: Response) => {
|
|
const param = req.body;
|
|
/*if(!param.name){
|
|
return res.status(BAD_REQUEST).send({
|
|
message: "email parameter is empty"
|
|
});
|
|
}*/
|
|
//TODO check user email for The Coding Machine game
|
|
const userId = uuid();
|
|
const token = Jwt.sign({name: param.name, userId: userId} as TokenInterface, SECRET_KEY, {expiresIn: '24h'});
|
|
return res.status(OK).send({
|
|
token: token,
|
|
mapUrlStart: URL_ROOM_STARTED,
|
|
userId: userId,
|
|
});
|
|
});
|
|
}
|
|
}
|