Skip to content

Commit

Permalink
Auth Middleware Refactors (#2092)
Browse files Browse the repository at this point in the history
  • Loading branch information
cameron-eyds authored Dec 18, 2024
1 parent b5263f7 commit 15327c0
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions ppr-ui/src/middleware/01.redirect.global.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { Route } from 'vue-router'
import { RouteNames } from '@/enums'
import { SessionStorageKeys } from 'sbc-common-components/src/util/constants'
import { useNavigation } from '@/composables'

const { goToRoute } = useNavigation()

/** Returns True if route requires authentication, else False. */
function requiresAuth (route: Route): boolean {
Expand Down Expand Up @@ -41,23 +38,15 @@ function isLoginSuccess (route: Route): boolean {
export default defineNuxtRouteMiddleware( (to) => {
if (isLoginSuccess(to)) {
// this route is to verify login
navigateTo({
name: RouteNames.SIGN_IN,
query: { redirect: to.query.redirect }
})
} else {
if (requiresAuth(to) && !isAuthenticated()) {
// this route needs authentication, so re-route to login
navigateTo({ name: RouteNames.LOGIN, query: { redirect: to.fullPath } })
// return goToRoute(RouteNames.LOGIN, { redirect: to.fullPath })
} else {
if (isLogin(to) && isAuthenticated()) {
// this route is to dashboard after login
navigateTo({ name: RouteNames.DASHBOARD })
} else {
// otherwise just proceed normally
navigateTo()
}
}
return navigateTo({ name: RouteNames.SIGN_IN, query: { redirect: to.query.redirect } })
}

if (requiresAuth(to) && !isAuthenticated()) {
// this route needs authentication, so re-route to login
return navigateTo({ name: RouteNames.LOGIN, query: { redirect: to.path } })
}

if (isLogin(to) && isAuthenticated()) {
return navigateTo({ name: RouteNames.DASHBOARD })
}
})

0 comments on commit 15327c0

Please sign in to comment.