Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 65b0d92

Browse files
author
sw-yx
committed
add create user function
1 parent c3aa046 commit 65b0d92

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
name: "create-user",
3+
description:
4+
"Programmatically create a Netlify Identity user by invoking a function",
5+
onComplete() {
6+
console.log(`create-user function created from template!`);
7+
console.log(
8+
"REMINDER: Make sure to call this function with a Netlify Identity JWT. See https://netlify-gotrue-in-react.netlify.com/ for demo"
9+
);
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fetch = require("node-fetch");
2+
3+
exports.handler = async (event, context) => {
4+
if (event.httpMethod !== "POST")
5+
return { statusCode: 400, body: "Must POST to this function" };
6+
7+
// send account information along with the POST
8+
const { email, password, full_name } = JSON.parse(event.body);
9+
if (!email) return { statusCode: 400, body: "email missing" };
10+
if (!password) return { statusCode: 400, body: "password missing" };
11+
if (!full_name) return { statusCode: 400, body: "full_name missing" };
12+
13+
// identity.token is a short lived admin token which
14+
// is provided to all Netlify Functions to interact
15+
// with the Identity API
16+
const { identity } = context.clientContext;
17+
18+
await fetch(`${identity.url}/admin/users`, {
19+
method: "POST",
20+
headers: { Authorization: `Bearer ${identity.token}` },
21+
body: JSON.stringify({
22+
email,
23+
password,
24+
confirm: true,
25+
user_metadata: {
26+
full_name
27+
}
28+
})
29+
});
30+
31+
return {
32+
statusCode: 200,
33+
body: "success!"
34+
};
35+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "create-user",
3+
"version": "1.0.0",
4+
"description": "netlify functions:create - Programmatically create a Netlify Identity user by invoking a function",
5+
"main": "create-user.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"netlify",
11+
"serverless",
12+
"js",
13+
"identity",
14+
"authentication"
15+
],
16+
"author": "Netlify",
17+
"license": "MIT",
18+
"dependencies": {
19+
"node-fetch": "^2.3.0"
20+
}
21+
}

0 commit comments

Comments
 (0)