Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth MiddleWare Redirect Fixes #2092

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })
}
})
Loading