Last active
February 15, 2018 14:43
-
-
Save travist/09c11ac0593f88cd3e9189d7c357c81c to your computer and use it in GitHub Desktop.
Form.io Server-to-Server User Creation and Authentication
This file contains hidden or 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
var request = require("request"); | |
// The api key is generated within the Form.io project settings. | |
var apiKey = '23kj2k3jhkj2h3'; | |
// Create a new user. | |
request({ | |
method: 'POST', | |
url: 'https://example.form.io/user/submission', | |
headers: { | |
'content-type': 'application/json', | |
'x-token': apiKey | |
}, | |
body: { | |
data: { | |
email: '[email protected]', | |
password: apiKey | |
} | |
}, | |
json: true | |
}, function (error, response, body) { | |
if (error) throw new Error(error); | |
// The user is created. | |
console.log(body); | |
}); |
This file contains hidden or 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
var request = require("request"); | |
// The api key is generated within the Form.io project settings. | |
var apiKey = '23kj2k3jhkj2h3'; | |
// Log the user in using the api key. | |
request({ | |
method: 'POST', | |
url: 'https://example.form.io/user/login', | |
headers: { | |
'content-type': 'application/json' | |
}, | |
body: { | |
data: { | |
email: '[email protected]', | |
password: apiKey | |
} | |
}, | |
json: true | |
}, function (error, response, body) { | |
if (error) throw new Error(error); | |
// This is the users auth token, and can now be handed to a user on the front end. | |
var userToken = response.headers['x-jwt-token']; | |
// You can also use that token to save a submission to another form as that user like so. | |
request({ | |
method: 'POST', | |
url: 'https://example.form.io/someform', | |
headers: { | |
'content-type': 'application/json', | |
'x-jwt-token': userToken | |
}, | |
body: { | |
data: { | |
firstName: 'Travis', | |
lastName: 'Tidwell', | |
email: '[email protected]' | |
} | |
}, | |
json: true | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment