forked from prisma/prisma1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.js
More file actions
81 lines (72 loc) · 2.15 KB
/
signup.js
File metadata and controls
81 lines (72 loc) · 2.15 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const fromEvent = require('graphcool-lib').fromEvent
const bcryptjs = require('bcryptjs')
const validator = require('validator')
const userQuery = `
query UserQuery($email: String!) {
User(email: $email){
id
password
}
}`
const createUserMutation = `
mutation CreateUserMutation($email: String!, $passwordHash: String!) {
createUser(
email: $email,
password: $passwordHash
) {
id
}
}`
const getGraphcoolUser = (api, email) => {
return api.request(userQuery, { email })
.then(userQueryResult => {
if (userQueryResult.error) {
return Promise.reject(userQueryResult.error)
} else {
return userQueryResult.User
}
})
}
const createGraphcoolUser = (api, email, passwordHash) => {
return api.request(createUserMutation, { email, passwordHash })
.then(userMutationResult => {
return userMutationResult.createUser.id
})
}
module.exports = function(event) {
if (!event.context.graphcool.pat) {
console.log('Please provide a valid root token!')
return { error: 'Email Signup not configured correctly.'}
}
// Retrieve payload from event
const email = event.data.email
const password = event.data.password
// Create Graphcool API (based on https://github.com/graphcool/graphql-request)
const graphcool = fromEvent(event)
const api = graphcool.api('simple/v1')
const SALT_ROUNDS = 10
if (validator.isEmail(email)) {
return getGraphcoolUser(api, email)
.then(graphcoolUser => {
if (!graphcoolUser) {
return bcryptjs.hash(password, SALT_ROUNDS)
.then(hash => createGraphcoolUser(api, email, hash))
} else {
return Promise.reject('Email already in use')
}
})
.then(graphcoolUserId => {
return graphcool.generateAuthToken(graphcoolUserId, 'User')
.then(token => {
return { data: {id: graphcoolUserId, token}}
})
})
.catch(error => {
// Log error, but don't expose to caller
console.log(`Error: ${JSON.stringify(error)}`)
return { error: 'An unexpected error occured.' }
})
} else {
return { error: 'Not a valid email' }
}
}