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

Commit

Permalink
add create user function
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-yx committed Jun 5, 2019
1 parent c3aa046 commit 65b0d92
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
name: "create-user",
description:
"Programmatically create a Netlify Identity user by invoking a function",
onComplete() {
console.log(`create-user function created from template!`);
console.log(
"REMINDER: Make sure to call this function with a Netlify Identity JWT. See https://netlify-gotrue-in-react.netlify.com/ for demo"
);
}
};
35 changes: 35 additions & 0 deletions src/functions-templates/js/create-user/create-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fetch = require("node-fetch");

exports.handler = async (event, context) => {
if (event.httpMethod !== "POST")
return { statusCode: 400, body: "Must POST to this function" };

// send account information along with the POST
const { email, password, full_name } = JSON.parse(event.body);
if (!email) return { statusCode: 400, body: "email missing" };
if (!password) return { statusCode: 400, body: "password missing" };
if (!full_name) return { statusCode: 400, body: "full_name missing" };

// identity.token is a short lived admin token which
// is provided to all Netlify Functions to interact
// with the Identity API
const { identity } = context.clientContext;

await fetch(`${identity.url}/admin/users`, {
method: "POST",
headers: { Authorization: `Bearer ${identity.token}` },
body: JSON.stringify({
email,
password,
confirm: true,
user_metadata: {
full_name
}
})
});

return {
statusCode: 200,
body: "success!"
};
};
21 changes: 21 additions & 0 deletions src/functions-templates/js/create-user/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "create-user",
"version": "1.0.0",
"description": "netlify functions:create - Programmatically create a Netlify Identity user by invoking a function",
"main": "create-user.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"netlify",
"serverless",
"js",
"identity",
"authentication"
],
"author": "Netlify",
"license": "MIT",
"dependencies": {
"node-fetch": "^2.3.0"
}
}

0 comments on commit 65b0d92

Please sign in to comment.