2020-04-04 17:22:02 +02:00
|
|
|
import {Application, Request, Response} from "express";
|
2020-04-05 14:31:49 +02:00
|
|
|
import Jwt from "jsonwebtoken";
|
2020-04-04 17:22:02 +02:00
|
|
|
import {BAD_REQUEST, OK} from "http-status-codes";
|
2020-05-10 13:58:32 +02:00
|
|
|
import {SECRET_KEY, ROOM_STARTED, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
|
2020-04-10 12:54:05 +02:00
|
|
|
import { uuid } from 'uuidv4';
|
2020-04-04 17:22:02 +02:00
|
|
|
|
|
|
|
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(){
|
|
|
|
this.App.post("/login", (req: Request, res: Response) => {
|
|
|
|
let param = req.body;
|
|
|
|
if(!param.email){
|
|
|
|
return res.status(BAD_REQUEST).send({
|
|
|
|
message: "email parameter is empty"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//TODO check user email for The Coding Machine game
|
2020-04-10 12:54:05 +02:00
|
|
|
let userId = uuid();
|
2020-05-10 13:58:32 +02:00
|
|
|
let token = Jwt.sign({email: param.email, userId: userId}, SECRET_KEY, {expiresIn: '24h'});
|
2020-04-10 12:54:05 +02:00
|
|
|
return res.status(OK).send({
|
|
|
|
token: token,
|
2020-05-10 13:58:32 +02:00
|
|
|
startedRoom: {key: ROOM_STARTED, url: URL_ROOM_STARTED},
|
2020-05-09 19:41:21 +02:00
|
|
|
userId: userId,
|
2020-04-10 12:54:05 +02:00
|
|
|
});
|
2020-04-04 17:22:02 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|