Skip to content

Commit ee00939

Browse files
author
Ian Davis
authored
Add findUserByEmail (#55)
* Add findUserByEmail This index already exists, there is just no way to read from it. * Add unit test for findUserByEmail
1 parent df25d32 commit ee00939

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/user-store.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,27 @@ class UserStore {
273273
})
274274
}
275275

276+
/**
277+
* Loads and returns a user object for a given email.
278+
*
279+
* @param email {string}
280+
*
281+
* @return {Promise<Object>} User info, parsed from a JSON string
282+
*/
283+
findUserByEmail (email) {
284+
const emailKey = UserStore.normalizeEmailKey(email)
285+
286+
return this.backend.get('users-by-email', emailKey)
287+
.then(user => {
288+
if (user && user.link) {
289+
// this is an alias record, fetch the user it points to
290+
return this.findUser(user.link)
291+
}
292+
293+
return user
294+
})
295+
}
296+
276297
/**
277298
* Creates and returns a salted password hash, for storage with the user
278299
* record.

test/unit/user-store-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ describe('UserStore', () => {
192192
})
193193
})
194194

195+
it('should look up user record by normalized email', () => {
196+
const email = '[email protected]'
197+
const user = { id: 'abc', email: email }
198+
199+
store.backend.get = sinon.stub().resolves(user)
200+
201+
return store.findUserByEmail(email)
202+
.then(fetchedUser => {
203+
expect(fetchedUser).to.equal(user)
204+
205+
expect(store.backend.get).to.have.been
206+
.calledWith('users-by-email', 'alice%40example.com')
207+
})
208+
})
209+
195210
it('should look up user record via an alias record', () => {
196211
const aliasId = 'alice.solidtest.space/profile/card#me'
197212
const aliasKey = 'alice.solidtest.space%2Fprofile%2Fcard%23me'

0 commit comments

Comments
 (0)