diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index 0e5304a..661d1b9 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -26,15 +26,13 @@ export default function Login(props: Props) { const options: RedirectLoginOptions = { authorizationParams: {} // Initialize here }; - console.log(location) + if (location && location.search) { let parsed = queryString.parse(location.search); - console.log(parsed.signup) if (parsed.signup === "true") { // if (options.authorizationParams) options.authorizationParams.screen_hint = "signup"; // 'signup' tells Auth0 to show the signup tab options.authorizationParams!.screen_hint = "signup"; // 'signup' tells Auth0 to show the signup tab - console.log(options) } if (parsed.email && parsed.email !== "") { diff --git a/src/providers/auth.tsx b/src/providers/auth.tsx index e66829a..2c412f8 100644 --- a/src/providers/auth.tsx +++ b/src/providers/auth.tsx @@ -1,31 +1,55 @@ -import { useAuth0 } from "@auth0/auth0-react"; +import { RedirectLoginOptions, useAuth0 } from "@auth0/auth0-react"; +import queryString from "query-string"; import { PropsWithChildren, useEffect } from "react"; interface Props extends PropsWithChildren{ setToken: React.Dispatch> + prevPath?: string } export default function AuthWrapper(props: Props) { - const { children, setToken } = props; - const { isLoading, loginWithRedirect, isAuthenticated, getAccessTokenSilently } = useAuth0(); - - useEffect(() => { - if (!isAuthenticated && !isLoading) { - loginWithRedirect(); - } - }, [isAuthenticated, loginWithRedirect, isLoading]); + const { children, setToken } = props; + const { isLoading, loginWithRedirect, isAuthenticated, getAccessTokenSilently } = useAuth0(); - useEffect(() => { - if (isAuthenticated) getAccessTokenSilently().then(t => { - if (t.length < 1) { - loginWithRedirect() - } - setToken(t) - }).catch(() => { - // No token, go back to login - loginWithRedirect() - }) - }, [setToken, isAuthenticated]) - return children; + useEffect(() => { + const url = new URL(window.location.href); + + const isManualLoginPage = url.pathname === "/login"; + console.log(isManualLoginPage) + + if (isManualLoginPage) { + const options: RedirectLoginOptions = { + authorizationParams: {} // Initialize here + }; + + let parsed = queryString.parse(url.search); + if (parsed.signup === "true") { + options.authorizationParams!.screen_hint = "signup"; + } + + if (parsed.email && parsed.email !== "") { + options.authorizationParams!.login_hint = parsed.email.toString(); // prefill email + } + console.log(options) + loginWithRedirect(options) + + } else if (!isAuthenticated && !isLoading) { + loginWithRedirect(); + } + }, [isAuthenticated, loginWithRedirect, isLoading]); + + useEffect(() => { + if (isAuthenticated) getAccessTokenSilently().then(t => { + if (t.length < 1) { + loginWithRedirect() + } + setToken(t) + }).catch(() => { + // No token, go back to login + loginWithRedirect() + }) + }, [setToken, isAuthenticated]) + + return children; } \ No newline at end of file diff --git a/src/user/UserMenu.tsx b/src/user/UserMenu.tsx index bfd1baf..2b41a6f 100644 --- a/src/user/UserMenu.tsx +++ b/src/user/UserMenu.tsx @@ -1,11 +1,10 @@ -import { Avatar, Box, Button, Checkbox, Divider, IconButton, ListItemIcon, ListItemText, Menu, MenuItem, PaletteColor, Theme, Tooltip, useTheme } from "@mui/material"; +import { Avatar, Box, Button, Checkbox, Divider, ListItemIcon, ListItemText, Menu, MenuItem, PaletteColor, Theme, Tooltip, useTheme } from "@mui/material"; import { useGlobalState } from "../providers/StateContainer"; import { getSecondaryColour, getSignatureAccentColour } from "../services/whiteLabel"; import { makeStyles } from "@mui/styles"; import { LockOpen, Person, Lock, Settings, SupervisedUserCircle as TeamIcon, ExitToApp, PersonAdd } from "@mui/icons-material"; import { useState } from "react"; import UserTeamName from "./UserTeamName"; -import ThemeIcon from "../common/ThemeIcon"; import { useAuth0 } from "@auth0/auth0-react"; import UserSettings from "./UserSettings"; import UserAvatar from "./UserAvatar";