-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add /session-sync for restoring sessions
- Loading branch information
1 parent
3aba343
commit f4c7514
Showing
3 changed files
with
96 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bigcommerce/catalyst-core": patch | ||
--- | ||
|
||
Support session sync endpoint |
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,87 @@ | ||
import { unstable_rethrow as rethrow, redirect } from 'next/navigation'; | ||
import { graphql } from '~/client/graphql'; | ||
import { client } from '~/client'; | ||
import { signIn } from '~/auth'; | ||
import { setCartId } from '~/lib/cookies/cart'; | ||
|
||
// GQL mutation for validating Session Sync JWT | ||
const ValidateSessionSyncJwt = graphql(` | ||
mutation ValidateSessionSyncJwt($jwt: String!) { | ||
validateSessionSyncJwt(jwt: $jwt) { | ||
errors { | ||
... on InvalidSessionSyncJwtError { | ||
errorType | ||
message | ||
} | ||
... on JwtTokenExpiredError { | ||
message | ||
} | ||
} | ||
content { | ||
cart { | ||
entityId | ||
} | ||
customer { | ||
entityId | ||
firstName | ||
lastName | ||
} | ||
customerAccessToken { | ||
value | ||
} | ||
redirectTo | ||
} | ||
} | ||
} | ||
`); | ||
|
||
export async function GET(request: Request) { | ||
const { searchParams } = new URL(request.url); | ||
const jwt = searchParams.get('jwt'); | ||
|
||
if (!jwt) { | ||
return redirect('/login?error=MissingToken'); | ||
} | ||
|
||
try { | ||
// Validate the session sync JWT | ||
const response = await client.fetch({ | ||
document: ValidateSessionSyncJwt, | ||
variables: { jwt }, | ||
fetchOptions: { cache: 'no-store' }, | ||
}); | ||
|
||
const data = response.data.validateSessionSyncJwt; | ||
if (data.errors?.length) { | ||
return redirect('/login?error=InvalidToken'); | ||
} | ||
|
||
// Restore cart | ||
if (data.content?.cart?.entityId) { | ||
await setCartId(data.content.cart.entityId); | ||
} | ||
|
||
// Restore the customer session | ||
// (signIn will re-validate an active session for next-auth) | ||
if (data.content?.customerAccessToken?.value) { | ||
await signIn('credentials', { | ||
type: 'jwt', | ||
jwt: data.content.customerAccessToken.value, | ||
redirectTo: data.content.redirectTo, | ||
}); | ||
} | ||
|
||
// Get relative redirectTo path from full URL | ||
const redirectTo = new URL(data.content?.redirectTo || '/').pathname; | ||
|
||
// Finally, redirect user to the redirectTo parameter or default page | ||
redirect(redirectTo); | ||
} catch (error) { | ||
rethrow(error); | ||
redirect('/login?error=InvalidToken'); | ||
} | ||
} | ||
|
||
export const runtime = 'edge'; | ||
export const dynamic = 'force-dynamic'; |
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