web: open reset form immediately for reset links
This commit is contained in:
@@ -15,6 +15,15 @@ const PENDING_NOTIFICATION_NAV_KEY = "bm_pending_notification_nav";
|
|||||||
const PENDING_RESET_PASSWORD_TOKEN_KEY = "bm_pending_reset_password_token";
|
const PENDING_RESET_PASSWORD_TOKEN_KEY = "bm_pending_reset_password_token";
|
||||||
|
|
||||||
export function App() {
|
export function App() {
|
||||||
|
const [pendingResetToken, setPendingResetToken] = useState<string | null>(() => {
|
||||||
|
const tokenFromLink = extractPasswordResetTokenFromLocation();
|
||||||
|
if (tokenFromLink) {
|
||||||
|
window.localStorage.setItem(PENDING_RESET_PASSWORD_TOKEN_KEY, tokenFromLink);
|
||||||
|
window.history.replaceState(null, "", "/");
|
||||||
|
return tokenFromLink;
|
||||||
|
}
|
||||||
|
return window.localStorage.getItem(PENDING_RESET_PASSWORD_TOKEN_KEY);
|
||||||
|
});
|
||||||
const accessToken = useAuthStore((s) => s.accessToken);
|
const accessToken = useAuthStore((s) => s.accessToken);
|
||||||
const me = useAuthStore((s) => s.me);
|
const me = useAuthStore((s) => s.me);
|
||||||
const loadMe = useAuthStore((s) => s.loadMe);
|
const loadMe = useAuthStore((s) => s.loadMe);
|
||||||
@@ -71,16 +80,10 @@ export function App() {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const resetToken = extractPasswordResetTokenFromLocation();
|
if (pendingResetToken && accessToken) {
|
||||||
if (!resetToken) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
window.localStorage.setItem(PENDING_RESET_PASSWORD_TOKEN_KEY, resetToken);
|
|
||||||
if (accessToken) {
|
|
||||||
logout();
|
logout();
|
||||||
}
|
}
|
||||||
window.history.replaceState(null, "", "/");
|
}, [pendingResetToken, accessToken, logout]);
|
||||||
}, [accessToken, logout]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const nav = extractNotificationNavigationFromLocation();
|
const nav = extractNotificationNavigationFromLocation();
|
||||||
@@ -154,15 +157,20 @@ export function App() {
|
|||||||
})();
|
})();
|
||||||
}, [accessToken, joiningInvite, loadChats, me, setActiveChatId, setFocusedMessage, showToast]);
|
}, [accessToken, joiningInvite, loadChats, me, setActiveChatId, setFocusedMessage, showToast]);
|
||||||
|
|
||||||
if (!accessToken || !me) {
|
if (pendingResetToken) {
|
||||||
const pendingResetToken = window.localStorage.getItem(PENDING_RESET_PASSWORD_TOKEN_KEY);
|
|
||||||
return (
|
return (
|
||||||
<AuthPage
|
<AuthPage
|
||||||
initialResetToken={pendingResetToken}
|
initialResetToken={pendingResetToken}
|
||||||
onResetTokenConsumed={() => window.localStorage.removeItem(PENDING_RESET_PASSWORD_TOKEN_KEY)}
|
onResetTokenConsumed={() => {
|
||||||
|
window.localStorage.removeItem(PENDING_RESET_PASSWORD_TOKEN_KEY);
|
||||||
|
setPendingResetToken(null);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (!accessToken || !me) {
|
||||||
|
return <AuthPage />;
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ChatsPage />
|
<ChatsPage />
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ interface AuthPanelProps {
|
|||||||
export function AuthPanel({ initialResetToken = null, onResetTokenConsumed }: AuthPanelProps) {
|
export function AuthPanel({ initialResetToken = null, onResetTokenConsumed }: AuthPanelProps) {
|
||||||
const login = useAuthStore((s) => s.login);
|
const login = useAuthStore((s) => s.login);
|
||||||
const loading = useAuthStore((s) => s.loading);
|
const loading = useAuthStore((s) => s.loading);
|
||||||
const [step, setStep] = useState<Step>("email");
|
const [step, setStep] = useState<Step>(initialResetToken?.trim() ? "reset" : "email");
|
||||||
const [email, setEmail] = useState("");
|
const [email, setEmail] = useState("");
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
@@ -161,13 +161,21 @@ export function AuthPanel({ initialResetToken = null, onResetTokenConsumed }: Au
|
|||||||
return (
|
return (
|
||||||
<div className="mx-auto mt-16 w-full max-w-md rounded-2xl border border-slate-700/70 bg-panel p-6 shadow-xl">
|
<div className="mx-auto mt-16 w-full max-w-md rounded-2xl border border-slate-700/70 bg-panel p-6 shadow-xl">
|
||||||
<p className="mb-1 text-xl font-semibold">
|
<p className="mb-1 text-xl font-semibold">
|
||||||
{step === "email" ? "Sign in to BenyaMessenger" : step === "register" ? "Create account" : "Enter credentials"}
|
{step === "email"
|
||||||
|
? "Sign in to BenyaMessenger"
|
||||||
|
: step === "register"
|
||||||
|
? "Create account"
|
||||||
|
: step === "reset"
|
||||||
|
? "Reset password"
|
||||||
|
: "Enter credentials"}
|
||||||
</p>
|
</p>
|
||||||
<p className="mb-4 text-sm text-slate-400">
|
<p className="mb-4 text-sm text-slate-400">
|
||||||
{step === "email"
|
{step === "email"
|
||||||
? "Enter your email to continue"
|
? "Enter your email to continue"
|
||||||
: step === "register"
|
: step === "register"
|
||||||
? "This email is not registered yet. Complete registration."
|
? "This email is not registered yet. Complete registration."
|
||||||
|
: step === "reset"
|
||||||
|
? "Enter a new password for your account."
|
||||||
: step === "password"
|
: step === "password"
|
||||||
? "Enter your password"
|
? "Enter your password"
|
||||||
: "Two-factor authentication is enabled"}
|
: "Two-factor authentication is enabled"}
|
||||||
|
|||||||
Reference in New Issue
Block a user