Netlify Identity Widget
A component used to authenticate with Netlify's Identity service. Live demo
For a lower level library to the underlying GoTrue
API, see gotrue-js.
What is Netlify Identity
Netlify’s Identity service is a plug-and-play microservice for handling site functionalities like signups, logins, password recovery, user metadata, and roles. You can use it from single page apps instead of rolling your own, and integrate with any service that understands JSON Web Tokens (JWTs).
Learn more about this service from this blog post.
Usage
Simply include the widget on your site, and things like invites, confirmation codes, etc, will start working.
You can add controls for the widget with HTML:
<!DOCTYPE html>
<html>
<head>
<title>A static website</title>
<script defer type="text/javascript" src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
</head>
<body>
<div data-netlify-identity-menu></div>
<div data-netlify-identity-button>Login with Netlify Identity</div>
</body>
</html>
The widget will automatically attach itself to the window object as window.netlifyIdentity
.
You can use this global object like this:
netlifyIdentity.open()
const user = netlifyIdentity.currentUser();
netlifyIdentity.on('init', user => console.log(user))
netlifyIdentity.on('login', user => console.log(user))
netlifyIdentity.on('logout', () => console.log("Logged out"))
netlifyIdentity.on('error', err => console.error("Logged out"))
netlifyIdentity.on('open', () => console.log("Widget opened"))
netlifyIdentity.on('close', () => console.log("Widget closed"))
loginModal.close()
loginModal.logout();
A note on script tag versioning
The v1
in the above URL is not pinned to the major version of the module API, and will only reflect breaking changes in the
markup API.
Module API
Netlify Identity Widget also has a module api:
yarn add netlify-identity-widget
Import or require as usual:
const netlifyIdentity = require("netlify-identity-widget")
netlifyIdentity.init({
container: "#netlify-modal"
});
netlifyIdentity.open()
netlifyIdentity.on('init', user => console.log(user))
netlifyIdentity.on('login', login => console.log(user))
netlifyIdentity.on('logout', () => console.log("Logged out"))
netlifyIdentity.on('error', err => console.error("Logged out"))
netlifyIdentity.on('open', () => console.log("Widget opened"))
netlifyIdentity.on('close', () => console.log("Widget closed"))
identity.close()
netlifyIdentity.logout();
netlifyIdentity.gotrue
Localhost
When using the widget on localhost, it will prompt for your Netlify SiteURL the first time it is opened. Entering the siteURL populates the browser's localStorage.
This allows the widget to know which instance of Netlify Identity it should communicate with with zero
configuration.
FAQ
- If you experience a 404 while testing the Netlify Identity Widget on a local environment, you can
manually remove the netlifySiteURL from localStorage by doing the following in the console.
localStorage.removeItem("netlifySiteURL")