-
-
Save StephenOTT/b36be452bcb22571cacf8e6c6535d880 to your computer and use it in GitHub Desktop.
Form.io Server-to-Server User Creation and Authentication
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
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', | |
headers: { | |
'content-type': 'application/json', | |
'x-token': apiKey | |
}, | |
body: { | |
data: { | |
email: 'travis@form.io', | |
password: apiKey | |
} | |
}, | |
json: true | |
}, function (error, response, body) { | |
if (error) throw new Error(error); | |
// The user is created. | |
console.log(body); | |
}); |
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
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: 'travis@form.io', | |
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: 'travis@form.io' | |
} | |
}, | |
json: true | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment