MC/website/node/public/login.js

13 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let mode='login';const form=document.querySelector('#account-form'),status=document.querySelector('#account-status'),submit=document.querySelector('#account-submit');
const bioField=document.createElement('label'),bio=document.createElement('textarea'),ageField=document.createElement('label'),age=document.createElement('select');
bioField.textContent='Biography · required · 10–1,000 characters';bio.name='bio';bio.minLength=10;bio.maxLength=1000;bio.rows=4;bioField.append(bio);
ageField.textContent='Age category';age.name='ageBand';for(const [value,label]of [['','Choose your age category'],['under15','Under 15'],['15to17','15–17'],['18plus','18 or older']]){const option=document.createElement('option');option.value=value;option.textContent=label;age.append(option);}ageField.append(age);
form.insertBefore(ageField,document.querySelector('#account-note'));form.insertBefore(bioField,document.querySelector('#account-note'));
function registrationFields(){const registering=mode==='register';bioField.hidden=ageField.hidden=bio.disabled=age.disabled=!registering;bio.required=age.required=registering;bio.setCustomValidity('');}
bio.addEventListener('input',()=>bio.setCustomValidity(mode==='register'&&bio.value.trim().length<10?'Write a biography of at least 10 characters.':''));registrationFields();
function change(next){mode=next;for(const type of ['login','register'])document.querySelector('#'+type+'-tab').setAttribute('aria-pressed',String(next===type));form.elements.password.autocomplete=mode==='register'?'new-password':'current-password';form.elements.password.minLength=mode==='register'?12:1;form.elements.password.placeholder=mode==='register'?'At least 12 characters':'Your existing password';submit.textContent=mode==='register'?'Create account →':'Sign in →';document.querySelector('#account-note').textContent=mode==='register'?'Choose 3–32 letters, numbers or underscores, and a password of at least 12 characters.':'Use your DuneRealms website account.';status.textContent='';}
for(const type of ['login','register'])document.querySelector('#'+type+'-tab').addEventListener('click',()=>{change(type);registrationFields();});
function destination(){const value=new URLSearchParams(location.search).get('return_to');return value&&value.startsWith('/')&&!value.startsWith('//')&&!/[\\\r\n]/.test(value)?value:'/portal.html';}
form.addEventListener('submit',async event=>{event.preventDefault();submit.disabled=true;status.textContent='Please wait…';try{const response=await fetch('/api/auth/'+mode,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(Object.fromEntries(new FormData(form)))});const result=await response.json();if(!response.ok)throw Error(result.message||'Sign-in failed.');location.assign(destination());}catch(error){status.textContent=error.message;}finally{submit.disabled=false;}});
document.querySelector('#account-logout').addEventListener('click',async()=>{await fetch('/api/auth/logout',{method:'POST'});location.reload();});
fetch('/api/auth/session').then(r=>r.json()).then(session=>{if(session.authenticated){status.textContent='Signed in as '+session.user.username+'.';document.querySelector('#account-logout').hidden=false;}}).catch(()=>{});