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: add signup button, fix 'back' button on login #686

Merged
merged 3 commits into from
Sep 13, 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: 3 additions & 2 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function getClient() {
interface AuthClient {
isAuthenticated(): Promise<boolean>;
user(): Promise<undefined | User>;
login(redirectTo: string): Promise<void>;
login(redirectTo: string, isSignupFlow?: boolean): Promise<void>;
handleSigninRedirect(): Promise<void>;
logout(): Promise<void>;
getToken(): Promise<string | void>;
Expand All @@ -57,10 +57,11 @@ export const authClient: AuthClient = {
const user = await client.getUser();
return user;
},
async login(redirectTo: string) {
async login(redirectTo: string, isSignupFlow: boolean = false) {
const client = await getClient();
await client.loginWithRedirect({
authorizationParams: {
screen_hint: isSignupFlow ? 'signup' : 'login',
redirect_uri:
window.location.origin +
ROUTES.LOGIN_RESULT +
Expand Down
1 change: 1 addition & 0 deletions src/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const ROUTES = {
LOGOUT: '/logout',
LOGIN: '/login',
LOGIN_WITH_REDIRECT: () => '/login?from=' + encodeURIComponent(window.location.pathname),
SIGNUP_WITH_REDIRECT: () => '/login?signup&from=' + encodeURIComponent(window.location.pathname),
LOGIN_RESULT: '/login-result',
FILES: '/files',
MY_FILES: '/files/mine',
Expand Down
16 changes: 12 additions & 4 deletions src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,19 @@ export const router = createBrowserRouter(
// If they’re not authenticated, send them to Auth0
// Watch for a `from` query param, as unprotected routes will redirect
// to here for them to auth first
// Also watch for the presence of a `signup` query param, which means
// send the user to sign up flow, not login
const url = new URL(request.url);
const redirectTo = url.searchParams.get('from') || '';
await authClient.login(redirectTo);

return null;
const redirectTo = url.searchParams.get('from') || '/';
const isSignupFlow = url.searchParams.get('signup') !== null;
await authClient.login(redirectTo, isSignupFlow);

// auth0 will re-route us (above) but telling react-router where we
// are re-routing to makes sure that this doesn't end up in the history stack
// but we have to add an artifical delay that's long enough for
// the auth0 navigation to take place
await new Promise((resolve) => setTimeout(resolve, 10000));
return redirect(redirectTo);
}}
/>
<Route
Expand Down
26 changes: 16 additions & 10 deletions src/ui/components/PermissionOverlay.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Alert, Button, Paper, useTheme } from '@mui/material';
import { Alert, Button, Paper, Stack, useTheme } from '@mui/material';
import React, { useState } from 'react';
import { isMobile } from 'react-device-detect';
import { Link, useSubmit } from 'react-router-dom';
Expand All @@ -14,6 +14,7 @@ export function PermissionOverlay() {
const [isOpen, setIsOpen] = useState<boolean>(true);
const { permission } = useRecoilValue(editorInteractionStateAtom);
const { name, contents } = useFileContext();
const theme = useTheme();
const submit = useSubmit();

if ((permission === OWNER || permission === EDITOR) && isMobile && isOpen) {
Expand All @@ -34,15 +35,20 @@ export function PermissionOverlay() {
severity="info"
sx={{ width: '100%' }}
action={
<Button
component={Link}
to={ROUTES.LOGIN_WITH_REDIRECT()}
variant="contained"
size="small"
disableElevation
>
Log in
</Button>
<Stack direction="row" gap={theme.spacing(1)}>
<Button component={Link} to={ROUTES.LOGIN_WITH_REDIRECT()} variant="outlined" size="small">
Log in
</Button>
<Button
component={Link}
to={ROUTES.SIGNUP_WITH_REDIRECT()}
variant="contained"
size="small"
disableElevation
>
Sign up
</Button>
</Stack>
}
>
<strong>Welcome to Quadratic.</strong> You must log in to edit this file.
Expand Down