This repository has been archived by the owner on Sep 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sw-yx
committed
Jun 5, 2019
1 parent
c3aa046
commit 65b0d92
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/functions-templates/js/create-user/.netlify-function-template.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |