Skip to content

Commit

Permalink
fix(seaside): #184 fix relogin on private page on refresh
Browse files Browse the repository at this point in the history
improve session refresh mecanism
  • Loading branch information
Marthym committed Nov 7, 2023
1 parent 6283cf0 commit aee1752
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
43 changes: 17 additions & 26 deletions seaside/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ import NotificationArea from '@/common/components/notificationArea/NotificationA
import { EventType } from '@/techwatch/model/EventType.enum';
import { Notification } from '@/services/notification/Notification.type';
import { registerNotificationListener, unregisterNotificationListener } from '@/layout/services/ServerEventService';
import { refresh } from '@/security/services/AuthenticationService';
import notificationService from '@/services/notification/NotificationService';
import { useStore } from 'vuex';
import { Store, useStore } from 'vuex';
import { UPDATE_MUTATION as STATS_UPDATE_MUTATION } from '@/techwatch/store/statistics/StatisticsConstants';
import {
HAS_ROLE_USER_GETTER,
LOGOUT_MUTATION,
UPDATE_MUTATION as USER_UPDATE_MUTATION,
} from '@/store/user/UserConstants';
import { HAS_ROLE_USER_GETTER } from '@/store/user/UserConstants';
import { UserState } from '@/store/user/user';
@Component({
components: {
Expand All @@ -47,27 +43,22 @@ import {
},
})
export default class App extends Vue {
private readonly store;
private readonly store: Store<UserState>;
mounted(): void {
refresh().subscribe({
next: session => {
this.store.commit(USER_UPDATE_MUTATION, session.user);
this.registerSessionNotifications();
},
error: () => {
this.store.commit(LOGOUT_MUTATION);
unregisterNotificationListener(EventType.NEWS_UPDATE, this.onServerMessage);
const unwatch = this.store.watch(
(state, getters) => getters[HAS_ROLE_USER_GETTER],
newValue => {
unwatch();
if (newValue) {
this.registerSessionNotifications();
}
});
},
});
if (this.store.state.user.isAuthenticated) {
this.registerSessionNotifications();
} else {
unregisterNotificationListener(EventType.NEWS_UPDATE, this.onServerMessage);
const unwatch = this.store.watch(
(state, getters) => getters[HAS_ROLE_USER_GETTER],
newValue => {
unwatch();
if (newValue) {
this.registerSessionNotifications();
}
});
}
}
private registerSessionNotifications(): void {
Expand Down
32 changes: 28 additions & 4 deletions seaside/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { routes as adminRoutes } from '@/administration/router';
import { routes as configRoutes } from '@/configuration/router';
import { routes as teamsRoutes } from '@/teams/router';
import { routes as techwatchRoutes } from '@/techwatch/router';
import { useStore } from 'vuex';
import { Store, useStore } from 'vuex';
import { UserState } from '@/store/user/user';
import { refresh } from '@/security/services/AuthenticationService';
import { LOGOUT_MUTATION, UPDATE_MUTATION as USER_UPDATE_MUTATION } from '@/store/user/UserConstants';

const LoginPage = () => import('@/pages/LoginPage.vue');

Expand All @@ -20,11 +22,33 @@ const router = createRouter({
],
} as RouterOptions);

function refreshSession(store: Store<UserState>): Promise<boolean> {
const isAuthenticated = useStore<UserState>().state.user.isAuthenticated;
if (isAuthenticated === undefined) {
return new Promise(resolve => {
refresh().subscribe({
next: session => {
store.commit(USER_UPDATE_MUTATION, session.user);
resolve(true);
},
error: () => {
store.commit(LOGOUT_MUTATION);
resolve(false);
},
});
});
} else {
return Promise.resolve(true);
}
}

router.beforeEach(async to => {
const isAuthenticated = await refreshSession(useStore<UserState>());
if (to.matched.some(record => record.meta.requiresAuth)) {
const isAuthenticated = useStore<UserState>().state.user.isAuthenticated;
if (isAuthenticated !== true) {
return { name: 'LoginPage', query: {redirect: to.path} };
console.debug('to', to);
console.debug('isAuthenticated', isAuthenticated);
if (!isAuthenticated) {
return { name: 'LoginPage', query: { redirect: to.path } };
}
}
});
Expand Down

0 comments on commit aee1752

Please sign in to comment.