-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
116 lines (110 loc) · 2.88 KB
/
auth.ts
File metadata and controls
116 lines (110 loc) · 2.88 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { headers } from "next/headers";
import NextAuth, { DefaultSession } from "next-auth";
import Google from "next-auth/providers/google";
import { createApolloClient } from "./lib/apollo";
import { gql } from "@apollo/client";
import { cookies } from "next/headers";
declare module "next-auth" {
interface Session {
user: {
id: string;
email: string;
name?: string | null;
} & DefaultSession["user"];
accessToken?: string;
}
interface JWT {
sub?: string;
accessToken?: string;
}
}
const GOOGLE_AUTH_MUTATION = gql`
mutation GoogleAuth($input: GoogleAuthInput!) {
googleAuth(input: $input) {
success
token
user {
_id
email
first_name
last_name
}
}
}
`;
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Google({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
authorization: {
params: {
prompt: "consent",
access_type: "offline",
response_type: "code",
},
},
}),
],
callbacks: {
async signIn({ user, account }) {
if (account?.provider === "google") {
try {
const apollo = createApolloClient();
const { data } = await apollo.mutate({
mutation: GOOGLE_AUTH_MUTATION,
variables: {
input: {
email: user.email || "",
provider: "google",
google_id: account.providerAccountId || "",
picture: user.image || "",
access_token: account.access_token || "",
id_token: account.id_token || "",
},
},
});
const cookieStore = cookies();
if (data?.googleAuth?.success) {
cookieStore.set("token", data.googleAuth.token);
return true;
} else {
return false;
}
} catch (error: any) {
console.error("Auth error details:", {
message: error.message,
graphQLErrors: error.graphQLErrors,
networkError: error.networkError,
stack: error.stack,
});
return false;
}
}
return true;
},
async session({ session, token }) {
if (token && session.user) {
session.user.id = (token.sub as string) || "";
session.accessToken = (token.accessToken as string) || "";
}
return session;
},
async jwt({ token, account }) {
if (account) {
token.accessToken = account.access_token;
}
return token;
},
},
pages: {
signIn: "/auth/auth/sign-in",
error: "/auth/error",
},
session: {
strategy: "jwt",
maxAge: 30 * 24 * 60 * 60, // 30 days
},
secret: process.env.NEXTAUTH_SECRET,
debug: process.env.NODE_ENV === "development",
});