>(
- `/org/${orgId}/user/${user.userId}`,
- cookie
- )
- );
- const orgUser = await getOrgUser();
- } catch {
- redirect(`/`);
- }
-
return (
<>
Welcome to {orgId} dashboard
diff --git a/src/app/[orgId]/settings/general/layout.tsx b/src/app/[orgId]/settings/general/layout.tsx
index ee413b9e..f4dc6549 100644
--- a/src/app/[orgId]/settings/general/layout.tsx
+++ b/src/app/[orgId]/settings/general/layout.tsx
@@ -26,7 +26,7 @@ export default async function GeneralSettingsPage({
const user = await getUser();
if (!user) {
- redirect("/auth/login");
+ redirect(`/?redirect=/${orgId}/settings/general`);
}
let orgUser = null;
@@ -34,8 +34,8 @@ export default async function GeneralSettingsPage({
const getOrgUser = cache(async () =>
internal.get>(
`/org/${orgId}/user/${user.userId}`,
- await authCookieHeader()
- )
+ await authCookieHeader(),
+ ),
);
const res = await getOrgUser();
orgUser = res.data.data;
@@ -48,8 +48,8 @@ export default async function GeneralSettingsPage({
const getOrg = cache(async () =>
internal.get>(
`/org/${orgId}`,
- await authCookieHeader()
- )
+ await authCookieHeader(),
+ ),
);
const res = await getOrg();
org = res.data.data;
diff --git a/src/app/[orgId]/settings/layout.tsx b/src/app/[orgId]/settings/layout.tsx
index 45095896..b277b7f3 100644
--- a/src/app/[orgId]/settings/layout.tsx
+++ b/src/app/[orgId]/settings/layout.tsx
@@ -55,7 +55,7 @@ export default async function SettingsLayout(props: SettingsLayoutProps) {
const user = await getUser();
if (!user) {
- redirect("/auth/login");
+ redirect(`/?redirect=/${params.orgId}/`);
}
const cookie = await authCookieHeader();
@@ -64,8 +64,8 @@ export default async function SettingsLayout(props: SettingsLayoutProps) {
const getOrgUser = cache(() =>
internal.get>(
`/org/${params.orgId}/user/${user.userId}`,
- cookie
- )
+ cookie,
+ ),
);
const orgUser = await getOrgUser();
@@ -79,7 +79,7 @@ export default async function SettingsLayout(props: SettingsLayoutProps) {
let orgs: ListOrgsResponse["orgs"] = [];
try {
const getOrgs = cache(() =>
- internal.get>(`/orgs`, cookie)
+ internal.get>(`/orgs`, cookie),
);
const res = await getOrgs();
if (res && res.data.data.orgs) {
diff --git a/src/app/auth/login/DashboardLoginForm.tsx b/src/app/auth/login/DashboardLoginForm.tsx
index 986993cd..61abe8cc 100644
--- a/src/app/auth/login/DashboardLoginForm.tsx
+++ b/src/app/auth/login/DashboardLoginForm.tsx
@@ -30,7 +30,15 @@ export default function DashboardLoginForm({
router.push("/")}
+ onLogin={() => {
+ if (redirect && redirect.includes("http")) {
+ window.location.href = redirect;
+ } else if (redirect) {
+ router.push(redirect);
+ } else {
+ router.push("/");
+ }
+ }}
/>
diff --git a/src/app/auth/resource/[resourceId]/components/ResourceAuthPortal.tsx b/src/app/auth/resource/[resourceId]/components/ResourceAuthPortal.tsx
index 3556bf51..4e3a612b 100644
--- a/src/app/auth/resource/[resourceId]/components/ResourceAuthPortal.tsx
+++ b/src/app/auth/resource/[resourceId]/components/ResourceAuthPortal.tsx
@@ -38,6 +38,7 @@ import { LoginResponse } from "@server/routers/auth";
import ResourceAccessDenied from "./ResourceAccessDenied";
import LoginForm from "@app/components/LoginForm";
import { AuthWithPasswordResponse } from "@server/routers/resource";
+import { redirect } from "next/dist/server/api-utils";
const pinSchema = z.object({
pin: z
@@ -113,11 +114,6 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
},
});
- function constructRedirect(redirect: string): string {
- const redirectUrl = new URL(redirect);
- return redirectUrl.toString();
- }
-
const onPinSubmit = (values: z.infer) => {
setLoadingLogin(true);
api.post>(
@@ -127,9 +123,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
.then((res) => {
const session = res.data.data.session;
if (session) {
- const url = constructRedirect(props.redirect);
- console.log(url);
- window.location.href = url;
+ window.location.href = props.redirect;
}
})
.catch((e) => {
@@ -152,7 +146,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
.then((res) => {
const session = res.data.data.session;
if (session) {
- window.location.href = constructRedirect(props.redirect);
+ window.location.href = props.redirect;
}
})
.catch((e) => {
@@ -172,7 +166,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
}
if (!accessDenied) {
- window.location.href = constructRedirect(props.redirect);
+ window.location.href = props.redirect;
}
}
@@ -371,6 +365,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
className={`${numMethods <= 1 ? "mt-0" : ""}`}
>
await handleSSOAuth()
}
diff --git a/src/app/auth/verify-email/VerifyEmailForm.tsx b/src/app/auth/verify-email/VerifyEmailForm.tsx
index a8cedf9c..cceddc66 100644
--- a/src/app/auth/verify-email/VerifyEmailForm.tsx
+++ b/src/app/auth/verify-email/VerifyEmailForm.tsx
@@ -79,6 +79,7 @@ export default function VerifyEmailForm({
.catch((e) => {
setError(formatAxiosError(e, "An error occurred"));
console.error("Failed to verify email:", e);
+ setIsSubmitting(false);
});
if (res && res.data?.data?.valid) {
@@ -125,7 +126,7 @@ export default function VerifyEmailForm({
- Verify Your Email
+ Verify Email
Enter the verification code sent to your email address.
@@ -234,7 +235,7 @@ export default function VerifyEmailForm({
-
+
;
}) {
- if (process.env.PUBLIC_FLAGS_EMAIL_VERIFICATION_REQUIRED !== "true") {
+ if (process.env.FLAGS_EMAIL_VERIFICATION_REQUIRED !== "true") {
redirect("/");
}
const searchParams = await props.searchParams;
const getUser = cache(verifySession);
- const user = await getUser();
+ const user = await getUser({ skipCheckVerifyEmail: true });
if (!user) {
redirect("/");
diff --git a/src/app/invite/page.tsx b/src/app/invite/page.tsx
index 4f5923cd..a02e5862 100644
--- a/src/app/invite/page.tsx
+++ b/src/app/invite/page.tsx
@@ -21,7 +21,7 @@ export default async function InvitePage(props: {
const user = await verifySession();
if (!user) {
- redirect(`/auth/login?redirect=/invite?token=${params.token}`);
+ redirect(`/?redirect=/invite?token=${params.token}`);
}
const parts = tokenParam.split("-");
diff --git a/src/app/page.tsx b/src/app/page.tsx
index 7f143399..8cdc09f6 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -12,21 +12,36 @@ import { cache } from "react";
export const dynamic = "force-dynamic";
export default async function Page(props: {
- searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
+ searchParams: Promise<{ redirect: string | undefined }>;
}) {
const params = await props.searchParams; // this is needed to prevent static optimization
const getUser = cache(verifySession);
- const user = await getUser();
+ const user = await getUser({ skipCheckVerifyEmail: true });
if (!user) {
- redirect("/auth/login");
+ if (params.redirect) {
+ redirect(`/auth/login?redirect=${params.redirect}`);
+ } else {
+ redirect(`/auth/login`);
+ }
+ }
+
+ if (
+ !user.emailVerified &&
+ process.env.FLAGS_EMAIL_VERIFICATION_REQUIRED === "true"
+ ) {
+ if (params.redirect) {
+ redirect(`/auth/verify-email?redirect=${params.redirect}`);
+ } else {
+ redirect(`/auth/verify-email`);
+ }
}
let orgs: ListOrgsResponse["orgs"] = [];
try {
const res = await internal.get>(
`/orgs`,
- await authCookieHeader()
+ await authCookieHeader(),
);
if (res && res.data.data.orgs) {
diff --git a/src/app/setup/layout.tsx b/src/app/setup/layout.tsx
index c8c96ac7..49925527 100644
--- a/src/app/setup/layout.tsx
+++ b/src/app/setup/layout.tsx
@@ -19,7 +19,7 @@ export default async function SetupLayout({
const user = await getUser();
if (!user) {
- redirect("/");
+ redirect("/?redirect=/setup");
}
return {children}
;
diff --git a/src/components/LoginForm.tsx b/src/components/LoginForm.tsx
index ea132bb2..c57d9ab9 100644
--- a/src/components/LoginForm.tsx
+++ b/src/components/LoginForm.tsx
@@ -72,13 +72,14 @@ export default function LoginForm({ redirect, onLogin }: LoginFormProps) {
);
});
+ console.log(res);
+
if (res && res.status === 200) {
setError(null);
- console.log(res);
-
if (res.data?.data?.emailVerificationRequired) {
if (redirect) {
+ console.log("here", redirect)
router.push(`/auth/verify-email?redirect=${redirect}`);
} else {
router.push("/auth/verify-email");
@@ -86,14 +87,8 @@ export default function LoginForm({ redirect, onLogin }: LoginFormProps) {
return;
}
- if (redirect && redirect.includes("http")) {
- window.location.href = redirect;
- } else if (redirect) {
- router.push(redirect);
- } else {
- if (onLogin) {
- await onLogin();
- }
+ if (onLogin) {
+ await onLogin();
}
}
diff --git a/src/lib/auth/verifySession.ts b/src/lib/auth/verifySession.ts
index 59f9386c..d12c81ff 100644
--- a/src/lib/auth/verifySession.ts
+++ b/src/lib/auth/verifySession.ts
@@ -3,15 +3,33 @@ import { authCookieHeader } from "@app/api/cookies";
import { GetUserResponse } from "@server/routers/user";
import { AxiosResponse } from "axios";
-export async function verifySession(): Promise {
+export async function verifySession({
+ skipCheckVerifyEmail,
+}: {
+ skipCheckVerifyEmail?: boolean;
+} = {}): Promise {
try {
const res = await internal.get>(
"/user",
- await authCookieHeader()
+ await authCookieHeader(),
);
- return res.data.data;
- } catch {
+ const user = res.data.data;
+
+ if (!user) {
+ return null;
+ }
+
+ if (
+ !skipCheckVerifyEmail &&
+ !user.emailVerified &&
+ process.env.FLAGS_EMAIL_VERIFICATION_REQUIRED == "true"
+ ) {
+ return null;
+ }
+
+ return user;
+ } catch (e) {
return null;
}
-}
\ No newline at end of file
+}