This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetUserByEmail.js
70 lines (63 loc) · 1.59 KB
/
getUserByEmail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const Nano = require('nano')
const debug = require('debug')('choirless')
let nano = null
let db = null
// fetch a user with email
// Parameters:
// - email - the email address of the user
const getUserByEmail = async (opts) => {
// connect to db - reuse connection if present
if (!db) {
nano = Nano(process.env.COUCH_URL)
db = nano.db.use(process.env.COUCH_USERS_DATABASE)
}
// check for mandatory parameters
if (!opts.email) {
return {
body: { ok: false, message: 'missing mandatory parameters' },
statusCode: 400,
headers: { 'Content-Type': 'application/json' }
}
}
// fetch user from database
let statusCode = 200
let body = null
try {
const query = {
selector: {
email: opts.email
}
}
debug('postUserLogin', query)
const result = await db.find(query)
const doc = result.docs ? result.docs[0] : null
// if there is a doc for this email address
if (doc) {
// form the response
body = {
ok: true,
user: doc
}
// don't show stored password & salt
delete body.user.password
delete body.user.salt
delete body.user._id
delete body.user._rev
// infer userType if missing
body.user.userType = body.user.userType ? body.user.userType : 'regular'
} else {
body = { ok: false }
statusCode = 404
}
} catch (e) {
body = { ok: false }
statusCode = 404
}
// return API response
return {
body: body,
statusCode: statusCode,
headers: { 'Content-Type': 'application/json' }
}
}
module.exports = getUserByEmail