Skip to content

Instantly share code, notes, and snippets.

@Alex-Wauters
Created April 14, 2019 15:36
Show Gist options
  • Save Alex-Wauters/1f270363a7c1b90a34a79749a653a828 to your computer and use it in GitHub Desktop.
Save Alex-Wauters/1f270363a7c1b90a34a79749a653a828 to your computer and use it in GitHub Desktop.
// Abbreviated version from https://github.com/Alex-Wauters/firebase-auth-azure-ad/blob/master/main.js
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
console.log('User is logged in');
startApp();
} else {
const url = window.location.href;
if (url.indexOf('jwt=') > -1) { // The minted firebase token from Auth function
const token = url.substr( url.indexOf('jwt=') + 4);
firebase.auth().signInWithCustomToken(token)
.then((user) => {
window.location.href="/";
})
.catch(function (error) {
// Handle Errors here.
});
} else { // User not authenticated and no custom token present. Redirect to Azure AD for authentication token
let redirectUrl = "/auth"; //TODO: Redirect to the URL of your auth function
if (window.location.port === "5000") { // Adjust the requested redirectUri for local development
redirectUrl = redirectUrl + "&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fauth";
}
window.location.href = redirectUrl;
}
}
});
function startApp() { //Load front-end application
new Vue({
el: '#app',
template: '<App/>',
components: {App}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment