Skip to content

Commit cd13344

Browse files
committed
Fix NextAuth options route export
1 parent 00a43fe commit cd13344

File tree

6 files changed

+121
-119
lines changed

6 files changed

+121
-119
lines changed

src/app/(root)/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { revalidateTag } from "next/cache";
77

88
import hacky from "@/assets/images/hacky-peeking.png";
99
import UserSignOut from "@/components/UserSignOut";
10-
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
10+
import authOptions from "@/app/api/auth/[...nextauth]/authOptions";
1111
import { UserService } from "@/services/UserService";
1212
import { CompanyService } from "@/services/CompanyService";
1313

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { NextAuthOptions } from "next-auth";
2+
import GoogleProvider from "next-auth/providers/google";
3+
import LinkedInProvider from "next-auth/providers/linkedin";
4+
5+
const CANNON_AUTH_ENDPOINT = process.env.CANNON_URL + "/auth";
6+
const FENIX_AUTH_URL = process.env.FENIX_URL + "/oauth/userdialog";
7+
const FENIX_TOKEN_URL = process.env.FENIX_URL + "/oauth/access_token";
8+
const FENIX_PROFILE_URL = process.env.FENIX_URL + "/api/fenix/v1/person";
9+
const FENIX_CALLBACK_URI = process.env.WEBAPP_URL + "/api/auth/callback/fenix";
10+
11+
export default {
12+
secret: process.env.NEXTAUTH_SECRET,
13+
providers: [
14+
GoogleProvider({
15+
clientId: process.env.GOOGLE_CLIENT_ID as string,
16+
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
17+
}),
18+
LinkedInProvider({
19+
clientId: process.env.LINKEDIN_CLIENT_ID as string,
20+
clientSecret: process.env.LINKEDIN_CLIENT_SECRET as string,
21+
}),
22+
{
23+
id: "microsoft",
24+
name: "Microsoft",
25+
type: "oauth",
26+
idToken: true,
27+
wellKnown:
28+
"https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration",
29+
authorization: {
30+
params: { scope: "openid email" },
31+
},
32+
async profile(profile) {
33+
return {
34+
id: profile.sub,
35+
email: profile.email,
36+
};
37+
},
38+
clientId: process.env.MICROSOFT_CLIENT_ID as string,
39+
clientSecret: process.env.MICROSOFT_CLIENT_SECRET as string,
40+
},
41+
{
42+
id: "fenix",
43+
name: "Fenix",
44+
type: "oauth",
45+
authorization: {
46+
url: FENIX_AUTH_URL,
47+
params: { scope: "" },
48+
},
49+
token: {
50+
async request({ params }) {
51+
if (params.code) {
52+
const url =
53+
FENIX_TOKEN_URL +
54+
"?" +
55+
new URLSearchParams({
56+
client_id: process.env.FENIX_CLIENT_ID as string,
57+
client_secret: process.env.FENIX_CLIENT_SECRET as string,
58+
redirect_uri: FENIX_CALLBACK_URI,
59+
grant_type: "authorization_code",
60+
code: params.code,
61+
});
62+
const resp = await fetch(url, {
63+
method: "POST",
64+
});
65+
if (resp.ok) {
66+
return { tokens: await resp.json() };
67+
}
68+
}
69+
return { tokens: {} };
70+
},
71+
},
72+
userinfo: FENIX_PROFILE_URL,
73+
async profile(profile) {
74+
return {
75+
id: profile.username,
76+
name: profile.name,
77+
email: profile.email,
78+
image: `https://fenix.tecnico.ulisboa.pt/user/photo/${profile.username}`,
79+
};
80+
},
81+
clientId: process.env.FENIX_CLIENT_ID as string,
82+
clientSecret: process.env.FENIX_CLIENT_SECRET as string,
83+
},
84+
],
85+
callbacks: {
86+
async redirect() {
87+
return "/";
88+
},
89+
async jwt({ token, user, account }) {
90+
// The arguments user, account and profile are only passed the first time this callback is called
91+
// on a new session, after the user signs in. In subsequent calls, only token will be available.
92+
if (user) {
93+
const url = CANNON_AUTH_ENDPOINT + "/" + account?.provider;
94+
const resp = await fetch(url, {
95+
method: "POST",
96+
headers: {
97+
"Content-Type": "application/json",
98+
},
99+
body: JSON.stringify({ accessToken: account?.access_token }),
100+
});
101+
if (resp.ok) {
102+
token.cannonToken = (await resp.json()).token;
103+
token.loginWith = account?.provider ?? "";
104+
}
105+
}
106+
return token;
107+
},
108+
async session({ token, session }) {
109+
session.cannonToken = token.cannonToken;
110+
session.loginWith = token.loginWith;
111+
return session;
112+
},
113+
},
114+
} as NextAuthOptions;
Lines changed: 2 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,5 @@
1-
import NextAuth, { NextAuthOptions } from "next-auth";
2-
import GoogleProvider from "next-auth/providers/google";
3-
import LinkedInProvider from "next-auth/providers/linkedin";
4-
5-
const CANNON_AUTH_ENDPOINT = process.env.CANNON_URL + "/auth";
6-
const FENIX_AUTH_URL = process.env.FENIX_URL + "/oauth/userdialog";
7-
const FENIX_TOKEN_URL = process.env.FENIX_URL + "/oauth/access_token";
8-
const FENIX_PROFILE_URL = process.env.FENIX_URL + "/api/fenix/v1/person";
9-
const FENIX_CALLBACK_URI = process.env.WEBAPP_URL + "/api/auth/callback/fenix";
10-
11-
export const authOptions: NextAuthOptions = {
12-
secret: process.env.NEXTAUTH_SECRET,
13-
providers: [
14-
GoogleProvider({
15-
clientId: process.env.GOOGLE_CLIENT_ID as string,
16-
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
17-
}),
18-
LinkedInProvider({
19-
clientId: process.env.LINKEDIN_CLIENT_ID as string,
20-
clientSecret: process.env.LINKEDIN_CLIENT_SECRET as string,
21-
}),
22-
{
23-
id: "microsoft",
24-
name: "Microsoft",
25-
type: "oauth",
26-
idToken: true,
27-
wellKnown:
28-
"https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration",
29-
authorization: {
30-
params: { scope: "openid email" },
31-
},
32-
async profile(profile) {
33-
return {
34-
id: profile.sub,
35-
email: profile.email,
36-
};
37-
},
38-
clientId: process.env.MICROSOFT_CLIENT_ID as string,
39-
clientSecret: process.env.MICROSOFT_CLIENT_SECRET as string,
40-
},
41-
{
42-
id: "fenix",
43-
name: "Fenix",
44-
type: "oauth",
45-
authorization: {
46-
url: FENIX_AUTH_URL,
47-
params: { scope: "" },
48-
},
49-
token: {
50-
async request({ params }) {
51-
if (params.code) {
52-
const url =
53-
FENIX_TOKEN_URL +
54-
"?" +
55-
new URLSearchParams({
56-
client_id: process.env.FENIX_CLIENT_ID as string,
57-
client_secret: process.env.FENIX_CLIENT_SECRET as string,
58-
redirect_uri: FENIX_CALLBACK_URI,
59-
grant_type: "authorization_code",
60-
code: params.code,
61-
});
62-
const resp = await fetch(url, {
63-
method: "POST",
64-
});
65-
if (resp.ok) {
66-
return { tokens: await resp.json() };
67-
}
68-
}
69-
return { tokens: {} };
70-
},
71-
},
72-
userinfo: FENIX_PROFILE_URL,
73-
async profile(profile) {
74-
return {
75-
id: profile.username,
76-
name: profile.name,
77-
email: profile.email,
78-
image: `https://fenix.tecnico.ulisboa.pt/user/photo/${profile.username}`,
79-
};
80-
},
81-
clientId: process.env.FENIX_CLIENT_ID as string,
82-
clientSecret: process.env.FENIX_CLIENT_SECRET as string,
83-
},
84-
],
85-
callbacks: {
86-
async redirect() {
87-
return "/";
88-
},
89-
async jwt({ token, user, account }) {
90-
// The arguments user, account and profile are only passed the first time this callback is called
91-
// on a new session, after the user signs in. In subsequent calls, only token will be available.
92-
if (user) {
93-
const url = CANNON_AUTH_ENDPOINT + "/" + account?.provider;
94-
const resp = await fetch(url, {
95-
method: "POST",
96-
headers: {
97-
"Content-Type": "application/json",
98-
},
99-
body: JSON.stringify({ accessToken: account?.access_token }),
100-
});
101-
if (resp.ok) {
102-
token.cannonToken = (await resp.json()).token;
103-
token.loginWith = account?.provider ?? "";
104-
}
105-
}
106-
return token;
107-
},
108-
async session({ token, session }) {
109-
session.cannonToken = token.cannonToken;
110-
session.loginWith = token.loginWith;
111-
return session;
112-
},
113-
},
114-
};
1+
import NextAuth from "next-auth";
2+
import authOptions from "./authOptions";
1153

1164
const handler = NextAuth(authOptions);
1175
export { handler as GET, handler as POST };

src/app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import "@/styles/globals.css";
77
import Navbar from "@/components/Navbar";
88
import BottomNavbar from "@/components/BottomNavbar";
99
import AuthProvider from "@/context/AuthProvider";
10-
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
10+
import authOptions from "@/app/api/auth/[...nextauth]/authOptions";
1111

1212
const montserrat = Montserrat({ subsets: ["latin"] });
1313

src/app/my-cv/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getServerSession } from "next-auth";
55
import MyCVClient from "./MyCVClient";
66
import cvIcon from "@/assets/icons/cv.png";
77
import UserSignOut from "@/components/UserSignOut";
8-
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
8+
import authOptions from "@/app/api/auth/[...nextauth]/authOptions";
99
import { UserService } from "@/services/UserService";
1010

1111
export default async function UploadCV() {

src/components/BottomNavbar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { redirect } from "next/navigation";
22
import { getServerSession } from "next-auth/next";
33

44
import UserSignOut from "@/components/UserSignOut";
5-
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
5+
import authOptions from "@/app/api/auth/[...nextauth]/authOptions";
66
import { UserService } from "@/services/UserService";
77

88
import ClientBottomNavbar from "./ClientBottomNavbar";
@@ -13,7 +13,7 @@ export default async function BottomNavbar() {
1313

1414
const user: User = await UserService.getMe(session.cannonToken);
1515
if (!user) return <UserSignOut />;
16-
16+
1717
return (
1818
<ClientBottomNavbar user={user} ></ClientBottomNavbar>
1919
);

0 commit comments

Comments
 (0)