Skip to content

Commit

Permalink
Add auth redirect support.. (#220)
Browse files Browse the repository at this point in the history
* feat: add redirectUrl support
  • Loading branch information
oreHGA committed Jun 3, 2024
1 parent 8e27f6b commit 8316e60
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
3 changes: 2 additions & 1 deletion frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NEXT_AUTH_SECRET=
NEXT_PUBLIC_NEUROFUSION_BACKEND_URL=
NEXT_PUBLIC_FUSION_NOSTR_PUBLIC_KEY=
NEXT_PUBLIC_ANALYSIS_SERVER_URL=
NEXT_PUBLIC_ANALYSIS_SERVER_URL=
NEXTAUTH_URL=
4 changes: 2 additions & 2 deletions frontend/src/components/charts/line-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export const FusionLineChart: FC<LineChartProps> = ({ seriesData, startDate, tim
}, [seriesData]);

return (
<div>
<ReactEcharts option={chartOptions} style={{ height: "500px", width: "800px" }} opts={{ renderer: "canvas" }} />
<div className="p-5">
<ReactEcharts option={chartOptions} style={{ height: "500px", width: "100%" }} opts={{ renderer: "canvas" }} />
</div>
);
};
12 changes: 7 additions & 5 deletions frontend/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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) {
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/pages/auth/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]");
}
};

Expand Down
5 changes: 2 additions & 3 deletions frontend/src/pages/quest/[guid].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
Expand Down Expand Up @@ -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,
},
};
Expand Down

0 comments on commit 8316e60

Please sign in to comment.