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

@ -16,6 +16,9 @@ export class AuthenticateController extends BaseController {
this.register();
this.verify();
this.anonymLogin();
this.userRegister();
this.userLogin();
this.forgotPassword();
}
//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 ALLOW_ARTILLERY = process.env.ALLOW_ARTILLERY ? process.env.ALLOW_ARTILLERY == 'true' : false;
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 MAX_USERS_PER_ROOM = parseInt(process.env.MAX_USERS_PER_ROOM || '') || 600;
const CPU_OVERHEAT_THRESHOLD = Number(process.env.CPU_OVERHEAT_THRESHOLD) || 80;

View file

@ -110,6 +110,61 @@ class AdminApi {
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();