|
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"; |
115 | 3 |
|
116 | 4 | const handler = NextAuth(authOptions);
|
117 | 5 | export { handler as GET, handler as POST };
|
0 commit comments