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});
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,11 +1,10 @@
|
|||
import socketIO = require('socket.io');
|
||||
import {Socket} from "socket.io";
|
||||
import * as http from "http";
|
||||
import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition"; //TODO fix to use "_Model/.."
|
||||
import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix to use "_Model/.."
|
||||
import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition"; //TODO fix import by "_Model/.."
|
||||
import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.."
|
||||
import Jwt, {JsonWebTokenError} from "jsonwebtoken";
|
||||
|
||||
const SECRET_KEY = process.env.SECRET_KEY || "THECODINGMACHINE_SECRET_KEY";
|
||||
import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
|
||||
|
||||
export class IoSocketController{
|
||||
Io: socketIO.Server;
|
||||
|
@ -39,8 +38,13 @@ export class IoSocketController{
|
|||
x: user x position on map
|
||||
y: user y position on map
|
||||
*/
|
||||
|
||||
socket.on('join-room', (message : string) => {
|
||||
let messageUserPosition = new MessageUserPosition(message);
|
||||
let messageUserPosition = this.hydrateMessageReceive(message);
|
||||
if(!messageUserPosition){
|
||||
return socket.emit("message-error", JSON.stringify({message: "Error format message"}))
|
||||
}
|
||||
//join user in room
|
||||
socket.join(messageUserPosition.roomId);
|
||||
// sending to all clients in room except sender
|
||||
this.saveUserPosition((socket as ExSocketInterface), messageUserPosition);
|
||||
|
@ -48,7 +52,10 @@ export class IoSocketController{
|
|||
});
|
||||
|
||||
socket.on('user-position', (message : string) => {
|
||||
let messageUserPosition = new MessageUserPosition(message);
|
||||
let messageUserPosition = this.hydrateMessageReceive(message);
|
||||
if(!messageUserPosition){
|
||||
return socket.emit("message-error", JSON.stringify({message: "Error format message"}));
|
||||
}
|
||||
// sending to all clients in room except sender
|
||||
this.saveUserPosition((socket as ExSocketInterface), messageUserPosition);
|
||||
socket.to(messageUserPosition.roomId).emit('join-room', messageUserPosition.toString());
|
||||
|
@ -60,4 +67,14 @@ export class IoSocketController{
|
|||
saveUserPosition(socket : ExSocketInterface, message : MessageUserPosition){
|
||||
socket.position = message.position;
|
||||
}
|
||||
|
||||
//Hydrate and manage error
|
||||
hydrateMessageReceive(message : string) : MessageUserPosition | null{
|
||||
try {
|
||||
return new MessageUserPosition(message);
|
||||
}catch (err) {
|
||||
//TODO log error
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue