Add authenticate
- Create new controller authenticate with login root.. - Update and manage error message socket io. - Create enum for environment variables
This commit is contained in:
parent
e8da727cae
commit
53e1600e67
8 changed files with 85 additions and 7 deletions
28
back/src/Controller/AuthenticateController.ts
Normal file
28
back/src/Controller/AuthenticateController.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import {Application, Request, Response} from "express";
|
||||
import Jwt, {JsonWebTokenError} from "jsonwebtoken";
|
||||
import {BAD_REQUEST, OK} from "http-status-codes";
|
||||
import {SECRET_KEY} from "../Enum/EnvironmentVariable";
|
||||
|
||||
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
|
||||
let token = Jwt.sign({email: param.email}, SECRET_KEY, {expiresIn: '24h'});
|
||||
return res.status(OK).send({token: token});
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue