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

fix: should redirect to previous page after logging in #172

Merged
merged 10 commits into from
Feb 11, 2022
8 changes: 7 additions & 1 deletion src/blocks/sign-in-button/sign-in-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import 'twin.macro';
import { Button, ButtonProps } from '$/components/button';
import { useCurrentUser } from '$/contexts/current-user-context/use-current-user';
import { useSignInWindow } from '$/hooks/use-sign-in-window';
import { CALLBACK_URL } from '$/lib/constants';

export type SignInButtonProps = Pick<ButtonProps, 'variant' | 'size'> & {
inPageNav?: boolean;
};

const handleSessionAndSignIn = () => {
sessionStorage.setItem(CALLBACK_URL, location.pathname);
signIn();
};

export function SignInButton({
variant = 'solid',
inPageNav,
Expand All @@ -23,7 +29,7 @@ export function SignInButton({
<Button
color="primary"
variant={variant}
onClick={() => (!inPageNav ? handleSignIn() : signIn())}
onClick={() => (!inPageNav ? handleSignIn() : handleSessionAndSignIn())}
{...restProps}
>
<span tw="inline-flex flex-row items-center space-x-1">
Expand Down
5 changes: 3 additions & 2 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const LOG_IN_SUCCESS_KEY = 'LOG_IN_SUCCESS';
export const LOG_OUT_SUCCESS_KEY = 'LOG_OUT_SUCCESS';
export const LOG_IN_SUCCESS_KEY = 'log.in.success';
export const LOG_OUT_SUCCESS_KEY = 'log.out.success';
export const CALLBACK_URL = 'callback.url';

// 7 days
export const SESSION_MAX_AGE = 7 * 24 * 60 * 60;
Expand Down
6 changes: 4 additions & 2 deletions src/pages/auth/redirecting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SiteLayout } from '$/blocks/layout';
import { Spinner } from '$/components/spinner';
import { useCurrentUser } from '$/contexts/current-user-context/use-current-user';
import { useTimeout } from '$/hooks/use-timeout';
import { APP_NAME, LOG_IN_SUCCESS_KEY } from '$/lib/constants';
import { APP_NAME, CALLBACK_URL, LOG_IN_SUCCESS_KEY } from '$/lib/constants';
import { hasValidUserProfile } from '$/utilities/user';

export default function Redirecting(): JSX.Element {
Expand All @@ -28,7 +28,9 @@ export default function Redirecting(): JSX.Element {
} else if (!hasValidUserProfile(data)) {
router.push('/auth/welcome?invalidProfile=true');
} else if (data.id) {
router.push('/dashboard');
const callbackUrl = sessionStorage.getItem(CALLBACK_URL);
sessionStorage.removeItem(CALLBACK_URL);
router.push(callbackUrl || '/dashboard');
}
}, [router, session?.isNewUser, data, status, loading]);
useTimeout(() => {
Expand Down