1
1
import { TRPCError } from '@trpc/server' ;
2
2
import dayjs from 'dayjs' ;
3
3
import { eq } from 'drizzle-orm/expressions' ;
4
+ import type { AdapterAccount } from 'next-auth/adapters' ;
4
5
import { DeepPartial } from 'utility-types' ;
5
6
6
7
import { LobeChatDatabase } from '@/database/type' ;
@@ -9,7 +10,14 @@ import { UserKeyVaults, UserSettings } from '@/types/user/settings';
9
10
import { merge } from '@/utils/merge' ;
10
11
import { today } from '@/utils/time' ;
11
12
12
- import { NewUser , UserItem , UserSettingsItem , userSettings , users } from '../../schemas' ;
13
+ import {
14
+ NewUser ,
15
+ UserItem ,
16
+ UserSettingsItem ,
17
+ nextauthAccounts ,
18
+ userSettings ,
19
+ users ,
20
+ } from '../../schemas' ;
13
21
import { SessionModel } from './session' ;
14
22
15
23
type DecryptUserKeyVaults = (
@@ -97,6 +105,25 @@ export class UserModel {
97
105
} ;
98
106
} ;
99
107
108
+ getUserSSOProviders = async ( ) => {
109
+ return this . db . query . nextauthAccounts
110
+ . findMany ( {
111
+ where : eq ( nextauthAccounts . userId , this . userId ) ,
112
+ } )
113
+ . then ( ( accounts ) =>
114
+ accounts . map ( ( account ) => {
115
+ // Pick necessary fields, don't expose the sensitive information
116
+ return {
117
+ expires_at : account ?. expires_at ,
118
+ provider : account ?. provider ,
119
+ providerAccountId : account ?. providerAccountId ,
120
+ scope : account ?. scope ,
121
+ type : account ?. type ,
122
+ } as unknown as AdapterAccount ;
123
+ } ) ,
124
+ ) as Promise < AdapterAccount [ ] > ;
125
+ } ;
126
+
100
127
getUserSettings = async ( ) => {
101
128
return this . db . query . userSettings . findFirst ( { where : eq ( userSettings . id , this . userId ) } ) ;
102
129
} ;
0 commit comments