Skip to content

Commit

Permalink
Merge pull request #135 from manchenkoff/109-enhancement-redirects-st…
Browse files Browse the repository at this point in the history
…ack-up

fix: trim trailing slash on redirects
  • Loading branch information
manchenkoff authored Aug 8, 2024
2 parents 971f9bd + d24014d commit d05e8d5
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
25 changes: 14 additions & 11 deletions src/runtime/composables/useSanctumAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useSanctumUser } from './useSanctumUser';
import { navigateTo, useNuxtApp, useRoute } from '#app';
import { useSanctumConfig } from './useSanctumConfig';
import { useSanctumAppConfig } from './useSanctumAppConfig';
import { trimTrailingSlash } from '../utils/formatter';

export interface SanctumAuth<T> {
user: Ref<T | null>;
Expand Down Expand Up @@ -45,6 +46,7 @@ export const useSanctumAuth = <T>(): SanctumAuth<T> => {
*/
async function login(credentials: Record<string, any>) {
const currentRoute = useRoute();
const currentPath = trimTrailingSlash(currentRoute.path);

if (isAuthenticated.value === true) {
if (options.redirectIfAuthenticated === false) {
Expand All @@ -53,13 +55,13 @@ export const useSanctumAuth = <T>(): SanctumAuth<T> => {

if (
options.redirect.onLogin === false ||
options.redirect.onLogin === currentRoute.path
options.redirect.onLogin === currentPath
) {
return;
}

await nuxtApp.runWithContext(() =>
navigateTo(options.redirect.onLogin as string)
await nuxtApp.runWithContext(
async () => await navigateTo(options.redirect.onLogin as string)
);
}

Expand All @@ -77,9 +79,9 @@ export const useSanctumAuth = <T>(): SanctumAuth<T> => {
if (options.redirect.keepRequestedRoute) {
const requestedRoute = currentRoute.query.redirect;

if (requestedRoute && requestedRoute !== currentRoute.path) {
await nuxtApp.runWithContext(() =>
navigateTo(requestedRoute as string)
if (requestedRoute && requestedRoute !== currentPath) {
await nuxtApp.runWithContext(
async () => await navigateTo(requestedRoute as string)
);

return;
Expand All @@ -93,8 +95,8 @@ export const useSanctumAuth = <T>(): SanctumAuth<T> => {
return;
}

await nuxtApp.runWithContext(() =>
navigateTo(options.redirect.onLogin as string)
await nuxtApp.runWithContext(
async () => await navigateTo(options.redirect.onLogin as string)
);
}

Expand All @@ -107,6 +109,7 @@ export const useSanctumAuth = <T>(): SanctumAuth<T> => {
}

const currentRoute = useRoute();
const currentPath = trimTrailingSlash(currentRoute.path);

await client(options.endpoints.logout, { method: 'post' });

Expand All @@ -118,13 +121,13 @@ export const useSanctumAuth = <T>(): SanctumAuth<T> => {

if (
options.redirect.onLogout === false ||
currentRoute.path === options.redirect.onLogout
currentPath === options.redirect.onLogout
) {
return;
}

await nuxtApp.runWithContext(() =>
navigateTo(options.redirect.onLogout as string)
await nuxtApp.runWithContext(
async () => await navigateTo(options.redirect.onLogout as string)
);
}

Expand Down
7 changes: 5 additions & 2 deletions src/runtime/httpFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ export function createHttpClient(logger: ConsolaInstance): $Fetch {
options.redirectIfUnauthenticated &&
options.redirect.onAuthOnly
) {
await nuxtApp.runWithContext(() =>
navigateTo(options.redirect.onAuthOnly as string)
await nuxtApp.runWithContext(
async () =>
await navigateTo(
options.redirect.onAuthOnly as string
)
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/interceptors/cookie/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export default async function handleResponseHeaders(

// follow redirects on client
if (ctx.response.redirected) {
await app.runWithContext(() => navigateTo(ctx.response!.url));
await app.runWithContext(
async () => await navigateTo(ctx.response!.url)
);
}
}
3 changes: 2 additions & 1 deletion src/runtime/middleware/sanctum.auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineNuxtRouteMiddleware, navigateTo, createError } from '#app';
import type { RouteLocationRaw } from 'vue-router';
import { useSanctumConfig } from '../composables/useSanctumConfig';
import { useSanctumAuth } from '../composables/useSanctumAuth';
import { trimTrailingSlash } from '../utils/formatter';

export default defineNuxtRouteMiddleware((to) => {
const options = useSanctumConfig();
Expand All @@ -20,7 +21,7 @@ export default defineNuxtRouteMiddleware((to) => {
const redirect: RouteLocationRaw = { path: endpoint };

if (options.redirect.keepRequestedRoute) {
redirect.query = { redirect: to.fullPath };
redirect.query = { redirect: trimTrailingSlash(to.fullPath) };
}

return navigateTo(redirect, { replace: true });
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/middleware/sanctum.global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineNuxtRouteMiddleware, navigateTo } from '#app';
import type { RouteLocationRaw } from 'vue-router';
import { useSanctumConfig } from '../composables/useSanctumConfig';
import { useSanctumAuth } from '../composables/useSanctumAuth';
import { trimTrailingSlash } from '../utils/formatter';

export default defineNuxtRouteMiddleware((to) => {
const options = useSanctumConfig();
Expand Down Expand Up @@ -36,7 +37,8 @@ export default defineNuxtRouteMiddleware((to) => {
}

const isPageForGuestsOnly =
to.path === loginPage || to.meta.sanctum?.guestOnly === true;
trimTrailingSlash(to.path) === loginPage ||
to.meta.sanctum?.guestOnly === true;

if (isAuthenticated.value === true) {
if (isPageForGuestsOnly) {
Expand All @@ -53,7 +55,7 @@ export default defineNuxtRouteMiddleware((to) => {
const redirect: RouteLocationRaw = { path: loginPage };

if (options.redirect.keepRequestedRoute) {
redirect.query = { redirect: to.fullPath };
redirect.query = { redirect: trimTrailingSlash(to.fullPath) };
}

return navigateTo(redirect, { replace: true });
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/utils/formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function trimTrailingSlash(path: string): string {
return path.replace(/\/$/, '');
}

0 comments on commit d05e8d5

Please sign in to comment.