Finish login and reset password

This commit is contained in:
Gregoire Parant 2020-12-21 19:43:57 +01:00
parent d5dc807b09
commit 53b96d61fe
8 changed files with 256 additions and 31 deletions

View file

@ -10,7 +10,7 @@
background: #eceeee; background: #eceeee;
border: 1px solid #42464b; border: 1px solid #42464b;
border-radius: 6px; border-radius: 6px;
height: 257px; height: 270px;
margin: 20px auto 0; margin: 20px auto 0;
width: 298px; width: 298px;
} }

View file

@ -16,6 +16,7 @@
#menuIcon button img{ #menuIcon button img{
width: 14px; width: 14px;
padding-top: 4px; padding-top: 4px;
cursor: url('/resources/logos/cursor_pointer.png'), pointer;
} }
#menuIcon section { #menuIcon section {
margin: 10px; margin: 10px;

View file

@ -112,6 +112,6 @@
</section> </section>
<section class="action"> <section class="action">
<button type="submit" id="gameRegisterFormSubmit">Sign In</button> <button type="submit" id="gameRegisterFormSubmit">Sign In</button>
<button type="submit" id="gameRegisterFormCancel">Login</button> <a href="#" id="gameRegisterFormCancel">Login</a>
</section> </section>
</form> </form>

View file

@ -19,7 +19,7 @@ class ConnectionManager {
private connectedUser?: ConnectedUser|null; private connectedUser?: ConnectedUser|null;
constructor() { constructor() {
this.verifyLoggedUser(); //this.userLogin();
} }
/** /**
@ -119,17 +119,16 @@ class ConnectionManager {
return this.connexionType; return this.connexionType;
} }
public verifyLoggedUser(){ /**
*
* @param email
* @param password
*/
public userLogin(email: string, password: string){
//Verify spark session //Verify spark session
Axios.get('http://admin.workadventure.localhost/user/connected').then((res) => { //TODO change url addresse
console.log('res', res); return Axios.post('http://pusher.workadventure.localhost/user/login',{email, password}).then((res) => {
Axios.get('http://admin.workadventure.localhost/notifications/recent').then((res) => { return res.data;
this.connectedUser = res.data;
}).catch((err) => {
console.error(err);
this.connectedUser = null;
throw new ErrorConnectedError('Error to get user element');
});
}).catch((err) => { }).catch((err) => {
if(err instanceof ErrorConnectedError) { if(err instanceof ErrorConnectedError) {
throw err; throw err;
@ -146,10 +145,33 @@ class ConnectionManager {
* @param name * @param name
* @param email * @param email
* @param password * @param password
* @param confirmPassword
*/ */
public registerUser(name: string, email: string, password: string, confirmPassword: string){ public registerUser(name: string, email: string, password: string){
Axios.get('http://admin.workadventure.localhost/member/register') //TODO change url addresse
Axios.post('http://pusher.workadventure.localhost/member/register', {
name,
email,
password
})
.then((res) => {
this.connectedUser = res.data;
})
.catch((err) => {
this.connectedUser = null;
console.log(err);
throw err;
})
}
/**
*
* @param email
*/
public passwordReset(email: string) {
//TODO change url addresse
return Axios.post('http://pusher.workadventure.localhost/user/password/reset', {
email,
})
.then((res) => { .then((res) => {
this.connectedUser = res.data; this.connectedUser = res.data;
}) })

View file

@ -4,6 +4,7 @@ import {gameManager} from "../Game/GameManager";
import {localUserStore} from "../../Connexion/LocalUserStore"; import {localUserStore} from "../../Connexion/LocalUserStore";
import {mediaManager} from "../../WebRtc/MediaManager"; import {mediaManager} from "../../WebRtc/MediaManager";
import {coWebsiteManager} from "../../WebRtc/CoWebsiteManager"; import {coWebsiteManager} from "../../WebRtc/CoWebsiteManager";
import {connectionManager} from "../../Connexion/ConnectionManager";
export const MenuSceneName = 'MenuScene'; export const MenuSceneName = 'MenuScene';
const gameMenuKey = 'gameMenu'; const gameMenuKey = 'gameMenu';
@ -144,6 +145,7 @@ export class MenuScene extends Phaser.Scene {
openSideMenu() { openSideMenu() {
if (this.sideMenuOpened) return; if (this.sideMenuOpened) return;
this.closeAll();
this.sideMenuOpened = true; this.sideMenuOpened = true;
this.menuButton.getChildByID('openMenuButton').innerHTML = 'X'; this.menuButton.getChildByID('openMenuButton').innerHTML = 'X';
if (gameManager.getCurrentGameScene(this).connection && gameManager.getCurrentGameScene(this).connection.isAdmin()) { if (gameManager.getCurrentGameScene(this).connection && gameManager.getCurrentGameScene(this).connection.isAdmin()) {
@ -161,12 +163,7 @@ export class MenuScene extends Phaser.Scene {
private closeSideMenu(): void { private closeSideMenu(): void {
if (!this.sideMenuOpened) return; if (!this.sideMenuOpened) return;
this.sideMenuOpened = false; this.sideMenuOpened = false;
this.closeGameQualityMenu(); this.closeAll();
this.closeGameForgotPassword();
this.closeGameLogin();
this.closeGameRegister();
this.closeGameShare();
this.menuButton.getChildByID('openMenuButton').innerHTML = `<img src="/static/images/menu.svg">`;
gameManager.getCurrentGameScene(this).ConsoleGlobalMessageManager.disabledMessageConsole(); gameManager.getCurrentGameScene(this).ConsoleGlobalMessageManager.disabledMessageConsole();
this.tweens.add({ this.tweens.add({
targets: this.menuElement, targets: this.menuElement,
@ -176,13 +173,13 @@ export class MenuScene extends Phaser.Scene {
}); });
} }
private openGameSettingsMenu(): void { private openGameSettingsMenu(): void {
if (this.settingsMenuOpened) { if (this.settingsMenuOpened) {
this.closeGameQualityMenu();
return; return;
} }
//close all
this.closeAll();
this.settingsMenuOpened = true; this.settingsMenuOpened = true;
const gameQualitySelect = this.gameQualityMenuElement.getChildByID('select-game-quality') as HTMLInputElement; const gameQualitySelect = this.gameQualityMenuElement.getChildByID('select-game-quality') as HTMLInputElement;
@ -233,12 +230,11 @@ export class MenuScene extends Phaser.Scene {
} }
private openGameLogin(): void{ private openGameLogin(): void{
this.closeGameRegister();
this.closeGameForgotPassword();
if (this.gameLoginMenuOpened) { if (this.gameLoginMenuOpened) {
this.closeGameLogin(); this.closeGameLogin();
return; return;
} }
this.closeAll();
this.gameLoginMenuOpened = true; this.gameLoginMenuOpened = true;
let middleY = (window.innerHeight / 3) - (257); let middleY = (window.innerHeight / 3) - (257);
@ -273,6 +269,8 @@ export class MenuScene extends Phaser.Scene {
this.closeGameShare(); this.closeGameShare();
return; return;
} }
//close all
this.closeAll();
const gameShareLink = this.gameShareElement.getChildByID('gameShareLink') as HTMLInputElement; const gameShareLink = this.gameShareElement.getChildByID('gameShareLink') as HTMLInputElement;
gameShareLink.value = location.toString(); gameShareLink.value = location.toString();
@ -313,7 +311,7 @@ export class MenuScene extends Phaser.Scene {
let errorForm = false; let errorForm = false;
const gameLoginEmail = this.gameLoginElement.getChildByID('gameLoginEmail') as HTMLInputElement; const gameLoginEmail = this.gameLoginElement.getChildByID('gameLoginEmail') as HTMLInputElement;
const gameLoginPassword = this.gameLoginElement.getChildByID('gameLoginPassword') as HTMLInputElement; const gameLoginPassword = this.gameLoginElement.getChildByID('gameLoginPassword') as HTMLInputElement;
const gameLoginError = this.gameLoginElement.getChildByID('gameLoginError') as HTMLInputElement; const gameLoginError = this.gameLoginElement.getChildByID('gameLoginError') as HTMLParagraphElement;
gameLoginError.innerText = ''; gameLoginError.innerText = '';
gameLoginError.style.display = 'none'; gameLoginError.style.display = 'none';
@ -337,8 +335,16 @@ export class MenuScene extends Phaser.Scene {
gameLoginError.innerText = 'Login or password incorrect'; gameLoginError.innerText = 'Login or password incorrect';
gameLoginError.style.display = 'block'; gameLoginError.style.display = 'block';
//TODO login user in back //TODO login user in back
connectionManager.userLogin(gameLoginEmail.value, gameLoginPassword.value).then(() => {
this.closeGameLogin();
}).catch((err) => {
gameLoginError.innerText = err.message;
gameLoginError.style.display = 'block';
});
} }
private sendEmail(){ private sendEmail(){
const gameForgotPasswordInfo = this.gameForgotPasswordElement.getChildByID('gameForgotPasswordInfo') as HTMLParagraphElement; const gameForgotPasswordInfo = this.gameForgotPasswordElement.getChildByID('gameForgotPasswordInfo') as HTMLParagraphElement;
gameForgotPasswordInfo.style.display = 'none'; gameForgotPasswordInfo.style.display = 'none';
@ -357,8 +363,16 @@ export class MenuScene extends Phaser.Scene {
return; return;
} }
//TODO send email //TODO send email
gameForgotPasswordInfo.style.display = 'block'; connectionManager.passwordReset(gameLoginForgotPasswordEmail.value).then(() => {
gameForgotPasswordInfo.innerText = 'We have emailed your password reset link!'; gameForgotPasswordInfo.style.display = 'block';
gameForgotPasswordInfo.innerText = 'We have emailed your password reset link!';
gameLoginForgotPasswordEmail.value = '';
this.closeGameLogin();
}).catch((err) => {
gameForgotPasswordError.innerText = err.message;
gameForgotPasswordError.style.display = 'block';
gameLoginForgotPasswordEmail.value = '';
});
} }
private register(){ private register(){
@ -405,6 +419,9 @@ export class MenuScene extends Phaser.Scene {
} }
private openGameForgotPassword(): void{ private openGameForgotPassword(): void{
//close all
this.closeAll();
let middleY = (window.innerHeight / 3) - (257); let middleY = (window.innerHeight / 3) - (257);
if(middleY < 0){ if(middleY < 0){
middleY = 0; middleY = 0;
@ -432,6 +449,9 @@ export class MenuScene extends Phaser.Scene {
} }
private openGameRegister(): void{ private openGameRegister(): void{
//close all
this.closeAll();
let middleY = (window.innerHeight / 3) - (257); let middleY = (window.innerHeight / 3) - (257);
if(middleY < 0){ if(middleY < 0){
middleY = 0; middleY = 0;
@ -516,4 +536,13 @@ export class MenuScene extends Phaser.Scene {
const sparkHost = 'https://'+window.location.host.replace('play.', '')+'/choose-map.html'; const sparkHost = 'https://'+window.location.host.replace('play.', '')+'/choose-map.html';
window.open(sparkHost, '_blank'); window.open(sparkHost, '_blank');
} }
private closeAll(){
this.closeGameQualityMenu();
this.closeGameForgotPassword();
this.closeGameLogin();
this.closeGameRegister();
this.closeGameShare();
this.menuButton.getChildByID('openMenuButton').innerHTML = `<img src="/static/images/menu.svg">`;
}
} }

View file

@ -16,6 +16,9 @@ export class AuthenticateController extends BaseController {
this.register(); this.register();
this.verify(); this.verify();
this.anonymLogin(); this.anonymLogin();
this.userRegister();
this.userLogin();
this.forgotPassword();
} }
//Try to login with an admin token //Try to login with an admin token
@ -132,4 +135,119 @@ export class AuthenticateController extends BaseController {
})); }));
}); });
} }
private userLogin(){
this.App.options("/user/login", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.end();
});
this.App.post("/user/login", (res: HttpResponse, req: HttpRequest) => {
(async () => {
res.onAborted(() => {
console.warn('Login request was aborted');
})
let userUuid = '';
try {
const params = await res.json();
const response = await adminApi.loginUser(params.email as string, params.password as string);
userUuid = response.data.uuid as string;
const authToken = jwtTokenManager.createJWTToken(userUuid);
res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify({
authToken,
userUuid,
user: response.data
}));
}catch (err){
res.writeStatus("400 KO");
this.addCorsHeaders(res);
res.end(JSON.stringify({
message: 'Email or password incorrect'
}));
}
})();
});
}
private userRegister(){
this.App.options("/user/register", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.end();
});
this.App.post("/user/register", (res: HttpResponse, req: HttpRequest) => {
(async () => {
res.onAborted(() => {
console.warn('Register request was aborted');
})
let userUuid = '';
try {
const params = await res.json();
const response = await adminApi.register(
params.name as string,
params.email as string,
params.password as string
);
userUuid = response.data.uuid as string;
const authToken = jwtTokenManager.createJWTToken(userUuid);
res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify({
authToken,
userUuid,
user: response.data
}));
}catch (err){
res.writeStatus("400 KO");
this.addCorsHeaders(res);
res.end(JSON.stringify({
message: err.message
}));
}
})();
});
}
private forgotPassword() {
this.App.options("/user/password/reset", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.end();
});
this.App.post("/user/password/reset", (res: HttpResponse, req: HttpRequest) => {
(async () => {
res.onAborted(() => {
console.warn('Forgot password request was aborted');
});
try {
const params = await res.json();
await adminApi.forgotPassword(params.email as string);
res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify({
message: 'Email sent!'
}));
} catch (err) {
res.writeStatus("400 KO");
this.addCorsHeaders(res);
res.end(JSON.stringify({
message: err.message
}));
}
})();
});
}
} }

View file

@ -4,7 +4,7 @@ const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIM
const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48; const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48;
const ALLOW_ARTILLERY = process.env.ALLOW_ARTILLERY ? process.env.ALLOW_ARTILLERY == 'true' : false; const ALLOW_ARTILLERY = process.env.ALLOW_ARTILLERY ? process.env.ALLOW_ARTILLERY == 'true' : false;
const API_URL = process.env.API_URL || ''; const API_URL = process.env.API_URL || '';
const ADMIN_API_URL = process.env.ADMIN_API_URL || ''; const ADMIN_API_URL = process.env.ADMIN_API_URL || 'http://admin';
const ADMIN_API_TOKEN = process.env.ADMIN_API_TOKEN || 'myapitoken'; const ADMIN_API_TOKEN = process.env.ADMIN_API_TOKEN || 'myapitoken';
const MAX_USERS_PER_ROOM = parseInt(process.env.MAX_USERS_PER_ROOM || '') || 600; const MAX_USERS_PER_ROOM = parseInt(process.env.MAX_USERS_PER_ROOM || '') || 600;
const CPU_OVERHEAT_THRESHOLD = Number(process.env.CPU_OVERHEAT_THRESHOLD) || 80; const CPU_OVERHEAT_THRESHOLD = Number(process.env.CPU_OVERHEAT_THRESHOLD) || 80;

View file

@ -110,6 +110,61 @@ class AdminApi {
headers: {"Authorization": `${ADMIN_API_TOKEN}`} headers: {"Authorization": `${ADMIN_API_TOKEN}`}
}); });
} }
/**
*
* @param email
* @param password
*/
loginUser(email: string, password: string) {
console.log('email', email);
console.log('password', password);
return Axios.post(`${ADMIN_API_URL}/api/user/login`, {
email,
password,
},
{
headers: {"Authorization": `${ADMIN_API_TOKEN}`}
});
}
/**
*
* @param name
* @param email
* @param password
*/
register(
name: string,
email: string,
password: string,
) {
return Axios.post(`${ADMIN_API_URL}/api/user/register`, {
name,
email,
password,
},
{
headers: {"Authorization": `${ADMIN_API_TOKEN}`}
});
}
/**
*
* @param name
* @param email
* @param password
*/
forgotPassword(
email: string
) {
return Axios.post(`${ADMIN_API_URL}/api/user/password/reset`, {
email
},
{
headers: {"Authorization": `${ADMIN_API_TOKEN}`}
});
}
} }
export const adminApi = new AdminApi(); export const adminApi = new AdminApi();