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

Commit 0bd9e2d

Browse files
committed
version 0.0.1
1 parent e468746 commit 0bd9e2d

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
lines changed

functions/identity-external-signup.js

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,61 +82,97 @@ function updateNetlifyUser (key, usersUrl, adminAuthHeader){
8282
}
8383
}
8484

85-
function handler(event, context, callback) {
85+
function checkNetlifyUserHasDbToken (usersUrl, adminAuthHeader){
86+
console.log("Checking Netlify user account....")
87+
return new Promise((resolve, reject)=>{
88+
fetch(usersUrl, {
89+
method: "GET",
90+
headers: { Authorization: adminAuthHeader },
91+
})
92+
.then(response => response.json())
93+
.then(data => {
94+
let dbToken = data.app_metadata.faunadb_token
95+
console.log("Does user have DB token?", dbToken);
96+
if(!!dbToken === true){
97+
resolve(data)
98+
}
99+
else if(!!dbToken === false){
100+
resolve(false)
101+
}
102+
})
103+
.catch(e => {
104+
console.error("error authorising user",e)
105+
reject(e)
106+
});
86107

87-
// the context of the netlify function needs to be set to idenity
88-
// is set when calling this function with
108+
})
109+
}
110+
111+
function handler(event, context, callback) {
89112

90113
const { identity, user } = context.clientContext;
91114

92-
//block if user hits endpoint direclty
115+
//Guard if user hits this function URL direclty
93116
if (!user) {
94117
return callback(null, {
95118
statusCode: 401,
96119
body: "<img src='https://media.tenor.co/images/fb288a6182d05e93d8e731cec487a0ad/tenor.gif' alt='You should'nt be here...'>"
97120
});
98121
}
99122

123+
// Try-block required as we cant guarantee the event.body can parse correctly
124+
// if it fails it results in a runtime error.
100125
try {
101126
let payload = JSON.parse(event.body);
102127
let userData = payload.user;
103128
const usersUrl = `${identity.url}/admin/users/${userData.id}`;
104129
const adminAuthHeader = `Bearer ${identity.token}`;
105130

106131
console.log("admin url check", usersUrl)
132+
console.log("bearer token check", adminAuthHeader)
107133

108-
109-
//TODO - check if user already exists in db
110-
// if so send the current netlify user object
111-
112-
const password = generator.generate({
113-
length: 10,
114-
numbers: true
115-
});
116-
117-
console.log("Creating user in DB via external signup")
118-
119-
createUser(userData, password)
120-
.then((user) => obtainToken(user, password))
121-
.then((key) => updateNetlifyUser(key, usersUrl, adminAuthHeader))
122-
.then((resp) => {
123-
console.log("Received response: ", resp)
134+
checkNetlifyUserHasDbToken(usersUrl, adminAuthHeader)
135+
.then((resp) => {
136+
if(!!resp === true){
137+
//send the callback and end the process
138+
console.log("User has DB token present, ending process")
124139
callback(null, {
125140
statusCode: 200,
126141
body: JSON.stringify(resp)
127142
})
128-
})
129-
.catch((error) => {
130-
console.error("Unable to create a user account", error)
131-
callback(null, {
132-
statusCode: 500,
133-
body: JSON.stringify({
134-
error: error
143+
return
144+
} else {
145+
// As no DB token is present, we can gurantee this is brand new signup
146+
// therefor go ahead and create the new user in the DB
147+
console.log("New user, creating user in DB via external signup")
148+
149+
const password = generator.generate({
150+
length: 10,
151+
numbers: true
152+
});
153+
154+
createUser(userData, password)
155+
.then((user) => obtainToken(user, password))
156+
.then((key) => updateNetlifyUser(key, usersUrl, adminAuthHeader))
157+
.then((resp) => {
158+
console.log("Received response: ", !!resp)
159+
callback(null, {
160+
statusCode: 200,
161+
body: JSON.stringify(resp.data)
162+
})
135163
})
136-
})
137-
})
138-
}
139-
catch(error) {
164+
.catch((error) => {
165+
console.error("Unable to create a user account", error)
166+
callback(null, {
167+
statusCode: 500,
168+
body: JSON.stringify({
169+
error: error
170+
})
171+
})
172+
})
173+
}
174+
})
175+
} catch(error) {
140176
let errorMessage = "Cant process the given payload"
141177
callback(null, {
142178
statusCode: 418,

src/store/modules/auth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ export default {
130130
.then((resp) => resp.json())
131131
.then(resp => {
132132
console.log("response back", resp)
133-
console.log("seeting current user to state, ", resp.data)
134-
commit("SET_CURRENT_USER", resp.data)
133+
console.log("seeting current user to state, ", resp)
134+
commit("SET_CURRENT_USER", resp)
135135
})
136136
.catch(error => {console.error("problem with external signup function" , error)})
137137

0 commit comments

Comments
 (0)