Skip to content

Commit 15327c0

Browse files
authored
Auth Middleware Refactors (#2092)
1 parent b5263f7 commit 15327c0

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

ppr-ui/src/middleware/01.redirect.global.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { Route } from 'vue-router'
22
import { RouteNames } from '@/enums'
33
import { SessionStorageKeys } from 'sbc-common-components/src/util/constants'
4-
import { useNavigation } from '@/composables'
5-
6-
const { goToRoute } = useNavigation()
74

85
/** Returns True if route requires authentication, else False. */
96
function requiresAuth (route: Route): boolean {
@@ -41,23 +38,15 @@ function isLoginSuccess (route: Route): boolean {
4138
export default defineNuxtRouteMiddleware( (to) => {
4239
if (isLoginSuccess(to)) {
4340
// this route is to verify login
44-
navigateTo({
45-
name: RouteNames.SIGN_IN,
46-
query: { redirect: to.query.redirect }
47-
})
48-
} else {
49-
if (requiresAuth(to) && !isAuthenticated()) {
50-
// this route needs authentication, so re-route to login
51-
navigateTo({ name: RouteNames.LOGIN, query: { redirect: to.fullPath } })
52-
// return goToRoute(RouteNames.LOGIN, { redirect: to.fullPath })
53-
} else {
54-
if (isLogin(to) && isAuthenticated()) {
55-
// this route is to dashboard after login
56-
navigateTo({ name: RouteNames.DASHBOARD })
57-
} else {
58-
// otherwise just proceed normally
59-
navigateTo()
60-
}
61-
}
41+
return navigateTo({ name: RouteNames.SIGN_IN, query: { redirect: to.query.redirect } })
42+
}
43+
44+
if (requiresAuth(to) && !isAuthenticated()) {
45+
// this route needs authentication, so re-route to login
46+
return navigateTo({ name: RouteNames.LOGIN, query: { redirect: to.path } })
47+
}
48+
49+
if (isLogin(to) && isAuthenticated()) {
50+
return navigateTo({ name: RouteNames.DASHBOARD })
6251
}
6352
})

0 commit comments

Comments
 (0)