diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index 446b1176..bb0729b8 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -7,7 +7,6 @@ import {localUserStore} from "./LocalUserStore"; import {ConnectedUser, LocalUser} from "./LocalUser"; import {Room} from "./Room"; import {NotConnectedError} from "../Exception/NotConnectedError"; -import {ErrorConnectedError} from "../Exception/ErrorConnectedError"; const URL_ROOM_STARTED = '/Floor0/floor0.json'; @@ -19,7 +18,8 @@ class ConnectionManager { private connectedUser?: ConnectedUser|null; constructor() { - //this.userLogin(); + this.connectedUser = localUserStore.getUserConnected(); + this.getNotification(); } /** @@ -124,17 +124,39 @@ class ConnectionManager { * @param email * @param password */ - public userLogin(email: string, password: string){ + public userLogin(email: string, password: string) { //Verify spark session //TODO change url addresse - return Axios.post('http://pusher.workadventure.localhost/user/login',{email, password}).then((res) => { - return res.data; + + return Axios.post('http://pusher.workadventure.localhost/user/login', {email, password}).then((res) => { + const user = res.data; + this.localUser = new LocalUser(res.data.userUuid, res.data.authToken, res.data.textures || []); + localUserStore.saveUser(this.localUser); + this.connectedUser = new ConnectedUser( + user.name, + user.email, + user.uuid, + user.jwtToken, + [], + [] + ); + localUserStore.saveUserConnected(this.connectedUser); + + return this.getNotification().then((response) => { + if (!this.connectedUser) { + return; + } + this.connectedUser.setNotification(response.data.notification || []); + this.connectedUser.setAnnouncements(response.data.announcements || []); + localUserStore.saveUserConnected(this.connectedUser); + }).catch((err) => { + console.info(err); + }); + }).catch((err) => { - if(err instanceof ErrorConnectedError) { - throw err; - } console.log('err', err); this.connectedUser = null; + localUserStore.clearUserConnected(); throw new NotConnectedError('User not connected'); }); } @@ -181,6 +203,10 @@ class ConnectionManager { throw err; }) } + + private getNotification(){ + return Axios.get('http://pusher.workadventure.localhost/user/notifications/recent'); + } } export const connectionManager = new ConnectionManager(); diff --git a/front/src/Connexion/LocalUser.ts b/front/src/Connexion/LocalUser.ts index 62918814..287ccdfd 100644 --- a/front/src/Connexion/LocalUser.ts +++ b/front/src/Connexion/LocalUser.ts @@ -14,8 +14,18 @@ export class ConnectedUser { constructor( public readonly name:string, public readonly email: string, - public readonly notification: [], - public readonly announcements: [], + public readonly uuid:string, + public readonly jwtToken: string, + public notification?: [], + public announcements?: [], ) { } + + setNotification(notification: []){ + this.notification = notification; + } + + setAnnouncements(announcements: []){ + this.announcements = announcements; + } } diff --git a/front/src/Connexion/LocalUserStore.ts b/front/src/Connexion/LocalUserStore.ts index 8ac8c7b2..dd5b1191 100644 --- a/front/src/Connexion/LocalUserStore.ts +++ b/front/src/Connexion/LocalUserStore.ts @@ -1,4 +1,4 @@ -import {LocalUser} from "./LocalUser"; +import {ConnectedUser, LocalUser} from "./LocalUser"; const characterLayersKey = 'characterLayers'; const gameQualityKey = 'gameQuality'; @@ -6,7 +6,7 @@ const videoQualityKey = 'videoQuality'; //todo: add localstorage fallback class LocalUserStore { - + saveUser(localUser: LocalUser) { localStorage.setItem('localUser', JSON.stringify(localUser)); } @@ -56,6 +56,17 @@ class LocalUserStore { setVideoQualityValue(value: number): void { localStorage.setItem(videoQualityKey, '' + value); } + + clearUserConnected(){ + localStorage.removeItem('connectedUser'); + } + saveUserConnected(connectedUser: ConnectedUser) { + localStorage.setItem('connectedUser', JSON.stringify(connectedUser)); + } + getUserConnected(): ConnectedUser|null { + const data = localStorage.getItem('connectedUser'); + return data ? JSON.parse(data) : null; + } } export const localUserStore = new LocalUserStore(); \ No newline at end of file diff --git a/front/src/Exception/ErrorConnectedError.ts b/front/src/Exception/ErrorConnectedError.ts deleted file mode 100644 index 783e93ed..00000000 --- a/front/src/Exception/ErrorConnectedError.ts +++ /dev/null @@ -1 +0,0 @@ -export class ErrorConnectedError extends Error{} \ No newline at end of file diff --git a/front/src/Phaser/Menu/MenuScene.ts b/front/src/Phaser/Menu/MenuScene.ts index 4ce72f42..7ecd53b2 100644 --- a/front/src/Phaser/Menu/MenuScene.ts +++ b/front/src/Phaser/Menu/MenuScene.ts @@ -331,11 +331,6 @@ export class MenuScene extends Phaser.Scene { } if(errorForm){return;} - - gameLoginError.innerText = 'Login or password incorrect'; - gameLoginError.style.display = 'block'; - //TODO login user in back - connectionManager.userLogin(gameLoginEmail.value, gameLoginPassword.value).then(() => { this.closeGameLogin(); }).catch((err) => { diff --git a/pusher/src/Controller/AuthenticateController.ts b/pusher/src/Controller/AuthenticateController.ts index ccf09bb1..b714a5f4 100644 --- a/pusher/src/Controller/AuthenticateController.ts +++ b/pusher/src/Controller/AuthenticateController.ts @@ -19,6 +19,7 @@ export class AuthenticateController extends BaseController { this.userRegister(); this.userLogin(); this.forgotPassword(); + this.notificationRecent(); } //Try to login with an admin token @@ -250,4 +251,35 @@ export class AuthenticateController extends BaseController { })(); }); } + + private notificationRecent() { + this.App.options("/user/notifications/recent", (res: HttpResponse, req: HttpRequest) => { + this.addCorsHeaders(res); + res.end(); + }); + + this.App.get("/user/notifications/recent", (res: HttpResponse, req: HttpRequest) => { + + (async () => { + + res.onAborted(() => { + console.warn('Forgot password request was aborted'); + }); + + try { + const response = await adminApi.getNotification(); + res.writeStatus("200 OK"); + this.addCorsHeaders(res); + res.end(JSON.stringify(response.data)); + } catch (err) { + res.writeStatus("400 KO"); + this.addCorsHeaders(res); + res.end(JSON.stringify({ + message: err.message + })); + } + + })(); + }); + } } diff --git a/pusher/src/Services/AdminApi.ts b/pusher/src/Services/AdminApi.ts index 27dbab59..18b3e081 100644 --- a/pusher/src/Services/AdminApi.ts +++ b/pusher/src/Services/AdminApi.ts @@ -165,6 +165,13 @@ class AdminApi { headers: {"Authorization": `${ADMIN_API_TOKEN}`} }); } + + getNotification() { + return Axios.get(`${ADMIN_API_URL}/notifications/recent`, + { + headers: {"Authorization": `${ADMIN_API_TOKEN}`} + }); + } } export const adminApi = new AdminApi(); diff --git a/pusher/src/Services/JWTTokenManager.ts b/pusher/src/Services/JWTTokenManager.ts index 8abb0e45..015a69f2 100644 --- a/pusher/src/Services/JWTTokenManager.ts +++ b/pusher/src/Services/JWTTokenManager.ts @@ -6,8 +6,12 @@ import {adminApi, AdminApiData} from "../Services/AdminApi"; class JWTTokenManager { - public createJWTToken(userUuid: string) { - return Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '200d'}); //todo: add a mechanic to refresh or recreate token + public createJWTToken(userUuid: string, email?: string, name?: string) { + return Jwt.sign({ + userUuid: userUuid, + email: email || null, + name: name || null, + }, SECRET_KEY, {expiresIn: '200d'}); //todo: add a mechanic to refresh or recreate token } public async getUserUuidFromToken(token: unknown): Promise {