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

feat: /account/login?redirect_to=/final/url should redirect to /final/url after authn #1950

Merged
merged 7 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/website/lib/magic.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ export async function isLoggedIn() {
* Login with email
*
* @param {string} email
* @param {string} [finalRedirectHref] - href of final url that end-user should
* be redirected to after successful authentication
*/
export async function loginEmail(email) {
export async function loginEmail(email, finalRedirectHref) {
const redirectUri = new URL('/callback/', window.location.origin);
if (finalRedirectHref) {
redirectUri.searchParams.set('redirect_uri', finalRedirectHref);
}
const didToken = await getMagic().auth.loginWithMagicLink({
email: email,
redirectURI: new URL('/callback/', window.location.origin).href,
redirectURI: redirectUri.href,
});

if (didToken) {
Expand Down
2 changes: 1 addition & 1 deletion packages/website/pages/account/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export function getStaticProps() {
props: {
title: 'Payment',
isRestricted: true,
redirectTo: '/login/',
redirectTo: '/login/?redirect_uri=/account/payment',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be /account/payment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redirectTo is where the http response will redirect to when the end-user isn't authenticated. This page is served at /account/payment. If redirect_to=/account/payment, it would be an infinite redirect.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, I misunderstood what was happening there.

stripePublishableKey,
},
};
Expand Down
15 changes: 9 additions & 6 deletions packages/website/pages/callback/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useEffect } from 'react';
import { useRouter } from 'next/router';
import { useQueryClient } from 'react-query';

import Loading from 'components/loading/loading';
import { redirectMagic, redirectSocial } from 'lib/magic.js';
import { redirectMagic, redirectSocial } from '../../lib/magic.js';
import Loading from '../../components/loading/loading.js';

export function getStaticProps() {
return {
Expand All @@ -16,13 +16,15 @@ export function getStaticProps() {
const Callback = () => {
const router = useRouter();
const queryClient = useQueryClient();

const redirectUriQuery = router.query.redirect_uri;
const redirectUri =
(redirectUriQuery && Array.isArray(redirectUriQuery) ? redirectUriQuery[0] : redirectUriQuery) ?? '/account';
useEffect(() => {
const finishSocialLogin = async () => {
try {
await redirectSocial();
await queryClient.invalidateQueries('magic-user');
router.push('/account');
router.push(redirectUri);
} catch (err) {
console.error(err);
await queryClient.invalidateQueries('magic-user');
Expand All @@ -33,7 +35,8 @@ const Callback = () => {
try {
await redirectMagic();
await queryClient.invalidateQueries('magic-user');
router.push('/account');

router.push(redirectUri);
} catch (err) {
console.error(err);
await queryClient.invalidateQueries('magic-user');
Expand All @@ -46,7 +49,7 @@ const Callback = () => {
if (router.query.provider) {
finishSocialLogin();
}
}, [router, router.query, queryClient]);
}, [router, router.query, queryClient, redirectUri]);

// TODO handle errors
return (
Expand Down
18 changes: 15 additions & 3 deletions packages/website/pages/login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ const LoginType = {
EMAIL: 'email',
};

/**
* Get the first thing
* @template T
* @param {T | T[] | undefined} thingOrThings
*/
function first(thingOrThings) {
if (Array.isArray(thingOrThings)) {
return thingOrThings[0];
}
return thingOrThings;
}

const Login = () => {
// App query client
const queryClient = useQueryClient();

// App wide methods
const { push } = useRouter();
const { push, query } = useRouter();

// Error states
const [errors, setErrors] = useState(/** @type {{email?: string}} */ ({}));
Expand All @@ -37,7 +49,7 @@ const Login = () => {
setErrors({ email: undefined });
setIsLoggingIn(LoginType.EMAIL);
try {
await loginEmail(email || '');
await loginEmail(email || '', first(query.redirect_uri));
await queryClient.invalidateQueries('magic-user');
push('/account');
} catch (error) {
Expand All @@ -48,7 +60,7 @@ const Login = () => {
// @ts-ignore Catch clause variable type annotation must be 'any' or 'unknown' if specified.ts(1196)
setErrors({ email: error.message });
}
}, [email, push, queryClient]);
}, [email, push, queryClient, query.redirect_uri]);

// Callback for github login logic
const onGithubLogin = useCallback(async () => {
Expand Down