2024-09-26 00:47:08 +01:00
|
|
|
import ChangeUsernameViewModel from "../models/account/ChangeUsernameViewModel";
|
2024-09-19 00:41:40 +01:00
|
|
|
import LoginViewModel from "../models/account/LoginViewModel";
|
|
|
|
import RegisterViewModel from "../models/account/RegisterViewModel";
|
|
|
|
import Session from "../objects/Session";
|
|
|
|
import UserService from "../services/UserService";
|
|
|
|
import Controller from "./Controller"
|
|
|
|
|
|
|
|
export default class AccountController extends Controller {
|
|
|
|
public async Login_Get_AllowAnonymous() {
|
|
|
|
return this.view();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Login_Post_AllowAnonymous(loginViewModel: LoginViewModel) {
|
|
|
|
if (typeof(loginViewModel.username) !== "string" || typeof(loginViewModel.password) !== "string") {
|
|
|
|
return this.badRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await UserService.AuthenticateUser(loginViewModel.username, loginViewModel.password);
|
|
|
|
if (!user) {
|
|
|
|
loginViewModel.password = "";
|
|
|
|
loginViewModel.message = "Username or Password is incorrect";
|
|
|
|
|
|
|
|
return this.view(loginViewModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
Session.AssignUserSession(this.res, user);
|
|
|
|
|
|
|
|
return this.redirectToAction("index", "home");
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Register_Get_AllowAnonymous() {
|
|
|
|
return this.view();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Register_Post_AllowAnonymous(registerViewModel: RegisterViewModel) {
|
|
|
|
if (typeof(registerViewModel.username) !== "string" || typeof(registerViewModel.password) !== "string") {
|
|
|
|
return this.badRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
const username = registerViewModel.username.replaceAll("<", "<").replaceAll(">", ">");
|
2024-09-26 00:47:08 +01:00
|
|
|
if (!await UserService.CreateUser(0, username, registerViewModel.password)) {
|
|
|
|
registerViewModel.password = "";
|
|
|
|
registerViewModel.message = "Sorry! That username is already taken.";
|
|
|
|
|
|
|
|
return this.view(registerViewModel);
|
|
|
|
}
|
2024-09-19 00:41:40 +01:00
|
|
|
|
|
|
|
const user = await UserService.GetUserByUsername(username);
|
|
|
|
if (!user) {
|
|
|
|
registerViewModel.password = "";
|
|
|
|
registerViewModel.message = "Failed to create your account, please try again later.";
|
|
|
|
|
|
|
|
return this.view(registerViewModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
Session.AssignUserSession(this.res, user);
|
|
|
|
|
|
|
|
return this.redirectToAction("index", "home");
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Logout_Get_AllowAnonymous() {
|
|
|
|
Session.Clear(this.req.cookies, this.res);
|
|
|
|
|
|
|
|
return this.redirectToAction("index", "home");
|
|
|
|
}
|
2024-09-26 00:47:08 +01:00
|
|
|
|
|
|
|
public async ChangeUsername_Get(changeUsernameViewModel:ChangeUsernameViewModel) {
|
|
|
|
return this.view(changeUsernameViewModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async ChangeUsername_Post(changeUsernameViewModel:ChangeUsernameViewModel) {
|
|
|
|
if (typeof(changeUsernameViewModel.username) !== "string") {
|
|
|
|
return this.badRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await UserService.SaveUsername(this.session.userId, changeUsernameViewModel.username);
|
|
|
|
if (!user) {
|
|
|
|
changeUsernameViewModel.message = "Sorry! That username is already taken.";
|
|
|
|
return this.view(changeUsernameViewModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.redirectToAction("index", "home");
|
|
|
|
}
|
2024-09-19 00:41:40 +01:00
|
|
|
}
|