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
methodattribute 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">
<!-- OPTIONAL --> <input type="hidden" name="redirectUrl" value="/">
<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/loginwith theemail,password, anddoRedirectvalues.
- The browser sends a POST request to
- Successful Login:
- If the
emailandpasswordare correct anddoRedirectis “true”, the EnergyPrint server redirects the user to their EnergyPrint Dashboard.
- If the
- Incorrect Credentials:
- If the
emailorpasswordis wrong, the EnergyPrint server will respond with an error status (ie 400,401) and json data including amessagedescribing the error.
- If the
- Alternative Behavior:
- If
doRedirectis 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 setdoRedirectto “true”. - Optional: In the same request, you can optionally specify a
redirectUrlto the form (as shown above) to redirect the user to a desired page after their session is created. This can be a relative or absolute url. By default, sessions will redirect to the main Dashboard view.
- 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.
- You can append an optional
redirectUrlto thehrefsetter above as a query parameter (ie&redirectUrl=https://live.energyprint.com/reports)