Create URL to get user notification
This commit is contained in:
parent
53b96d61fe
commit
69bf416dd0
|
@ -7,7 +7,6 @@ import {localUserStore} from "./LocalUserStore";
|
||||||
import {ConnectedUser, LocalUser} from "./LocalUser";
|
import {ConnectedUser, LocalUser} from "./LocalUser";
|
||||||
import {Room} from "./Room";
|
import {Room} from "./Room";
|
||||||
import {NotConnectedError} from "../Exception/NotConnectedError";
|
import {NotConnectedError} from "../Exception/NotConnectedError";
|
||||||
import {ErrorConnectedError} from "../Exception/ErrorConnectedError";
|
|
||||||
|
|
||||||
const URL_ROOM_STARTED = '/Floor0/floor0.json';
|
const URL_ROOM_STARTED = '/Floor0/floor0.json';
|
||||||
|
|
||||||
|
@ -19,7 +18,8 @@ class ConnectionManager {
|
||||||
private connectedUser?: ConnectedUser|null;
|
private connectedUser?: ConnectedUser|null;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
//this.userLogin();
|
this.connectedUser = localUserStore.getUserConnected();
|
||||||
|
this.getNotification();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -124,17 +124,39 @@ class ConnectionManager {
|
||||||
* @param email
|
* @param email
|
||||||
* @param password
|
* @param password
|
||||||
*/
|
*/
|
||||||
public userLogin(email: string, password: string){
|
public userLogin(email: string, password: string) {
|
||||||
//Verify spark session
|
//Verify spark session
|
||||||
//TODO change url addresse
|
//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) => {
|
}).catch((err) => {
|
||||||
if(err instanceof ErrorConnectedError) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
console.log('err', err);
|
console.log('err', err);
|
||||||
this.connectedUser = null;
|
this.connectedUser = null;
|
||||||
|
localUserStore.clearUserConnected();
|
||||||
throw new NotConnectedError('User not connected');
|
throw new NotConnectedError('User not connected');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -181,6 +203,10 @@ class ConnectionManager {
|
||||||
throw err;
|
throw err;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getNotification(){
|
||||||
|
return Axios.get('http://pusher.workadventure.localhost/user/notifications/recent');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const connectionManager = new ConnectionManager();
|
export const connectionManager = new ConnectionManager();
|
||||||
|
|
|
@ -14,8 +14,18 @@ export class ConnectedUser {
|
||||||
constructor(
|
constructor(
|
||||||
public readonly name:string,
|
public readonly name:string,
|
||||||
public readonly email: string,
|
public readonly email: string,
|
||||||
public readonly notification: [],
|
public readonly uuid:string,
|
||||||
public readonly announcements: [],
|
public readonly jwtToken: string,
|
||||||
|
public notification?: [],
|
||||||
|
public announcements?: [],
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setNotification(notification: []){
|
||||||
|
this.notification = notification;
|
||||||
|
}
|
||||||
|
|
||||||
|
setAnnouncements(announcements: []){
|
||||||
|
this.announcements = announcements;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {LocalUser} from "./LocalUser";
|
import {ConnectedUser, LocalUser} from "./LocalUser";
|
||||||
|
|
||||||
const characterLayersKey = 'characterLayers';
|
const characterLayersKey = 'characterLayers';
|
||||||
const gameQualityKey = 'gameQuality';
|
const gameQualityKey = 'gameQuality';
|
||||||
|
@ -6,7 +6,7 @@ const videoQualityKey = 'videoQuality';
|
||||||
|
|
||||||
//todo: add localstorage fallback
|
//todo: add localstorage fallback
|
||||||
class LocalUserStore {
|
class LocalUserStore {
|
||||||
|
|
||||||
saveUser(localUser: LocalUser) {
|
saveUser(localUser: LocalUser) {
|
||||||
localStorage.setItem('localUser', JSON.stringify(localUser));
|
localStorage.setItem('localUser', JSON.stringify(localUser));
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,17 @@ class LocalUserStore {
|
||||||
setVideoQualityValue(value: number): void {
|
setVideoQualityValue(value: number): void {
|
||||||
localStorage.setItem(videoQualityKey, '' + value);
|
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();
|
export const localUserStore = new LocalUserStore();
|
|
@ -1 +0,0 @@
|
||||||
export class ErrorConnectedError extends Error{}
|
|
|
@ -331,11 +331,6 @@ export class MenuScene extends Phaser.Scene {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(errorForm){return;}
|
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(() => {
|
connectionManager.userLogin(gameLoginEmail.value, gameLoginPassword.value).then(() => {
|
||||||
this.closeGameLogin();
|
this.closeGameLogin();
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
|
|
|
@ -19,6 +19,7 @@ export class AuthenticateController extends BaseController {
|
||||||
this.userRegister();
|
this.userRegister();
|
||||||
this.userLogin();
|
this.userLogin();
|
||||||
this.forgotPassword();
|
this.forgotPassword();
|
||||||
|
this.notificationRecent();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Try to login with an admin token
|
//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
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
})();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,6 +165,13 @@ class AdminApi {
|
||||||
headers: {"Authorization": `${ADMIN_API_TOKEN}`}
|
headers: {"Authorization": `${ADMIN_API_TOKEN}`}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getNotification() {
|
||||||
|
return Axios.get(`${ADMIN_API_URL}/notifications/recent`,
|
||||||
|
{
|
||||||
|
headers: {"Authorization": `${ADMIN_API_TOKEN}`}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const adminApi = new AdminApi();
|
export const adminApi = new AdminApi();
|
||||||
|
|
|
@ -6,8 +6,12 @@ import {adminApi, AdminApiData} from "../Services/AdminApi";
|
||||||
|
|
||||||
class JWTTokenManager {
|
class JWTTokenManager {
|
||||||
|
|
||||||
public createJWTToken(userUuid: string) {
|
public createJWTToken(userUuid: string, email?: string, name?: string) {
|
||||||
return Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '200d'}); //todo: add a mechanic to refresh or recreate token
|
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> {
|
public async getUserUuidFromToken(token: unknown): Promise<string> {
|
||||||
|
|
Loading…
Reference in a new issue