|
|
@@ -1,5 +1,8 @@
|
|
|
import Page from "./Page.js";
|
|
|
import Elem from "../Elem.js";
|
|
|
+import EncryptionHandler from "../EncryptionHandler.js";
|
|
|
+import User from "../data/User.js";
|
|
|
+import Notifier from "../Notifier.js";
|
|
|
|
|
|
export default class Login extends Page{
|
|
|
constructor(){
|
|
|
@@ -8,15 +11,66 @@ export default class Login extends Page{
|
|
|
this.render();
|
|
|
}
|
|
|
|
|
|
- submit(){
|
|
|
+ async submit(){
|
|
|
event.preventDefault();
|
|
|
- console.log("submitting");
|
|
|
+
|
|
|
+ const email = this.container.querySelector(".email").value;
|
|
|
+ const password = this.container.querySelector(".password").value;
|
|
|
+
|
|
|
+ fetch("/api/user/salt", {
|
|
|
+ method: "POST",
|
|
|
+ headers: {
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ },
|
|
|
+ body: JSON.stringify({email})
|
|
|
+ })
|
|
|
+ .then(r=>r.json())
|
|
|
+ .then((response)=>{
|
|
|
+ if(response.error) throw response;
|
|
|
+
|
|
|
+ let hashPromise = EncryptionHandler.hashPassword(password, response.password_salt);
|
|
|
+ return Promise.all([hashPromise, email, response.password_salt]);
|
|
|
+ })
|
|
|
+ .then((data)=>{
|
|
|
+ return fetch("/api/user/login", {
|
|
|
+ method: "POST",
|
|
|
+ headers: {"Content-Type": "application/json"},
|
|
|
+ body: JSON.stringify({
|
|
|
+ password_hash: data[0],
|
|
|
+ email: data[1],
|
|
|
+ password_salt: data[2]
|
|
|
+ })
|
|
|
+ });
|
|
|
+ })
|
|
|
+ .then(r=>r.json())
|
|
|
+ .then((response)=>{
|
|
|
+ if(response.error) throw response;
|
|
|
+
|
|
|
+ window.user = new User(
|
|
|
+ response.id,
|
|
|
+ response.name,
|
|
|
+ response.email,
|
|
|
+ response.accounts
|
|
|
+ );
|
|
|
+ return EncryptionHandler.create(password, response.encryption_salt);
|
|
|
+ })
|
|
|
+ .then((encryptionHandler)=>{
|
|
|
+ window.encryptionHandler = encryptionHandler;
|
|
|
+ changePage("home");
|
|
|
+ })
|
|
|
+ .catch((err)=>{
|
|
|
+ if(err.error){
|
|
|
+ new Notifier("error", err.error.message);
|
|
|
+ }else{
|
|
|
+ new Notifier("error", "Something went wrong, try refreshing the page");
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
render(){
|
|
|
new Elem("form")
|
|
|
.addClass("standardForm")
|
|
|
- .onsubmit(this.submit)
|
|
|
+ .onsubmit(this.submit.bind(this))
|
|
|
.append(new Elem("h1")
|
|
|
.text("Login")
|
|
|
)
|
|
|
@@ -26,6 +80,8 @@ export default class Login extends Page{
|
|
|
.addClass("email")
|
|
|
.type("text")
|
|
|
.placeholder("Email")
|
|
|
+ .required()
|
|
|
+ .focus()
|
|
|
)
|
|
|
)
|
|
|
.append(new Elem("label")
|
|
|
@@ -34,6 +90,7 @@ export default class Login extends Page{
|
|
|
.addClass("password")
|
|
|
.type("password")
|
|
|
.placeholder("Password")
|
|
|
+ .required()
|
|
|
)
|
|
|
)
|
|
|
.append(new Elem("button")
|