Create URL to get user notification

This commit is contained in:
Gregoire Parant 2020-12-22 11:15:18 +01:00
parent 53b96d61fe
commit 69bf416dd0
8 changed files with 104 additions and 20 deletions

View file

@ -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();

View file

@ -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;
}
}

View file

@ -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();

View file

@ -1 +0,0 @@
export class ErrorConnectedError extends Error{}

View file

@ -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) => {

View file

@ -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
}));
}
})();
});
}
}

View file

@ -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();

View file

@ -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<string> {