/* Cognito-backed auth. Depends on amazon-cognito-identity-js (loaded via CDN * in each page's ) and window.MILITIA_CONFIG (config.js). Session * tokens live only in Cognito's own localStorage-backed storage; we never * touch raw passwords or tokens ourselves beyond reading the id token to * attach as a Bearer header on API calls. */ (function () { function pool() { return new AmazonCognitoIdentity.CognitoUserPool({ UserPoolId: window.MILITIA_CONFIG.cognitoUserPoolId, ClientId: window.MILITIA_CONFIG.cognitoClientId }); } function register(email, password) { return new Promise(function (resolve, reject) { var attrs = [new AmazonCognitoIdentity.CognitoUserAttribute({ Name: 'email', Value: email })]; pool().signUp(email, password, attrs, null, function (err, result) { if (err) return reject(err); resolve(result); }); }); } function confirmRegistration(email, code) { return new Promise(function (resolve, reject) { var user = new AmazonCognitoIdentity.CognitoUser({ Username: email, Pool: pool() }); user.confirmRegistration(code, true, function (err, result) { if (err) return reject(err); resolve(result); }); }); } function resendCode(email) { return new Promise(function (resolve, reject) { var user = new AmazonCognitoIdentity.CognitoUser({ Username: email, Pool: pool() }); user.resendConfirmationCode(function (err, result) { if (err) return reject(err); resolve(result); }); }); } function login(email, password) { return new Promise(function (resolve, reject) { var authDetails = new AmazonCognitoIdentity.AuthenticationDetails({ Username: email, Password: password }); var user = new AmazonCognitoIdentity.CognitoUser({ Username: email, Pool: pool() }); user.authenticateUser(authDetails, { onSuccess: function (session) { resolve(session); }, onFailure: function (err) { reject(err); } }); }); } function logout() { var user = pool().getCurrentUser(); if (user) user.signOut(); } function getSession() { return new Promise(function (resolve) { var user = pool().getCurrentUser(); if (!user) return resolve(null); user.getSession(function (err, session) { if (err || !session || !session.isValid()) return resolve(null); resolve(session); }); }); } function getIdToken() { return getSession().then(function (session) { return session ? session.getIdToken().getJwtToken() : null; }); } function reflectAuthUi() { getSession().then(function (session) { var isIn = !!session; document.querySelectorAll('[data-auth-visible="in"]').forEach(function (el) { el.style.display = isIn ? '' : 'none'; }); document.querySelectorAll('[data-auth-visible="out"]').forEach(function (el) { el.style.display = isIn ? 'none' : ''; }); if (document.body.getAttribute('data-require-auth') === 'true' && !isIn) { window.location.href = '/login.html?next=' + encodeURIComponent(window.location.pathname); } if (document.body.getAttribute('data-redirect-if-authed') === 'true' && isIn) { window.location.href = '/dashboard.html'; } }); } window.MilitiaAuth = { register: register, confirmRegistration: confirmRegistration, resendCode: resendCode, login: login, logout: logout, getSession: getSession, getIdToken: getIdToken, reflectAuthUi: reflectAuthUi }; })();