Skip to content

Commit

Permalink
fix: use referrer from client
Browse files Browse the repository at this point in the history
  • Loading branch information
rmonnier9 committed Oct 4, 2024
1 parent bd01c51 commit 35e6258
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 83 deletions.
22 changes: 12 additions & 10 deletions components-ui/button-pro-connect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
'use client';

import { logConversionEvent } from '#utils/matomo';
import { usePathname } from 'next/navigation';
import React from 'react';

type IProps = {
shouldRedirectToReferer: boolean;
event: string;
pathForRedirection?: string;
};

const ButtonProConnect: React.FC<IProps> = ({
shouldRedirectToReferer = false,
event = 'BTN_DEFAULT',
pathForRedirection,
}) => {
const currentPath = usePathname();
const referrer = document?.referrer;
const isFromSite = referrer.indexOf('https://annuaire-entreprises') === 0;

const pathFrom =
shouldRedirectToReferer && isFromSite ? referrer : currentPath;

return (
<form action="/api/auth/agent-connect/login" method="get">
{pathForRedirection && (
<input
readOnly
hidden
aria-hidden
name="pathFrom"
value={pathForRedirection}
/>
{pathFrom && (
<input readOnly hidden aria-hidden name="pathFrom" value={pathFrom} />
)}
<div className="fr-connect-group">
<button
Expand Down
26 changes: 3 additions & 23 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import { getIronSession } from 'iron-session';
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

const shouldRedirect = (
path: string,
search: string,
url: string,
referer: string
) => {
const shouldRedirect = (path: string, search: string, url: string) => {
try {
if (path.startsWith('/entreprise/')) {
const sirenOrSiretSlug = extractSirenOrSiretSlugFromUrl(path);
Expand Down Expand Up @@ -49,19 +44,6 @@ const shouldRedirect = (
return new URL(`/entreprise/${sirenOrSiretParam}?redirected=1`, url);
}
}

if (path.startsWith('/lp/agent-public')) {
const isFromSite = referer.indexOf('https://annuaire-entreprises') === 0;

const searchParams = new URLSearchParams(search);
const hasNoPathFrom = !searchParams.has('pathFrom');

if (isFromSite && hasNoPathFrom) {
const refererUrl = new URL(referer);
const pathFrom = refererUrl.pathname;
return new URL(`/lp/agent-public?pathFrom=${pathFrom}`, url);
}
}
} catch (e) {
logErrorInSentry(
new Exception({
Expand All @@ -78,13 +60,10 @@ const shouldRedirect = (

// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
const requestHeaders = new Headers(request.headers);

const redirection = shouldRedirect(
request.nextUrl.pathname,
request.nextUrl.search,
request.url,
requestHeaders.get('referer') || ''
request.url
);

if (redirection) {
Expand All @@ -94,6 +73,7 @@ export async function middleware(request: NextRequest) {
/**
* siren redirection logging
*/
const requestHeaders = new Headers(request.headers);
const nextUrl = request.nextUrl;
const paramIsPresent = nextUrl.search.indexOf('redirected=1') > -1;

Expand Down
50 changes: 0 additions & 50 deletions pages/connexion/agent-public.tsx

This file was deleted.

0 comments on commit 35e6258

Please sign in to comment.