File size: 2,408 Bytes
b841f1a
 
 
5ec491a
 
3ba9c0c
 
f80b091
 
 
 
 
 
3ba9c0c
 
38448fc
 
3ba9c0c
f80b091
 
3ba9c0c
f80b091
 
 
 
 
 
 
 
5ec491a
 
 
 
04735a9
5ec491a
 
 
 
 
04735a9
5ec491a
 
 
 
 
 
 
 
f80b091
 
 
 
 
 
a1c5622
 
 
 
 
04735a9
a1c5622
04735a9
5ec491a
a1c5622
f80b091
 
 
 
38448fc
 
 
 
 
 
f80b091
 
 
 
 
b841f1a
bfbf1a7
5ec491a
bfbf1a7
5ec491a
9333689
 
 
5ec491a
 
9333689
bfbf1a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import NextAuth, { type DefaultSession } from 'next-auth';
import GitHub from 'next-auth/providers/github';
import Google from 'next-auth/providers/google';
import { dbFindOrCreateUser } from './lib/db/functions';
import { redirect } from 'next/navigation';

declare module 'next-auth' {
  interface Session {
    user: {
      /** The user's id. */
      id: string;
    } & DefaultSession['user'];
  }
}

const restrictedPath = ['/project'];

export const {
  handlers: { GET, POST },
  auth,
} = NextAuth({
  providers: [
    GitHub,
    Google({
      clientId: process.env.GOOGLE_CLIENT_ID!,
      clientSecret: process.env.GOOGLE_SECRET!,
    }),
  ],
  callbacks: {
    async signIn({ profile, user }) {
      if (!profile) {
        return false;
      }
      const { email, name, picture } = profile;

      if (!email || !name) {
        return false;
      }

      const dbUser = await dbFindOrCreateUser(email, name, picture);

      if (dbUser) {
        user.id = dbUser.id;
        return true;
      }
      return false;
    },
    async jwt({ token, profile, user }) {
      if (profile) {
        token.id = profile.id || profile.sub;
        token.image = profile.avatar_url || profile.picture;
      }
      return token;
    },
    async session({ session, token }) {
      // TODO: this is temporary between we switch DB and make migration
      // so also UI might still have session, DB might already have cleaned up
      const email = session?.user?.email;
      const name = session?.user?.name;
      const avatar = session?.user?.image;
      if (email && name) {
        const dbUser = await dbFindOrCreateUser(email, name, avatar);
        // put db user id into session
        session.user.id = dbUser.id;
      }
      return session;
    },
    authorized({ request, auth }) {
      const isAdmin = !!auth?.user?.email?.endsWith('landing.ai');
      return restrictedPath.find(path =>
        request.nextUrl.pathname.startsWith(path),
      )
        ? isAdmin
        : true;
    },
  },
  pages: {
    signIn: '/sign-in', // overrides the next-auth default signin page https://authjs.dev/guides/basics/pages
  },
});

export async function sessionUser() {
  const session = await auth();
  const email = session?.user.email;
  return {
    email,
    isAdmin: !!email?.endsWith('landing.ai'),
    id: session?.user.id ?? null,
    user: session?.user ?? null,
  };
}