-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
232 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 187 additions & 2 deletions
189
apps/passport-client/new-components/screens/Login/NewCreatePasswordScreen.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,188 @@ | ||
export const NewCreatePasswordScreen = (): JSX.Element => { | ||
return <></>; | ||
import { requestVerifyToken } from "@pcd/passport-interface"; | ||
import { sleep, validateEmail } from "@pcd/util"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { AppContainer } from "../../../components/shared/AppContainer"; | ||
import { appConfig } from "../../../src/appConfig"; | ||
import { useDispatch, useQuery, useSelf } from "../../../src/appHooks"; | ||
import { hasPendingRequest } from "../../../src/sessionStorage"; | ||
import { | ||
LoginContainer, | ||
LoginTitleContainer | ||
} from "../../shared/Login/LoginComponents"; | ||
import { NewPasswordForm2 } from "../../shared/Login/NewPasswordForm2"; | ||
import { Typography } from "../../shared/Typography"; | ||
|
||
export const NewCreatePasswordScreen = (): JSX.Element | null => { | ||
const dispatch = useDispatch(); | ||
const self = useSelf(); | ||
const query = useQuery(); | ||
const email = query?.get("email"); | ||
const token = query?.get("token"); | ||
const autoRegister = query?.get("autoRegister") === "true"; | ||
const targetFolder = query?.get("targetFolder"); | ||
const [error, setError] = useState<string | undefined>(); | ||
const [password, setPassword] = useState(""); | ||
const [confirmPassword, setConfirmPassword] = useState(""); | ||
const [revealPassword, setRevealPassword] = useState(false); | ||
const [settingPassword, setSettingPassword] = useState(false); | ||
const [skipConfirm, setSkipConfirm] = useState(false); | ||
|
||
const redirectToLoginPageWithError = useCallback((e: Error | string) => { | ||
console.error(e); | ||
window.location.hash = "#/login"; | ||
window.location.reload(); | ||
}, []); | ||
|
||
const onSkipPassword = useCallback(async () => { | ||
try { | ||
// If email or token are undefined, we will already have redirected to | ||
// login, so this is just for type-checking | ||
if (email && token) { | ||
setSettingPassword(true); | ||
await sleep(); | ||
await dispatch({ | ||
type: "create-user-skip-password", | ||
email, | ||
token, | ||
targetFolder, | ||
autoRegister, | ||
newUi: true | ||
}); | ||
} | ||
} finally { | ||
setSettingPassword(false); | ||
if (autoRegister) { | ||
window.location.href = "#/new"; | ||
} | ||
} | ||
}, [dispatch, email, token, targetFolder, autoRegister]); | ||
|
||
const checkIfShouldRedirect = useCallback(async () => { | ||
// Redirect to home if already logged in | ||
if (self) { | ||
// Present alert if we had tried to auto-register with a different | ||
// email than the currently logged-in email. | ||
if (autoRegister && !self.emails.includes(email as string)) { | ||
alert( | ||
`You are already logged in as ${ | ||
self.emails.length === 1 | ||
? self.emails?.[0] | ||
: "an account that owns the following email addresses: " + | ||
self.emails.join(", ") | ||
}. Please log out and try navigating to the link again.` | ||
); | ||
} | ||
|
||
if (hasPendingRequest()) { | ||
window.location.hash = "#/new/login-interstitial"; | ||
} else { | ||
window.location.hash = "#/new"; | ||
} | ||
return; | ||
} | ||
if (!email || !validateEmail(email) || !token) { | ||
return redirectToLoginPageWithError( | ||
"Invalid email or token, redirecting to login" | ||
); | ||
} | ||
|
||
if (autoRegister) { | ||
await onSkipPassword(); | ||
} else { | ||
const verifyTokenResult = await requestVerifyToken( | ||
appConfig.zupassServer, | ||
email, | ||
token | ||
); | ||
|
||
if (!verifyTokenResult.success) { | ||
return redirectToLoginPageWithError( | ||
"Invalid email or token, redirecting to login" | ||
); | ||
} | ||
} | ||
}, [ | ||
self, | ||
email, | ||
redirectToLoginPageWithError, | ||
token, | ||
autoRegister, | ||
onSkipPassword | ||
]); | ||
|
||
useEffect(() => { | ||
checkIfShouldRedirect(); | ||
}, [checkIfShouldRedirect]); | ||
|
||
const onSetPassword = useCallback(async () => { | ||
try { | ||
// If email or token are undefined, we will already have redirected to | ||
// login, so this is just for type-checking | ||
if (email && token) { | ||
setSettingPassword(true); | ||
await sleep(); | ||
await dispatch({ | ||
type: "login", | ||
email, | ||
token, | ||
password, | ||
newUi: true | ||
}); | ||
} | ||
} finally { | ||
setSettingPassword(false); | ||
} | ||
}, [dispatch, email, password, token]); | ||
|
||
const onCancelClick = useCallback(() => { | ||
window.location.href = "#/new"; | ||
}, []); | ||
|
||
// If either email or token are undefined, we will already have redirected | ||
if (!email || !token) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<AppContainer bg="gray" fullscreen> | ||
<LoginContainer> | ||
<LoginTitleContainer> | ||
<Typography fontSize={24} fontWeight={800} color="#1E2C50"> | ||
ADD PASSWORD | ||
</Typography> | ||
<Typography | ||
fontSize={16} | ||
fontWeight={400} | ||
color="#1E2C50" | ||
family="Rubik" | ||
> | ||
Make sure that your password is secure, unique, and memorable. If | ||
you forget your password, you'll have to reset your account, and you | ||
will lose access to all your PCDs. | ||
</Typography> | ||
</LoginTitleContainer> | ||
<NewPasswordForm2 | ||
loading={settingPassword} | ||
autoFocus | ||
error={error} | ||
setError={setError} | ||
emails={[email]} | ||
revealPassword={revealPassword} | ||
setRevealPassword={setRevealPassword} | ||
submitButtonText={settingPassword ? "Confirming..." : "Confirm"} | ||
password={password} | ||
confirmPassword={confirmPassword} | ||
setPassword={setPassword} | ||
setConfirmPassword={setConfirmPassword} | ||
onSuccess={onSetPassword} | ||
onCancel={onCancelClick} | ||
// showSkipConfirm={!skipConfirm} | ||
// onSkipConfirm={(): void => { | ||
// onSkipPassword(); | ||
// }} | ||
style={{ width: "100%", marginBottom: 24 }} | ||
/> | ||
</LoginContainer> | ||
</AppContainer> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters