Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 07a243d

Browse files
committed
refactor: reused code frrom identity-signup
1 parent b59cd88 commit 07a243d

File tree

2 files changed

+47
-75
lines changed

2 files changed

+47
-75
lines changed

functions/identity-external-signup.js

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,20 @@ an Authorisation header that has a valid JWT
88

99
"use strict";
1010
const fetch = require("node-fetch");
11-
const faunadb = require("faunadb");
12-
const generator = require('generate-password');
13-
14-
/* configure faunaDB Client with our secret
15-
DB Secret key is held within the netlify online UI
16-
*/
17-
const q = faunadb.query
18-
const client = new faunadb.Client({
19-
secret: process.env.FAUNADB_SERVER_SECRET
20-
})
21-
22-
/* create a user in FaunaDB that can connect from the browser */
23-
/**
24-
*
25-
* @param {object} userData
26-
* @property {string} userData.id - netlify id nunmber
27-
* @property {object} userData.user_metadata - additonal arbitary
28-
* @param {string} password
29-
*/
30-
function createDbUser(userData, password) {
31-
return client.query(q.Create(q.Collection("users"), {
32-
credentials : {
33-
password : password
34-
},
35-
data : {
36-
id : userData.id,
37-
user_metadata : userData.user_metadata
38-
}
39-
}))
40-
}
41-
42-
function obtainToken(user, password) {
43-
return client.query(
44-
q.Login(q.Select("ref", user), { password }))
45-
}
11+
const identitySignup = require('./identity-signup')
4612

4713
/**
4814
* Update the app_metadata for a netlify user to include add the faunaDB token
49-
*
5015
* @param {object} appMetaDataObject - object containing any additional arbitary data for the user
5116
* @param {string} usersAdminUrl - url of eg "<SITE.com>/.netlify/identity/admin/users/123-abc-456"
5217
* @param {string} adminAuthHeader - authorisation JWT
5318
*/
5419
function updateNetlifyUserAppMetaData (appMetaData, usersAdminUrl, JWT){
5520

5621
return fetch(usersAdminUrl, {
57-
method: "PUT",
58-
headers: { Authorization: `Bearer ${JWT}` },
59-
body: JSON.stringify({app_metadata: appMetaData})
22+
method: "PUT",
23+
headers: { Authorization: `Bearer ${JWT}` },
24+
body: JSON.stringify({app_metadata: appMetaData})
6025
})
6126
.then(response => response.json())
6227
.then(data => data )
@@ -83,16 +48,12 @@ function handler(event, context, callback) {
8348
id : userID,
8449
user_metadata: user.user_metadata
8550
}
86-
const password = generator.generate({
87-
length: 10,
88-
numbers: true
89-
});
90-
51+
const password = identitySignup.generatePassword()
9152
console.log("admin url check", usersAdminUrl)
9253
console.log("bearer token check", JWT)
9354

94-
createDbUser(userObject, password)
95-
.then((user) => obtainToken(user, password))
55+
identitySignup.createDbUser(userObject, password)
56+
.then((user) => identitySignup.obtainToken(user, password))
9657
.then((key) => updateNetlifyUserAppMetaData({db_token : key.secret} , usersAdminUrl, JWT))
9758
.then((resp) => {
9859
console.log("Received response: ", !!resp)
@@ -106,9 +67,7 @@ function handler(event, context, callback) {
10667
console.error("Unable to create a user account", error)
10768
callback(null, {
10869
statusCode: 418,
109-
body: JSON.stringify({
110-
error: error
111-
})
70+
body: JSON.stringify({error: error})
11271
})
11372
})
11473
}

functions/identity-signup.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ const client = new faunadb.Client({
88
secret: process.env.FAUNADB_SERVER_SECRET
99
})
1010

11-
/* create a user in FaunaDB that can connect from the browser */
12-
function createUser(userData, password) {
13-
return client.query(q.Create(q.Class("users"), {
11+
/**
12+
* create a user in FaunaDB that can connect from the browser
13+
* @param {object} userData
14+
* @property {string} userData.id - netlify id nunmber
15+
* @property {object} userData.user_metadata - additonal arbitary
16+
* @param {string} password
17+
*/
18+
function createDbUser(userData, password) {
19+
return client.query(q.Create(q.Collection("users"), {
1420
credentials : {
1521
password : password
1622
},
@@ -22,40 +28,47 @@ function createUser(userData, password) {
2228
}
2329

2430
function obtainToken(user, password) {
25-
console.log("creating FaunaDB token")
31+
console.log("Generating new DB token")
2632
return client.query(
2733
q.Login(q.Select("ref", user), { password }))
2834
}
2935

30-
function handler(event, context, callback) {
31-
var payload = JSON.parse(event.body);
32-
var userData = payload.user;
33-
34-
const password = generator.generate({
36+
function generatePassword(){
37+
return generator.generate({
3538
length: 10,
3639
numbers: true
3740
});
41+
}
42+
43+
function handler(event, context, callback) {
3844

39-
createUser(userData, password)
45+
let payload = JSON.parse(event.body);
46+
let userData = payload.user;
47+
const password = generatePassword();
48+
49+
createDbUser(userData, password)
4050
.then((user) => obtainToken(user, password))
41-
.then((key) => callback(null, {
42-
// the if return status is 200 or 204 the function will get blocked
43-
statusCode: 200,
44-
body: JSON.stringify({
45-
app_metadata: {
46-
//the return body will update the netlify user
47-
db_token : key.secret
48-
// we discard the credential, and can create a new one if we ever need a new token
49-
// faunadb_credential : password
50-
} })
51-
})).catch((e) => {
52-
console.error(e)
51+
.then((key) => {
52+
console.log("Successfully created DB account")
53+
callback(null, {
54+
//If return status is 200 or 204 the function will get blocked
55+
statusCode: 200,
56+
//the return body will update the netlify user
57+
body: JSON.stringify({ app_metadata: { db_token : key.secret} })
58+
})
59+
})
60+
.catch((e) => {
61+
console.error("Somethings gone wrong ",e)
5362
callback(null, {
5463
statusCode: 500,
55-
body: JSON.stringify({
56-
error: e
57-
})
64+
body: JSON.stringify({error: e})
5865
})
5966
})
6067
}
61-
module.exports = {handler: handler};
68+
69+
module.exports = {
70+
handler: handler,
71+
createDbUser: createDbUser,
72+
obtainToken: obtainToken,
73+
generatePassword: generatePassword
74+
};

0 commit comments

Comments
 (0)