Skip to content

Commit 31454dd

Browse files
committed
refactor: extraire fonction qui determine si le param 'op' est dans le param next_url
1 parent 51fe74e commit 31454dd

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

front/src/routes/+layout.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { redirect } from "@sveltejs/kit";
44
import { get } from "svelte/store";
55
import type { LayoutLoad } from "./$types";
66
import { needToAcceptCgu } from "$lib/utils/cgu";
7-
import { ORIENTATION_JWT_QUERY_PARAM } from "$lib/consts";
7+
import { doesUrlHaveOrientationJwt } from "./auth/utils";
88

99
export const prerender = false;
1010

@@ -34,13 +34,9 @@ export const load: LayoutLoad = async ({ url }) => {
3434
currentUserInfo = get(userInfo);
3535
}
3636

37-
const nextParam = url.searchParams.get("next");
38-
const nextUrl = nextParam ? new URL(nextParam, url.origin) : null;
39-
const hasOpJwt =
40-
url.searchParams.has(ORIENTATION_JWT_QUERY_PARAM) ||
41-
nextUrl?.searchParams.has(ORIENTATION_JWT_QUERY_PARAM) === true;
37+
const hasOrientationJwt = doesUrlHaveOrientationJwt(url);
4238

43-
if (currentUserInfo && !hasOpJwt) {
39+
if (currentUserInfo && !hasOrientationJwt) {
4440
// ⚠ Il est nécessaire d'acceder à url.pathname ici pour que cette fonction `load`
4541
// soit rappelée quand l'URL change, sans quoi SvelteKit optimise l'appel.
4642
// Voir: https://kit.svelte.dev/docs/load#rerunning-load-functions
@@ -85,6 +81,6 @@ export const load: LayoutLoad = async ({ url }) => {
8581
}
8682

8783
return {
88-
hasOpJwt,
84+
hasOpJwt: hasOrientationJwt,
8985
};
9086
};

front/src/routes/auth/pc-callback/+page.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { CANONICAL_URL } from "$lib/env";
22
import { redirect } from "@sveltejs/kit";
3-
import { getNextPage } from "../utils";
4-
import { ORIENTATION_JWT_QUERY_PARAM } from "$lib/consts";
3+
import { getNextPage, doesUrlHaveOrientationJwt } from "../utils";
54

65
export const load = ({ url }) => {
76
const nextPage = getNextPage(url);
7+
const hasOrientationJwt = doesUrlHaveOrientationJwt(url);
8+
89
url.searchParams.delete("next");
910

1011
const qsParams = url.searchParams.toString();
1112

12-
const nextPageUrl = new URL(nextPage, CANONICAL_URL);
13-
1413
if (
1514
(url.searchParams.has("siret") || url.searchParams.has("safir")) &&
16-
!nextPageUrl.searchParams.has(ORIENTATION_JWT_QUERY_PARAM)
15+
!hasOrientationJwt
1716
) {
1817
// Lors de l'identification OIDC côté backend, il a été demandé
1918
// de proposer à l'utilisateur le rattachement vers une structure

front/src/routes/auth/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
import { ORIENTATION_JWT_QUERY_PARAM } from "$lib/consts";
2+
3+
export function doesUrlHaveOrientationJwt(url: URL): boolean {
4+
if (url.searchParams.has(ORIENTATION_JWT_QUERY_PARAM)) {
5+
return true;
6+
}
7+
const nextParam = url.searchParams.get("next");
8+
if (nextParam) {
9+
const nextUrl = new URL(nextParam, url.origin);
10+
return nextUrl.searchParams.has(ORIENTATION_JWT_QUERY_PARAM);
11+
}
12+
return false;
13+
}
14+
115
export function getNextPage(url: URL) {
216
const next = url.searchParams.get("next");
317
const basePath = next && next.startsWith("/") ? next : "/";

0 commit comments

Comments
 (0)