Enabling Login to EnergyPrint from Your Web Site
This guide provides clear instructions to integrate your web site’s login form with EnergyPrint’s authentication system. By following these steps, users can log in to their EnergyPrint Dashboard directly from your site using their email and password.
Configuration Steps
- Set the Form Action
- Update your login form to send a POST request to:
https://api.energyprint.com/auth/login
- Update your login form to send a POST request to:
- Add Required Fields
- Include these fields in your form:
email
: The user’s email address (e.g., a text input).password
: The user’s password (e.g., a password input).doRedirect
: Set this to “true” to redirect users to their EnergyPrint Dashboard after a successful login. Use a hidden input field for this.
- Include these fields in your form:
- Use the POST Method
- Ensure your form’s
method
attribute is set to"post"
.
- Ensure your form’s
Example Login Form
Here’s a sample HTML form to implement this integration:
<form action="https://api.energyprint.com/auth/login" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="hidden" name="doRedirect" value="true">
<button type="submit">Log In to EnergyPrint</button>
</form>
How It Works
- When the User Submits the Form:
- The browser sends a POST request to
https://api.energyprint.com/auth/login
with theemail
,password
, anddoRedirect
values.
- The browser sends a POST request to
- Successful Login:
- If the
email
andpassword
are correct anddoRedirect
is “true”, the EnergyPrint server redirects the user to their EnergyPrint Dashboard.
- If the
- Incorrect Credentials:
- If the
email
orpassword
is wrong, the EnergyPrint server will respond with an error status (ie 400,401) and json data including amessage
describing the error.
- If the
- Alternative Behavior:
- If
doRedirect
is set to “false” or not included, the server returns an authentication token instead of redirecting. This token is useful for API integrations, but for this user-facing login feature, always setdoRedirect
to “true”.
- If
JS Request
You can also do this using javascript in the user’s browser and JSON by handling the redirect yourself, for example:
var axios = require("axios").default;
var options = {
method: 'POST',
url: 'https://api.energyprint.com/auth/login',
headers: {
'Content-Type': 'application/json'
},
data: {
email: "your_energyprint_login_email",
password: "your_energyprint_password"
}
};
axios.request(options).then(response => {
const {token} = response;
window.location.href = "https://live.energyprint.com/api/get_rails_session?jwt=" + token
})
.catch(console.error)
Additional Notes
- Security: The form uses HTTPS to securely transmit credentials to EnergyPrint.
- User Flow: After a successful login, users are redirected to the EnergyPrint Dashboard, leaving your web site. Plan your site’s navigation accordingly.