Hi, you can use a “beforeEnter” guard on the router to protect the pages that requires identification.
And personally, my login page is a popup, so when I detect in app.vue that the user is not identified anymore (with firebase, it’s simply listening to an event) I switch the login popup visibility.
Route.js
function checkAuth(to, from, resolve, reject) {
const { currentUser } = firebase.auth();
if (!currentUser) {
reject();
// If the forbidden page is not accessed in a visible view, we do not launch the animation
const router = this;
const { app } = router;
const thisView = router.view;
const isInCurrentView = thisView === app.views.current;
this.navigate('/auth-required/', {
animate: isInCurrentView,
reloadAll: true, // TODO : ideally, the parameters should be the same than those of the navigation that triggered the guard
});
} else resolve();
}
const routes = [{
path: '/',
component: HomePage,
beforeEnter: checkAuth,
alias: ['/home/'],
},
{
path: '/intro/',
component: IntroPage,
},
{
path: '/auth-required/',
component: AuthRequiredPage,
},
{
path: '/login-signup/',
component: LoginSignupPage,
},
{
path: '/contacts/',
component: ContactsPage,
beforeEnter: checkAuth,
},
{
path: '/contact/:contactId/',
component: ContactPage,
beforeEnter: checkAuth,
},
{
path: '/contact/',
component: ContactPage,
beforeEnter: checkAuth,
},
{
path: '/contact-edit/:contactId/',
component: ContactEditPage,
beforeEnter: checkAuth,
},
...
App.vue (inside mounted)
// We hide/show login form based on user authentication and if he has read the intro or not
let appLoaded = false; // See below for explanation
firebase.auth().onAuthStateChanged(() => {
const user = firebase.auth().currentUser;
this.showLoginPopup = !user;
console.log('views', this.$f7.views);
// We reload all the views if the user has changed
// When the app load the first time, the routes are already correct, so a reload is not necessary
// TODO: clearPreviousHistory does not work
if (appLoaded) {
const settingsReload = { reloadAll: true };
this.$f7.panel.close('left', false);
this.$f7.views.parameters.router.navigate('/parameters/', settingsReload);
this.$f7.views.home.router.navigate('/home/', settingsReload);
this.$f7.views.events.router.navigate('/events/', settingsReload);
this.$f7.views.contacts.router.navigate('/contacts/', settingsReload);
}
appLoaded = true;
});