@@ -19,6 +19,31 @@ const Adapter = (config, options = {}) => {
19
19
const EmailVerification = options . EmailVerification ? options . EmailVerification . model : Models . EmailVerification . model
20
20
const EmailVerificationSchema = options . EmailVerification ? options . EmailVerification . schema : Models . EmailVerification . schema
21
21
22
+ // Models default to being suitable for ANSI SQL database
23
+ // Some flexiblity is required to support non-SQL databases
24
+ const idKey = 'id'
25
+ const getById = ( id ) => id
26
+
27
+ /* @TODO This is a work in progress
28
+ // Some custom logic is required to make schemas compatible with MongoDB
29
+ if (config.type === 'mongodb') {
30
+ if (!options.mongodb) throw new Error('Experimental feature')
31
+
32
+ idKey = 'id'
33
+ AccountSchema.columns.id.objectId = true
34
+ AccountSchema.columns.userId.objectId = true
35
+ UserSchema.columns.id.objectId = true
36
+ SessionSchema.columns.id.objectId = true
37
+ SessionSchema.columns.userId.objectId = true
38
+ EmailVerificationSchema.columns.id.objectId = true
39
+
40
+ getById = (id) => {
41
+ console.log('fancy getById', id)
42
+ return config.mongodb.ObjectId(id)
43
+ }
44
+ }
45
+ */
46
+
22
47
// Parse config (uses options)
23
48
const defaultConfig = {
24
49
name : 'default' ,
@@ -71,12 +96,12 @@ const Adapter = (config, options = {}) => {
71
96
// Display debug output if debug option enabled
72
97
function _debug ( ...args ) {
73
98
if ( appOptions . debug ) {
74
- console . log ( '[DEBUG]' , ...args )
99
+ console . log ( '[NextAuth.js][ DEBUG]' , ...args )
75
100
}
76
101
}
77
102
78
103
async function createUser ( profile ) {
79
- _debug ( 'Create user ' , profile )
104
+ _debug ( 'createUser ' , profile )
80
105
try {
81
106
// Create user account
82
107
const user = new User ( profile . name , profile . email , profile . image )
@@ -88,17 +113,17 @@ const Adapter = (config, options = {}) => {
88
113
}
89
114
90
115
async function getUser ( id ) {
91
- _debug ( 'Get user ' , id )
116
+ _debug ( 'getUser ' , id )
92
117
try {
93
- return connection . getRepository ( User ) . findOne ( { id } )
118
+ return connection . getRepository ( User ) . findOne ( { [ idKey ] : getById ( id ) } )
94
119
} catch ( error ) {
95
120
console . error ( 'GET_USER_BY_ID_ERROR' , error )
96
121
return Promise . reject ( new Error ( 'GET_USER_BY_ID_ERROR' , error ) )
97
122
}
98
123
}
99
124
100
125
async function getUserByEmail ( email ) {
101
- _debug ( 'Get user by email address ' , email )
126
+ _debug ( 'getUserByEmail ' , email )
102
127
try {
103
128
return connection . getRepository ( User ) . findOne ( { email } )
104
129
} catch ( error ) {
@@ -108,37 +133,37 @@ const Adapter = (config, options = {}) => {
108
133
}
109
134
110
135
async function getUserByProviderAccountId ( providerId , providerAccountId ) {
111
- _debug ( 'Get user by provider account ID ' , providerId , providerAccountId )
136
+ _debug ( 'getUserByProviderAccountId ' , providerId , providerAccountId )
112
137
try {
113
138
const account = await connection . getRepository ( Account ) . findOne ( { providerId, providerAccountId } )
114
139
if ( ! account ) { return null }
115
- return connection . getRepository ( User ) . findOne ( { id : account . userId } )
140
+ return connection . getRepository ( User ) . findOne ( { [ idKey ] : getById ( account . userId ) } )
116
141
} catch ( error ) {
117
142
console . error ( 'GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR' , error )
118
143
return Promise . reject ( new Error ( 'GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR' , error ) )
119
144
}
120
145
}
121
146
122
147
async function getUserByCredentials ( credentials ) {
123
- _debug ( 'Get user by credentials ' , credentials )
148
+ _debug ( 'getUserByCredentials ' , credentials )
124
149
// @TODO Get user from DB
125
150
return false
126
151
}
127
152
128
153
async function updateUser ( user ) {
129
- _debug ( 'Update user ' , user )
154
+ _debug ( 'updateUser ' , user )
130
155
// @TODO Save changes to user object in DB
131
156
return false
132
157
}
133
158
134
159
async function deleteUser ( userId ) {
135
- _debug ( 'Delete user ' , userId )
160
+ _debug ( 'deleteUser ' , userId )
136
161
// @TODO Delete user from DB
137
162
return false
138
163
}
139
164
140
165
async function linkAccount ( userId , providerId , providerType , providerAccountId , refreshToken , accessToken , accessTokenExpires ) {
141
- _debug ( 'Link provider account ' , userId , providerId , providerType , providerAccountId , refreshToken , accessToken , accessTokenExpires )
166
+ _debug ( 'linkAccount ' , userId , providerId , providerType , providerAccountId , refreshToken , accessToken , accessTokenExpires )
142
167
try {
143
168
// Create provider account linked to user
144
169
const account = new Account ( userId , providerId , providerType , providerAccountId , refreshToken , accessToken , accessTokenExpires )
@@ -150,15 +175,15 @@ const Adapter = (config, options = {}) => {
150
175
}
151
176
152
177
async function unlinkAccount ( userId , providerId , providerAccountId ) {
153
- _debug ( 'Unlink provider account ' , userId , providerId , providerAccountId )
178
+ _debug ( 'unlinkAccount ' , userId , providerId , providerAccountId )
154
179
// @TODO Get current user from DB
155
180
// @TODO Delete [provider] object from user object
156
181
// @TODO Save changes to user object in DB
157
182
return false
158
183
}
159
184
160
185
async function createSession ( user ) {
161
- _debug ( 'Create session ' , user )
186
+ _debug ( 'createSession ' , user )
162
187
try {
163
188
const { sessionMaxAge } = appOptions
164
189
let expires = null
@@ -178,7 +203,7 @@ const Adapter = (config, options = {}) => {
178
203
}
179
204
180
205
async function getSession ( sessionToken ) {
181
- _debug ( 'Get session ' , sessionToken )
206
+ _debug ( 'getSession ' , sessionToken )
182
207
try {
183
208
const session = await connection . getRepository ( Session ) . findOne ( { sessionToken } )
184
209
@@ -196,7 +221,7 @@ const Adapter = (config, options = {}) => {
196
221
}
197
222
198
223
async function updateSession ( session , force ) {
199
- _debug ( 'Update session ' , session )
224
+ _debug ( 'updateSession ' , session )
200
225
try {
201
226
const { sessionMaxAge, sessionUpdateAge } = appOptions
202
227
@@ -216,7 +241,7 @@ const Adapter = (config, options = {}) => {
216
241
if ( new Date ( ) > dateSessionIsDueToBeUpdated ) {
217
242
const newExpiryDate = new Date ( )
218
243
newExpiryDate . setTime ( newExpiryDate . getTime ( ) + sessionMaxAge )
219
- session . sessionExpires = newExpiryDate . toISOString ( )
244
+ session . sessionExpires = newExpiryDate
220
245
} else if ( ! force ) {
221
246
return null
222
247
}
@@ -234,7 +259,7 @@ const Adapter = (config, options = {}) => {
234
259
}
235
260
236
261
async function deleteSession ( sessionToken ) {
237
- _debug ( 'Delete session ' , sessionToken )
262
+ _debug ( 'deleteSession ' , sessionToken )
238
263
try {
239
264
return await connection . getRepository ( Session ) . delete ( { sessionToken } )
240
265
} catch ( error ) {
@@ -244,7 +269,7 @@ const Adapter = (config, options = {}) => {
244
269
}
245
270
246
271
async function createEmailVerification ( email , url , token , secret , provider ) {
247
- _debug ( 'Create verification request ' , email )
272
+ _debug ( 'createEmailVerification ' , email )
248
273
try {
249
274
const { site, verificationMaxAge } = appOptions
250
275
const { verificationCallback } = provider
@@ -277,7 +302,7 @@ const Adapter = (config, options = {}) => {
277
302
}
278
303
279
304
async function getEmailVerification ( email , token , secret , provider ) {
280
- _debug ( 'Get verification request ' , email , token )
305
+ _debug ( 'getEmailVerification ' , email , token )
281
306
try {
282
307
// Hash token provided with secret before trying to match it with datbase
283
308
// @TODO Use bcrypt function here instead of simple salted hash
@@ -298,7 +323,7 @@ const Adapter = (config, options = {}) => {
298
323
}
299
324
300
325
async function deleteEmailVerification ( email , token , secret , provider ) {
301
- _debug ( 'Delete verification request ' , email , token )
326
+ _debug ( 'deleteEmailVerification ' , email , token )
302
327
try {
303
328
// Delete email verification so it cannot be used again
304
329
const hashedToken = createHash ( 'sha256' ) . update ( `${ token } ${ secret } ` ) . digest ( 'hex' )
0 commit comments