Skip to content

Commit 46ba055

Browse files
committed
Update auth-redirect.ts
1 parent fa0f6ca commit 46ba055

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/runtime/plugins/auth-redirect.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import type { Plugin } from '#app'
44
import { defineNuxtPlugin, addRouteMiddleware, defineNuxtRouteMiddleware, useRuntimeConfig, navigateTo } from '#imports'
55
import type { RouteLocationNormalized } from '#vue-router'
66

7+
function matchesAnyPattern(path: string, patterns: (string | undefined)[]): boolean {
8+
return patterns.some((pattern) => {
9+
if (!pattern) return false
10+
const regex = new RegExp(`^${pattern.replace(/\*/g, '.*')}$`)
11+
return regex.test(path)
12+
})
13+
}
14+
715
export default defineNuxtPlugin({
816
name: 'auth-redirect',
917
setup() {
@@ -15,21 +23,16 @@ export default defineNuxtPlugin({
1523

1624
// Redirect only on included routes (if defined)
1725
if (include && include.length > 0) {
18-
const isIncluded = include.some((path: string) => {
19-
const regex = new RegExp(`^${path.replace(/\*/g, '.*')}$`)
20-
return regex.test(to.path)
21-
})
22-
if (!isIncluded) {
26+
if (!matchesAnyPattern(to.path, include)) {
2327
return
2428
}
2529
}
2630

2731
// Do not redirect on login route, callback route and excluded routes
28-
const isExcluded = [...exclude ?? [], login, callback]?.some((path) => {
29-
const regex = new RegExp(`^${path.replace(/\*/g, '.*')}$`)
30-
return regex.test(to.path)
31-
})
32-
if (isExcluded) return
32+
const excludePatterns = [login, callback, ...(exclude ?? [])]
33+
if (matchesAnyPattern(to.path, excludePatterns)) {
34+
return
35+
}
3336

3437
const session = useSupabaseSession()
3538
if (!session.value) {

0 commit comments

Comments
 (0)