fix(auth-web): handle verify-email token links and show auth feedback
Some checks failed
CI / test (push) Failing after 1m56s
Some checks failed
CI / test (push) Failing after 1m56s
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { joinByInvite } from "../api/chats";
|
||||
import { verifyEmailRequest } from "../api/auth";
|
||||
import { ToastViewport } from "../components/ToastViewport";
|
||||
import { AuthPage } from "../pages/AuthPage";
|
||||
import { ChatsPage } from "../pages/ChatsPage";
|
||||
@@ -8,6 +9,7 @@ import { useChatStore } from "../store/chatStore";
|
||||
import { applyAppearancePreferences, getAppPreferences } from "../utils/preferences";
|
||||
|
||||
const PENDING_INVITE_TOKEN_KEY = "bm_pending_invite_token";
|
||||
const AUTH_NOTICE_KEY = "bm_auth_notice";
|
||||
|
||||
export function App() {
|
||||
const accessToken = useAuthStore((s) => s.accessToken);
|
||||
@@ -37,6 +39,23 @@ export function App() {
|
||||
});
|
||||
}, [accessToken, loadMe, refresh, logout]);
|
||||
|
||||
useEffect(() => {
|
||||
const verificationToken = extractEmailVerificationTokenFromLocation();
|
||||
if (!verificationToken) {
|
||||
return;
|
||||
}
|
||||
void (async () => {
|
||||
try {
|
||||
await verifyEmailRequest(verificationToken);
|
||||
window.localStorage.setItem(AUTH_NOTICE_KEY, "Email confirmed. You can now sign in.");
|
||||
} catch {
|
||||
window.localStorage.setItem(AUTH_NOTICE_KEY, "Email verification failed or token expired.");
|
||||
} finally {
|
||||
window.history.replaceState(null, "", "/");
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const token = extractInviteTokenFromLocation();
|
||||
if (!token) {
|
||||
@@ -90,3 +109,14 @@ function extractInviteTokenFromLocation(): string | null {
|
||||
const match = url.pathname.match(/^\/join\/([^/]+)$/i);
|
||||
return match?.[1]?.trim() || null;
|
||||
}
|
||||
|
||||
function extractEmailVerificationTokenFromLocation(): string | null {
|
||||
if (typeof window === "undefined") {
|
||||
return null;
|
||||
}
|
||||
const url = new URL(window.location.href);
|
||||
if (!/^\/verify-email\/?$/i.test(url.pathname)) {
|
||||
return null;
|
||||
}
|
||||
return url.searchParams.get("token")?.trim() || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user