mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-18 23:45:26 +02:00
improved get next url handling
This commit is contained in:
parent
40e1512df3
commit
b7a80e383a
@ -1,26 +1,14 @@
|
||||
import {useLocation, useNavigate} from "react-router-dom"
|
||||
import {useCallback} from "react"
|
||||
|
||||
import {getNextUrl} from "~/utils"
|
||||
|
||||
export default function useNavigateToNext(defaultNextUrl = "/"): () => void {
|
||||
const navigate = useNavigate()
|
||||
const location = useLocation()
|
||||
|
||||
const navigateToNext = useCallback(() => {
|
||||
const nextUrlSuggested =
|
||||
new URLSearchParams(location.search).get("next") || ""
|
||||
|
||||
const nextUrl = (() => {
|
||||
if (
|
||||
nextUrlSuggested.startsWith("/") ||
|
||||
nextUrlSuggested.startsWith(`https://${window.location.host}`)
|
||||
) {
|
||||
return nextUrlSuggested
|
||||
}
|
||||
|
||||
return defaultNextUrl
|
||||
})()
|
||||
|
||||
setTimeout(() => navigate(nextUrl), 0)
|
||||
setTimeout(() => navigate(getNextUrl(defaultNextUrl)), 0)
|
||||
}, [location, navigate])
|
||||
|
||||
return navigateToNext
|
||||
|
@ -1,15 +1,15 @@
|
||||
import * as yup from "yup"
|
||||
import {ReactElement, useContext} from "react"
|
||||
import {ReactElement, useContext, useLayoutEffect} from "react"
|
||||
import {useFormik} from "formik"
|
||||
import {MdLock} from "react-icons/md"
|
||||
import {useTranslation} from "react-i18next"
|
||||
import {useLoaderData} from "react-router-dom"
|
||||
import {useLoaderData, useNavigate} from "react-router-dom"
|
||||
|
||||
import {InputAdornment} from "@mui/material"
|
||||
|
||||
import {useNavigateToNext, useUser} from "~/hooks"
|
||||
import {AuthContext, PasswordField, SimpleForm} from "~/components"
|
||||
import {getMasterPassword} from "~/utils"
|
||||
import {AuthContext, EncryptionStatus, PasswordField, SimpleForm} from "~/components"
|
||||
import {getMasterPassword, getNextUrl} from "~/utils"
|
||||
import {ServerSettings} from "~/server-types"
|
||||
|
||||
interface Form {
|
||||
@ -18,8 +18,10 @@ interface Form {
|
||||
|
||||
export default function EnterDecryptionPassword(): ReactElement {
|
||||
const {t} = useTranslation()
|
||||
const navigate = useNavigate()
|
||||
const navigateToNext = useNavigateToNext()
|
||||
const user = useUser()
|
||||
const {encryptionStatus} = useContext(AuthContext)
|
||||
const serverSettings = useLoaderData() as ServerSettings
|
||||
const {_setEncryptionPassword} = useContext(AuthContext)
|
||||
|
||||
@ -52,6 +54,16 @@ export default function EnterDecryptionPassword(): ReactElement {
|
||||
},
|
||||
})
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (encryptionStatus === EncryptionStatus.Unavailable) {
|
||||
const nextUrl = getNextUrl()
|
||||
|
||||
navigate(`/auth/complete-account?setup=true&next=${nextUrl}`, {
|
||||
replace: true,
|
||||
})
|
||||
}
|
||||
}, [encryptionStatus])
|
||||
|
||||
return (
|
||||
<form onSubmit={formik.handleSubmit}>
|
||||
<SimpleForm
|
||||
|
12
src/utils/get-next-url.ts
Normal file
12
src/utils/get-next-url.ts
Normal file
@ -0,0 +1,12 @@
|
||||
export default function getNextUrl(defaultUrl = "/"): string {
|
||||
const nextUrlSuggested = new URLSearchParams(location.search).get("next") || ""
|
||||
|
||||
if (
|
||||
nextUrlSuggested.startsWith("/") ||
|
||||
nextUrlSuggested.startsWith(`https://${window.location.host}`)
|
||||
) {
|
||||
return nextUrlSuggested
|
||||
}
|
||||
|
||||
return defaultUrl
|
||||
}
|
@ -8,5 +8,7 @@ export * from "./when-enter-pressed"
|
||||
export {default as whenEnterPressed} from "./when-enter-pressed"
|
||||
export * from "./create-enum-map-from-translation"
|
||||
export {default as createEnumMapFromTranslation} from "./create-enum-map-from-translation"
|
||||
export * from "./get-next-url"
|
||||
export {default as getNextUrl} from "./get-next-url"
|
||||
|
||||
export * from "./crypto"
|
||||
|
Loading…
x
Reference in New Issue
Block a user