2019-11-22 07:50:15 +00:00
|
|
|
import { RequestHandler } from "../entities/RequestHandler";
|
2019-11-23 12:16:28 +00:00
|
|
|
import { AccountHelper } from "../helpers/AccountHelper";
|
2019-11-22 07:50:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Account controller
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class AccountController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to login user
|
|
|
|
*
|
|
|
|
* @param handler
|
|
|
|
*/
|
2019-11-23 12:16:28 +00:00
|
|
|
public static async LoginUser(handler: RequestHandler) {
|
2019-11-22 07:50:15 +00:00
|
|
|
|
2019-11-23 12:16:28 +00:00
|
|
|
// Get post data
|
|
|
|
const email = handler.postEmail("userMail");
|
|
|
|
const password = handler.postString("userPassword");
|
|
|
|
|
|
|
|
// TODO : add limits
|
|
|
|
|
|
|
|
// Authenticate user
|
|
|
|
const tokens = await AccountHelper.LoginUser(email, password, handler.getClientInfo());
|
|
|
|
|
|
|
|
if(tokens == null) {
|
|
|
|
// TODO : add limits
|
|
|
|
|
|
|
|
handler.error(401, "Invalid e-mail address / password !");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success
|
|
|
|
handler.send({
|
|
|
|
success: "User signed in!",
|
|
|
|
tokens: {
|
|
|
|
token1: tokens.token1,
|
|
|
|
token2: tokens.token2
|
|
|
|
}
|
|
|
|
});
|
2019-11-22 07:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 13:03:14 +00:00
|
|
|
/**
|
|
|
|
* Disconnect user
|
|
|
|
*
|
|
|
|
* @param handler
|
|
|
|
*/
|
|
|
|
public static async LogoutUser(handler: RequestHandler) {
|
|
|
|
|
|
|
|
await AccountHelper.DestroyUserTokens(handler.getClientInfo(),
|
|
|
|
handler.getUserId());
|
|
|
|
|
|
|
|
handler.success("User has been disconnected!");
|
|
|
|
}
|
|
|
|
|
2019-11-23 12:48:16 +00:00
|
|
|
/**
|
|
|
|
* Get current user ID
|
|
|
|
*
|
|
|
|
* @param handler
|
|
|
|
*/
|
|
|
|
public static CurrentUserID(handler: RequestHandler) {
|
|
|
|
handler.send({
|
|
|
|
userID: handler.getUserId()
|
|
|
|
});
|
|
|
|
}
|
2019-12-28 16:05:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check out whether an email is associated to an account
|
|
|
|
* or not
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async ExistsMail(h: RequestHandler) {
|
|
|
|
const email = h.postEmail("email");
|
|
|
|
|
|
|
|
h.send({
|
|
|
|
exists: await AccountHelper.ExistsEmail(email)
|
|
|
|
})
|
|
|
|
}
|
2019-11-22 07:50:15 +00:00
|
|
|
}
|