Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expires returnUrl param after a successful login #17

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/app/@typing/Settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ declare module 'App' {
* Read more at {@link https://docs.commercelayer.io/core/authentication/client-credentials#password}, {@link https://docs.commercelayer.io/core/authentication/password}
*/
customerAccessToken?: string
/**
* Access Token expiry date for a sales channel API credentials obtained using login password flow.
* Read more at {@link https://docs.commercelayer.io/core/authentication/client-credentials#password}, {@link https://docs.commercelayer.io/core/authentication/password}
*/
customerAccessTokenExpires?: string
/**
* Organization slug.
* Read more at {@link https://docs.commercelayer.io/core/v/api-reference/organization/object}.
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/components/forms/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ export const LoginForm = (): JSX.Element => {
.then((tokenData) => {
if (tokenData.accessToken != null) {
redirectToReturnUrl({
scope: tokenData.scope,
accessToken: tokenData.accessToken,
scope: tokenData.scope
expires: tokenData.expires.toISOString()
})
} else {
form.setError('root', {
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/components/forms/SignUpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ export const SignUpForm = (): JSX.Element => {
.then((tokenData) => {
if (tokenData.accessToken != null) {
redirectToReturnUrl({
scope: tokenData.scope,
accessToken: tokenData.accessToken,
scope: tokenData.scope
expires: tokenData.expires.toISOString()
})
}
})
Expand Down
9 changes: 7 additions & 2 deletions packages/app/src/utils/redirectToReturnUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { getParamFromUrl } from '#utils/getParamFromUrl'
import type { Settings } from 'App'

interface RedirectToReturnUrlConfig {
accessToken: Settings['accessToken']
scope: Settings['scope']
accessToken: Settings['customerAccessToken']
expires: Settings['customerAccessTokenExpires']
}

/**
Expand All @@ -15,15 +16,19 @@ interface RedirectToReturnUrlConfig {
* @param scope - String specified during the authentication flow to restrict the scope of obtained access token to a market and/or to a stock location.
*/
export const redirectToReturnUrl = ({
scope,
accessToken,
scope
expires
}: RedirectToReturnUrlConfig): void => {
const returnUrl = getParamFromUrl('returnUrl')
if (returnUrl != null && window !== undefined) {
const topWindow = isEmbedded() ? window.parent : window
const url = new URL(returnUrl)
url.searchParams.append('accessToken', accessToken ?? '')
url.searchParams.append('scope', scope)
if (expires != null) {
url.searchParams.append('expires', expires)
}
topWindow.location.href = url.href
}
}