www.spaceplanner.app

Web client to the spaceplanner API
git clone git://jacobedwards.org/www.spaceplanner.app
Log | Files | Refs

main.js (1474B)


      1 import * as api from "/lib/api.js"
      2 import * as etc from "/lib/etc.js"
      3 import * as ui from "/lib/ui.js"
      4 
      5 etc.handle_wrap(init)
      6 
      7 function init() {
      8 	if (api.authorized()) {
      9 		// Maybe don't do this?
     10 		window.location.href = "/floorplans"
     11 		return
     12 	}
     13 
     14 	etc.bar()
     15 
     16 	let email_input = document.getElementById("email")
     17 	let email_strict_input = document.getElementById("email-strict")
     18 
     19 	let username_input = ui.usernameInput()
     20 	document.getElementById("username").replaceWith(username_input)
     21 
     22 	let password_input = ui.passwordInput({ new: true })
     23 	document.getElementById("password").replaceWith(password_input)
     24 
     25 	if (!email_input || !email_strict_input || !username_input || !password_input) {
     26 		throw new Error("Unable to select email, username and password fields")
     27 	}
     28 
     29 	let form = document.getElementById("register")
     30 	if (!form) {
     31 		throw new Error("Unable to select registration form")
     32 	}
     33 	form.addEventListener("submit", function(event) {
     34 		event.preventDefault()
     35 		api.register(username_input.value, password_input.value, email_input.value,
     36 		    { email_policy: email_strict_input.checked })
     37 			.then(function() {
     38 				api.login(username_input.value, password_input.value)
     39 					.then(function() {
     40 						window.location.href = "/settings/verify-email"
     41 					})
     42 					.catch(function(err) {
     43 						console.error("Created user but was unable to login")
     44 						window.location.href = "/login"
     45 					})
     46 			})
     47 			.catch(function (error) {
     48 				return etc.error(error, form)
     49 			})
     50 	})
     51 }