-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[auth] Fixe les boucles de redirection + l'état d'utilisateur connect…
…é sans DCP
- Loading branch information
Showing
6 changed files
with
85 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/api/src/utilisateurs/shared/data_access/dcp.fetch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import {DBClient} from '../../../typeUtils'; | ||
|
||
export const dcpFetch = async ({ | ||
dbClient, | ||
user_id, | ||
}: { | ||
dbClient: DBClient; | ||
user_id: string; | ||
}) => { | ||
const {data} = await dbClient | ||
.from('dcp') | ||
.select('user_id,nom,prenom,telephone,cgu_acceptees_le') | ||
.match({user_id}); | ||
|
||
return data?.length ? data[0] : null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,49 @@ | ||
import {getAppBaseUrl, restoreSessionFromAuthTokens} from '@tet/api'; | ||
import {dcpFetch} from '@tet/api/dist/src/utilisateurs/shared/data_access/dcp.fetch'; | ||
import {RedirectType, redirect, useRouter} from 'next/navigation'; | ||
import {useEffect} from 'react'; | ||
import {restoreSessionFromAuthTokens} from '@tet/api'; | ||
import {supabase} from 'src/clientAPI'; | ||
|
||
// redirige sur l'url donnée si la session de l'utilisateur peut être restaurée depuis les cookies | ||
export const useRedirectTo = (redirectTo: string) => { | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
const restore = async () => { | ||
const ret = await restoreSessionFromAuthTokens(supabase); | ||
if (ret?.data?.session) { | ||
document.location.replace(redirectTo); | ||
const sessionData = await restoreSessionFromAuthTokens(supabase); | ||
const user = sessionData?.data.user; | ||
|
||
if (!user) { | ||
return; | ||
} | ||
|
||
const dcpData = await dcpFetch({ | ||
dbClient: supabase, | ||
user_id: user.id, | ||
}); | ||
|
||
if (user && !dcpData) { | ||
const searchParams = new URLSearchParams({redirect_to: redirectTo}); | ||
router.replace(`/signup?view=etape3&${searchParams}`); | ||
return; | ||
} | ||
|
||
if (redirectTo.startsWith('/')) { | ||
const url = getAppBaseUrl(window.location.hostname) + redirectTo; | ||
router.replace(url); | ||
return; | ||
} | ||
|
||
if (nestPasUneUrlAuth(redirectTo)) { | ||
router.replace(redirectTo); | ||
return; | ||
} | ||
}; | ||
restore(); | ||
}, [redirectTo]); | ||
}, [redirectTo, router]); | ||
}; | ||
|
||
function nestPasUneUrlAuth(url: string) { | ||
const authOrigin = window.document.location.origin; | ||
return !url.startsWith(authOrigin); | ||
} |