Migrating to uWS
This commit is contained in:
parent
783d58d3cb
commit
6a4c0c8678
31 changed files with 2056 additions and 1123 deletions
|
@ -3,38 +3,58 @@ 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';
|
||||
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
|
||||
import {BaseController} from "./BaseController";
|
||||
|
||||
export interface TokenInterface {
|
||||
name: string,
|
||||
userUuid: string
|
||||
}
|
||||
|
||||
export class AuthenticateController {
|
||||
App : Application;
|
||||
export class AuthenticateController extends BaseController {
|
||||
|
||||
constructor(App : Application) {
|
||||
this.App = App;
|
||||
constructor(private App : TemplatedApp) {
|
||||
super();
|
||||
this.login();
|
||||
}
|
||||
|
||||
onAbortedOrFinishedResponse(res: HttpResponse/*, readStream: any*/) {
|
||||
|
||||
console.log("ERROR! onAbortedOrFinishedResponse called!");
|
||||
/*if (res.id == -1) {
|
||||
console.log("ERROR! onAbortedOrFinishedResponse called twice for the same res!");
|
||||
} else {
|
||||
console.log('Stream was closed, openStreams: ' + --openStreams);
|
||||
console.timeEnd(res.id);
|
||||
readStream.destroy();
|
||||
}*/
|
||||
|
||||
/* Mark this response already accounted for */
|
||||
//res.id = -1;
|
||||
}
|
||||
|
||||
//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
|
||||
this.App.options("/login", (res: HttpResponse, req: HttpRequest) => {
|
||||
this.addCorsHeaders(res);
|
||||
|
||||
res.end();
|
||||
});
|
||||
|
||||
this.App.post("/login", async (res: HttpResponse, req: HttpRequest) => {
|
||||
this.addCorsHeaders(res);
|
||||
|
||||
res.onAborted(() => {
|
||||
console.warn('Login request was aborted');
|
||||
})
|
||||
const param = await res.json();
|
||||
const userUuid = uuid();
|
||||
const token = Jwt.sign({name: param.name, userUuid: userUuid} as TokenInterface, SECRET_KEY, {expiresIn: '24h'});
|
||||
return res.status(OK).send({
|
||||
res.writeStatus("200 OK").end(JSON.stringify({
|
||||
token: token,
|
||||
mapUrlStart: URL_ROOM_STARTED,
|
||||
userId: userUuid,
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue