mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-19 15:55: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 {useLocation, useNavigate} from "react-router-dom"
|
||||||
import {useCallback} from "react"
|
import {useCallback} from "react"
|
||||||
|
|
||||||
|
import {getNextUrl} from "~/utils"
|
||||||
|
|
||||||
export default function useNavigateToNext(defaultNextUrl = "/"): () => void {
|
export default function useNavigateToNext(defaultNextUrl = "/"): () => void {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const location = useLocation()
|
const location = useLocation()
|
||||||
|
|
||||||
const navigateToNext = useCallback(() => {
|
const navigateToNext = useCallback(() => {
|
||||||
const nextUrlSuggested =
|
setTimeout(() => navigate(getNextUrl(defaultNextUrl)), 0)
|
||||||
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)
|
|
||||||
}, [location, navigate])
|
}, [location, navigate])
|
||||||
|
|
||||||
return navigateToNext
|
return navigateToNext
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import * as yup from "yup"
|
import * as yup from "yup"
|
||||||
import {ReactElement, useContext} from "react"
|
import {ReactElement, useContext, useLayoutEffect} from "react"
|
||||||
import {useFormik} from "formik"
|
import {useFormik} from "formik"
|
||||||
import {MdLock} from "react-icons/md"
|
import {MdLock} from "react-icons/md"
|
||||||
import {useTranslation} from "react-i18next"
|
import {useTranslation} from "react-i18next"
|
||||||
import {useLoaderData} from "react-router-dom"
|
import {useLoaderData, useNavigate} from "react-router-dom"
|
||||||
|
|
||||||
import {InputAdornment} from "@mui/material"
|
import {InputAdornment} from "@mui/material"
|
||||||
|
|
||||||
import {useNavigateToNext, useUser} from "~/hooks"
|
import {useNavigateToNext, useUser} from "~/hooks"
|
||||||
import {AuthContext, PasswordField, SimpleForm} from "~/components"
|
import {AuthContext, EncryptionStatus, PasswordField, SimpleForm} from "~/components"
|
||||||
import {getMasterPassword} from "~/utils"
|
import {getMasterPassword, getNextUrl} from "~/utils"
|
||||||
import {ServerSettings} from "~/server-types"
|
import {ServerSettings} from "~/server-types"
|
||||||
|
|
||||||
interface Form {
|
interface Form {
|
||||||
@ -18,8 +18,10 @@ interface Form {
|
|||||||
|
|
||||||
export default function EnterDecryptionPassword(): ReactElement {
|
export default function EnterDecryptionPassword(): ReactElement {
|
||||||
const {t} = useTranslation()
|
const {t} = useTranslation()
|
||||||
|
const navigate = useNavigate()
|
||||||
const navigateToNext = useNavigateToNext()
|
const navigateToNext = useNavigateToNext()
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
|
const {encryptionStatus} = useContext(AuthContext)
|
||||||
const serverSettings = useLoaderData() as ServerSettings
|
const serverSettings = useLoaderData() as ServerSettings
|
||||||
const {_setEncryptionPassword} = useContext(AuthContext)
|
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 (
|
return (
|
||||||
<form onSubmit={formik.handleSubmit}>
|
<form onSubmit={formik.handleSubmit}>
|
||||||
<SimpleForm
|
<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 {default as whenEnterPressed} from "./when-enter-pressed"
|
||||||
export * from "./create-enum-map-from-translation"
|
export * from "./create-enum-map-from-translation"
|
||||||
export {default as createEnumMapFromTranslation} 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"
|
export * from "./crypto"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user