11import NextAuth from "next-auth"
22import GitHub from "next-auth/providers/github"
3+ import Google from "next-auth/providers/google"
34import { DrizzleAdapter } from "@auth/drizzle-adapter"
45import { createDb , Db } from "./db"
56import { accounts , users , roles , userRoles } from "./schema"
@@ -30,7 +31,7 @@ const getDefaultRole = async (): Promise<Role> => {
3031 ) {
3132 return defaultRole as Role
3233 }
33-
34+
3435 return ROLES . CIVILIAN
3536}
3637
@@ -102,6 +103,12 @@ export const {
102103 GitHub ( {
103104 clientId : process . env . AUTH_GITHUB_ID ,
104105 clientSecret : process . env . AUTH_GITHUB_SECRET ,
106+ allowDangerousEmailAccountLinking : true ,
107+ } ) ,
108+ Google ( {
109+ clientId : process . env . AUTH_GOOGLE_ID ,
110+ clientSecret : process . env . AUTH_GOOGLE_SECRET ,
111+ allowDangerousEmailAccountLinking : true ,
105112 } ) ,
106113 CredentialsProvider ( {
107114 name : "Credentials" ,
@@ -119,7 +126,7 @@ export const {
119126 let parsedCredentials : AuthSchema
120127 try {
121128 parsedCredentials = authSchema . parse ( { username, password, turnstileToken } )
122- // eslint-disable-next-line @typescript-eslint/no-unused-vars
129+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
123130 } catch ( error ) {
124131 throw new Error ( "输入格式不正确" )
125132 }
@@ -196,7 +203,7 @@ export const {
196203 where : eq ( userRoles . userId , session . user . id ) ,
197204 with : { role : true } ,
198205 } )
199-
206+
200207 if ( ! userRoleRecords . length ) {
201208 const defaultRole = await getDefaultRole ( )
202209 const role = await findOrCreateRole ( db , defaultRole )
@@ -208,10 +215,16 @@ export const {
208215 role : role
209216 } ]
210217 }
211-
218+
212219 session . user . roles = userRoleRecords . map ( ur => ( {
213220 name : ur . role . name ,
214221 } ) )
222+
223+ const userAccounts = await db . query . accounts . findMany ( {
224+ where : eq ( accounts . userId , session . user . id ) ,
225+ } )
226+
227+ session . user . providers = userAccounts . map ( account => account . provider )
215228 }
216229
217230 return session
@@ -224,7 +237,7 @@ export const {
224237
225238export async function register ( username : string , password : string ) {
226239 const db = createDb ( )
227-
240+
228241 const existing = await db . query . users . findFirst ( {
229242 where : eq ( users . username , username )
230243 } )
@@ -234,7 +247,7 @@ export async function register(username: string, password: string) {
234247 }
235248
236249 const hashedPassword = await hashPassword ( password )
237-
250+
238251 const [ user ] = await db . insert ( users )
239252 . values ( {
240253 username,
0 commit comments