-
-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathtokens.js
More file actions
49 lines (39 loc) · 1.34 KB
/
tokens.js
File metadata and controls
49 lines (39 loc) · 1.34 KB
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
'use strict'
const tokens = require('../database/tokens')
const config = require('../utils/config')
const KnownError = require('../utils/KnownError')
const ignoreCookie = require('../utils/ignoreCookie')
const response = (entry) => ({
id: entry.id,
created: entry.created,
updated: entry.updated,
})
module.exports = {
Mutation: {
createToken: async (parent, { input }, { setCookies }) => {
// If anonymous mode then skip checking for a username and password and just make the token
if (!config.isAnonymousMode) {
const { username, password } = input
if (config.username == null) throw new KnownError('Ackee username missing in environment')
if (config.password == null) throw new KnownError('Ackee username missing in environment')
if (username !== config.username) throw new KnownError('Username or password incorrect')
if (password !== config.password) throw new KnownError('Username or password incorrect')
}
const entry = await tokens.add()
// Set cookie to avoid reporting your own visits
setCookies.push(ignoreCookie.on)
return {
success: true,
payload: response(entry),
}
},
deleteToken: async (parent, { id }, { setCookies }) => {
await tokens.del(id)
// Remove cookie to report your own visits, again
setCookies.push(ignoreCookie.off)
return {
success: true,
}
},
},
}