From 87d823054efd036a52ab534832a82d4847be1489 Mon Sep 17 00:00:00 2001 From: Ore Ogundipe Date: Mon, 3 Jun 2024 15:24:08 -0700 Subject: [PATCH] feat: add redirectUrl support --- frontend/.env.example | 3 ++- frontend/src/pages/api/auth/[...nextauth].ts | 12 +++++++----- frontend/src/pages/auth/login.tsx | 5 ++--- frontend/src/pages/quest/[guid].tsx | 5 ++--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/frontend/.env.example b/frontend/.env.example index a97407a1..29e2600c 100644 --- a/frontend/.env.example +++ b/frontend/.env.example @@ -1,4 +1,5 @@ NEXT_AUTH_SECRET= NEXT_PUBLIC_NEUROFUSION_BACKEND_URL= NEXT_PUBLIC_FUSION_NOSTR_PUBLIC_KEY= -NEXT_PUBLIC_ANALYSIS_SERVER_URL= \ No newline at end of file +NEXT_PUBLIC_ANALYSIS_SERVER_URL= +NEXTAUTH_URL= \ No newline at end of file diff --git a/frontend/src/pages/api/auth/[...nextauth].ts b/frontend/src/pages/api/auth/[...nextauth].ts index 25823f6b..a807b6c6 100644 --- a/frontend/src/pages/api/auth/[...nextauth].ts +++ b/frontend/src/pages/api/auth/[...nextauth].ts @@ -7,8 +7,6 @@ import CredentialsProvider from "next-auth/providers/credentials"; import { randomBytes } from "crypto"; -const magic = new Magic(process.env.MAGIC_SECRET_KEY); - export const authOptions: NextAuthOptions = { secret: process.env.NEXT_AUTH_SECRET, session: { @@ -21,8 +19,12 @@ export const authOptions: NextAuthOptions = { signIn: "/auth/login", }, callbacks: { - async redirect() { - return "/playground"; + async redirect({ url, baseUrl }) { + // Allows relative callback URLs + if (url.startsWith("/")) return `${baseUrl}${url}`; + // Allows callback URLs on the same origin + else if (new URL(url).origin === baseUrl) return url; + return baseUrl; }, async jwt({ token, user }) { if (user) { @@ -48,7 +50,7 @@ export const authOptions: NextAuthOptions = { privateKey: { label: "privateKey", type: "password" }, }, async authorize(credentials, req) { - if (credentials && (credentials.userNpub && credentials.authToken)) { + if (credentials && credentials.userNpub && credentials.authToken) { const resObject: User = { id: randomBytes(4).toString("hex"), name: credentials.userNpub, diff --git a/frontend/src/pages/auth/login.tsx b/frontend/src/pages/auth/login.tsx index 00e6d040..428a745b 100644 --- a/frontend/src/pages/auth/login.tsx +++ b/frontend/src/pages/auth/login.tsx @@ -64,17 +64,16 @@ const LoginPage = React.memo(() => { const completeNostrLogin = async (publicKey: string, privateKey?: string) => { const authObject = await authService.completeNostrLogin(publicKey, privateKey); - console.log(authObject); if (authObject) { await signIn("credentials", { ...authObject, privateKey, redirect: true, - callbackUrl: router.query.callbackUrl?.toString(), + callbackUrl: router.query.callbackUrl?.toString() ?? "/playground", }); } else { // TODO: render error message - console.error("Error logging in"); + alert("Error logging in, please try again or reach out to contact@usefusion.app"); } }; diff --git a/frontend/src/pages/quest/[guid].tsx b/frontend/src/pages/quest/[guid].tsx index 54a7637b..0333aae1 100644 --- a/frontend/src/pages/quest/[guid].tsx +++ b/frontend/src/pages/quest/[guid].tsx @@ -10,9 +10,7 @@ import { api } from "~/config"; import { useSession } from "next-auth/react"; import { DisplayCategory, FusionHealthDataset, FusionQuestDataset, IQuest } from "~/@types"; import { usePathname } from "next/navigation"; -import { Experiment } from "~/components/lab"; import { FusionLineChart } from "~/components/charts"; -import { set } from "zod"; import dayjs from "dayjs"; const categories: DisplayCategory[] = [ @@ -223,9 +221,10 @@ export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { const session = await getServerSession(req, res, authOptions); if (!session) { + const currentUrl = `${req.url}`; return { redirect: { - destination: "/auth/login", + destination: `/auth/login?callbackUrl=${encodeURIComponent(currentUrl)}`, permanent: false, }, };