Skip to content

Commit

Permalink
Merge pull request #268 from wallotapp/201-redirect-to-the-dest-url-q…
Browse files Browse the repository at this point in the history
…uery-param-in-the-sso-site

Redirect to the dest URL query param in the SSO site
  • Loading branch information
kamarmack authored Feb 11, 2025
2 parents 6d63717 + 8ac12a8 commit 46ed926
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
10 changes: 8 additions & 2 deletions functions/rest-api/lib/app/wallot/users/registerUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { scheduleActivationReminderEmails } from './scheduleActivationReminderEm
export const registerUser = async ({
email,
password,
redirect_uri,
username,
}: RegisterUserParams): Promise<FunctionResponse<RegisterUserResponse>> => {
// Check that the username is unique and email is not already registered
Expand Down Expand Up @@ -82,12 +83,17 @@ export const registerUser = async ({
const customToken = await auth.createCustomToken(firebaseUser.uid);

// Construct the redirect URL using custom token
const redirectUri = getHomeSiteRoute({
const defaultRedirectURI = getHomeSiteRoute({
includeOrigin: true,
origin: siteOriginByTarget.HOME_SITE,
queryParams: { client_token: customToken },
routeStaticId: 'HOME_SITE__/GET_STARTED',
});
const redirectURI = redirect_uri
? redirect_uri.includes('?')
? `${redirect_uri}&client_token=${customToken}`
: `${redirect_uri}?client_token=${customToken}`
: defaultRedirectURI;

// Construct the post-response callback
const onFinished = async () => {
Expand Down Expand Up @@ -115,7 +121,7 @@ export const registerUser = async ({
};

return {
json: { custom_token: customToken, redirect_uri: redirectUri },
json: { custom_token: customToken, redirect_uri: redirectURI },
onFinished,
};
};
1 change: 1 addition & 0 deletions packages/javascript-sdk/users/utils/registerUserSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { passwordSchema } from './password.js';
export const registerUserProperties = {
email: YupHelpers.emailAddress().required(),
password: passwordSchema().required().default(''),
redirect_uri: YupHelpers.url().nullable().default(null),
username: usernameSchema().required().default(''),
} as const;
export const registerUserSchema = yup.object(registerUserProperties);
Expand Down
21 changes: 20 additions & 1 deletion sites/home-site/pages/account/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useQueryLoggedInUserStatus } from '@wallot/react/src/hooks/useQueryLogg
import { GoArrowRight, GoCheckCircle, GoPerson } from 'react-icons/go';
import { useQueryAssetOrdersForLoggedInUser } from '@wallot/react/src/features/assetOrders/hooks/useQueryAssetOrdersForLoggedInUser';
import { getCurrencyUsdStringFromCents } from 'ergonomic';
import { Fragment } from 'react';
import { Fragment, useEffect } from 'react';
import { BsBank } from 'react-icons/bs';
import { useToast } from 'ergonomic-react/src/components/ui/use-toast';

Expand All @@ -40,6 +40,8 @@ const Page: NextPage<PageStaticProps> = (props) => {
isUserActivatedByAlpaca,
isKycUser,
isUserWithAlpacaEquity,
isActivatedUser,
isLoggedInUserStatusLoading,
} = useQueryLoggedInUserStatus();
const { assetOrdersForLoggedInUser } = useQueryAssetOrdersForLoggedInUser();

Expand All @@ -60,6 +62,23 @@ const Page: NextPage<PageStaticProps> = (props) => {
routeId: ROUTE_RUNTIME_ID,
};

// ==== Effects ==== //

// Redirect to onboarding if user is not activated
useEffect(() => {
if (isLoggedInUserStatusLoading) return;
if (!isActivatedUser) {
void router.push(
getHomeSiteRoute({
includeOrigin: false,
origin: null,
queryParams: {},
routeStaticId: 'HOME_SITE__/GET_STARTED',
}),
);
}
}, [isLoggedInUserStatusLoading, isActivatedUser]);

// ==== Render ==== //
return (
<PageComponent {...pageProps}>
Expand Down
22 changes: 10 additions & 12 deletions sites/sso-site/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,17 @@ const Page: NextPage<PageStaticProps> = (props) => {
// ==== Constants ==== //

// Router Query
const _: RouteQueryParams = router?.query ?? {};
_;
const query: RouteQueryParams = router?.query ?? {};

// Router Query Param Values
// const { dest } = query;
const { dest } = query;

// Register Route
const registerRoute = getSsoSiteRoute({
routeStaticId: 'SSO_SITE__/REGISTER',
origin: null,
includeOrigin: false,
queryParams: { dest: undefined },
queryParams: { dest },
});

// Form
Expand Down Expand Up @@ -163,14 +162,13 @@ const Page: NextPage<PageStaticProps> = (props) => {
includeOrigin: true,
queryParams: { client_token: clientToken },
});
// const decodedDest = decodeURIComponent(dest ?? '');
// const hasQueryParams = decodedDest.includes('?');
// const destination = dest
// ? `${decodedDest}${
// hasQueryParams ? '&' : '?'
// }client_token=${clientToken}`
// : defaultDestination;
const destination = defaultDestination;
const decodedDest = decodeURIComponent(dest ?? '');
const hasQueryParams = decodedDest.includes('?');
const destination = dest
? `${decodedDest}${
hasQueryParams ? '&' : '?'
}client_token=${clientToken}`
: defaultDestination;

await router.push(destination);
return;
Expand Down
3 changes: 2 additions & 1 deletion sites/sso-site/pages/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ const Page: NextPage<PageStaticProps> = (props) => {
title: 'Creating your account...',
description: 'This may take a few moments.',
});
registerUser(data);
const redirectURI = dest ? decodeURIComponent(dest) : null;
registerUser({ ...data, redirect_uri: redirectURI });
};

// ==== Render ==== //
Expand Down

0 comments on commit 46ed926

Please sign in to comment.