@@ -83,7 +83,7 @@ export const defaultOptions = {
8383 sessionByUserIdKeyPrefix : "user:session:by-user-id:" ,
8484 userKeyPrefix : "user:" ,
8585 verificationTokenKeyPrefix : "user:token:" ,
86- authenticatorKeyPrefix : "authenticator:id: " ,
86+ authenticatorKeyPrefix : "authenticator:" ,
8787 authenticatorUserKeyPrefix : "authenticator:by-user-id:" ,
8888 useItemRaw : false ,
8989}
@@ -94,7 +94,7 @@ function isDate(value: any) {
9494 return value && isoDateRE . test ( value ) && ! isNaN ( Date . parse ( value ) )
9595}
9696
97- export function hydrateDates ( json : object ) {
97+ export function hydrateDates ( json : Record < string , any > ) {
9898 return Object . entries ( json ) . reduce ( ( acc , [ key , val ] ) => {
9999 acc [ key ] = isDate ( val ) ? new Date ( val as string ) : val
100100 return acc
@@ -134,15 +134,6 @@ export function UnstorageAdapter(
134134 }
135135 }
136136
137- async function getItems ( key : string [ ] ) {
138- if ( mergedOptions . useItemRaw ) {
139- // Unstorage missing method to get multiple items raw, i.e. `getItemsRaw`
140- return JSON . stringify ( await storage . getItems ( key ) )
141- } else {
142- return await storage . getItems ( key )
143- }
144- }
145-
146137 async function setItem ( key : string , value : string ) {
147138 if ( mergedOptions . useItemRaw ) {
148139 return await storage . setItemRaw ( key , value )
@@ -151,7 +142,7 @@ export function UnstorageAdapter(
151142 }
152143 }
153144
154- const setObjectAsJson = async ( key : string , obj : any ) => {
145+ const setObjectAsJson = async ( key : string , obj : Record < string , any > ) => {
155146 if ( mergedOptions . useItemRaw ) {
156147 await storage . setItemRaw ( key , obj )
157148 } else {
@@ -213,11 +204,21 @@ export function UnstorageAdapter(
213204 credentialId : string ,
214205 authenticator : AdapterAuthenticator
215206 ) : Promise < AdapterAuthenticator > => {
207+ let newCredsToSet = [ credentialId ]
208+
209+ const getItemReturn = await getItem < string [ ] > (
210+ `${ authenticatorUserKeyPrefix } ${ authenticator . userId } `
211+ )
212+
213+ if ( getItemReturn && getItemReturn [ 0 ] !== newCredsToSet [ 0 ] ) {
214+ newCredsToSet . push ( ...getItemReturn )
215+ }
216+
216217 await Promise . all ( [
217218 setObjectAsJson ( authenticatorKeyPrefix + credentialId , authenticator ) ,
218219 setItem (
219220 `${ authenticatorUserKeyPrefix } ${ authenticator . userId } ` ,
220- credentialId
221+ JSON . stringify ( newCredsToSet )
221222 ) ,
222223 ] )
223224 return authenticator
@@ -231,24 +232,19 @@ export function UnstorageAdapter(
231232 return hydrateDates ( authenticator )
232233 }
233234
234- // TODO: This one doesn't really work with KV storage, as we can't set the same
235- // key multiple times, they'll just overwrite one another. Maybe with some
236- // additional logic to write an array as the value instead of overwriting
237- // the pre-existing value. Probably in `setItems` implementation.
238235 const getAuthenticatorByUserId = async (
239236 userId : string
240237 ) : Promise < AdapterAuthenticator [ ] | [ ] > => {
241- const credentialIds = await getItems ( [
242- `${ authenticatorUserKeyPrefix } ${ userId } ` ,
243- ] )
244- if ( ! credentialIds . length ) return [ ]
238+ const credentialIds = await getItem < string [ ] > (
239+ `${ authenticatorUserKeyPrefix } ${ userId } `
240+ )
245241
246- const authenticators = [ ]
247- for ( const credentialId of credentialIds ) {
248- const credentialValue =
249- typeof credentialId === "string" ? credentialId : credentialId . value
242+ if ( ! credentialIds ) return [ ]
250243
251- const authenticator = await getAuthenticator ( credentialValue as string )
244+ const authenticators : AdapterAuthenticator [ ] = [ ]
245+
246+ for ( const credentialId of credentialIds ) {
247+ const authenticator = await getAuthenticator ( credentialId )
252248
253249 if ( authenticator ) {
254250 hydrateDates ( authenticator )
@@ -362,36 +358,22 @@ export function UnstorageAdapter(
362358 ] )
363359 } ,
364360 async createAuthenticator ( authenticator ) {
365- setAuthenticator ( authenticator . credentialID , authenticator )
366- return fromDBAuthenticator ( authenticator ) !
361+ await setAuthenticator ( authenticator . credentialID , authenticator )
362+ return authenticator
367363 } ,
368364 async getAuthenticator ( credentialID ) {
369- const authenticator = await getAuthenticator ( credentialID )
370- return fromDBAuthenticator ( authenticator )
365+ return getAuthenticator ( credentialID )
371366 } ,
372367 async listAuthenticatorsByUserId ( userId ) {
373368 const user = await getUser ( userId )
374369 if ( ! user ) return [ ]
375- const authenticators = await getAuthenticatorByUserId ( user . id )
376- return authenticators
370+ return getAuthenticatorByUserId ( user . id )
377371 } ,
378372 async updateAuthenticatorCounter ( credentialID , counter ) {
379373 const authenticator = await getAuthenticator ( credentialID )
380374 authenticator . counter = Number ( counter )
381- setAuthenticator ( credentialID , authenticator )
382- return fromDBAuthenticator ( authenticator ) !
375+ await setAuthenticator ( credentialID , authenticator )
376+ return authenticator
383377 } ,
384378 }
385379}
386-
387- function fromDBAuthenticator (
388- authenticator : AdapterAuthenticator & { id ?: string ; user ?: string }
389- ) : AdapterAuthenticator | null {
390- if ( ! authenticator ) return null
391- const { transports, id, user, ...other } = authenticator
392-
393- return {
394- ...other ,
395- transports : transports || undefined ,
396- }
397- }
0 commit comments