Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

netlify-identity-widget

netlify33.8kMIT1.9.2TypeScript support: definitely-typed

Netlify Identity widget for easy integration

authentication, identity, netlify

readme

Netlify Identity Widget

npm version

A component used to authenticate with Netlify's Identity service.

Live demo

For usage example with React and React Router, please see our /example folder and read the README.

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>

  <!-- include the widget -->
  <script type="text/javascript" src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
</head>
<body>
  <!-- Add a menu:
   Log in / Sign up - when the user is not logged in
   Username / Log out - when the user is logged in
  -->
  <div data-netlify-identity-menu></div>

  <!-- Add a simpler button:
    Simple button that will open the modal.
  -->
  <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:

// Open the modal
netlifyIdentity.open();

// Get the current user:
// Available after on('init') is invoked
const user = netlifyIdentity.currentUser();

// Bind to events
netlifyIdentity.on('init', user => console.log('init', user));
netlifyIdentity.on('login', user => console.log('login', user));
netlifyIdentity.on('logout', () => console.log('Logged out'));
netlifyIdentity.on('error', err => console.error('Error', err));
netlifyIdentity.on('open', () => console.log('Widget opened'));
netlifyIdentity.on('close', () => console.log('Widget closed'));

// Unbind from events
netlifyIdentity.off('login'); // to unbind all registered handlers
netlifyIdentity.off('login', handler); // to unbind a single handler

// Close the modal
netlifyIdentity.close();

// Log out the user
netlifyIdentity.logout();

// Refresh the user's JWT
// Call in on('login') handler to ensure token refreshed after it expires (1hr)  
// Note: this method returns a promise.
netlifyIdentity.refresh().then((jwt)=>console.log(jwt))

// Change language
netlifyIdentity.setLocale('en');

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', // defaults to document.body
  locale: 'en' // defaults to 'en'
});

netlifyIdentity.open(); // open the modal
netlifyIdentity.open('login'); // open the modal to the login tab
netlifyIdentity.open('signup'); // open the modal to the signup tab

netlifyIdentity.on('init', user => console.log('init', user));
netlifyIdentity.on('login', user => console.log('login', user));
netlifyIdentity.on('logout', () => console.log('Logged out'));
netlifyIdentity.on('error', err => console.error('Error', err));
netlifyIdentity.on('open', () => console.log('Widget opened'));
netlifyIdentity.on('close', () => console.log('Widget closed'));

// Unbind from events
netlifyIdentity.off('login'); // to unbind all registered handlers
netlifyIdentity.off('login', handler); // to unbind a single handler

// Close the modal
netlifyIdentity.close();

// Log out the user
netlifyIdentity.logout();

// refresh the user's JWT
// Note: this method returns a promise.
netlifyIdentity.refresh().then((jwt)=>console.log(jwt))

// Change language
netlifyIdentity.setLocale('en');

// Access the underlying GoTrue JS client.
// Note that doing things directly through the GoTrue client brings a risk of getting out of
// sync between your state and the widget’s state.
netlifyIdentity.gotrue;

netlifyIdentity.init([opts])

You can pass an optional opts object to configure the widget when using the module API. Options include:

{
  container: '#some-query-selector'; // container to attach to
  APIUrl: 'https://www.example.com/.netlify/functions/identity'; // Absolute url to endpoint.  ONLY USE IN SPECIAL CASES!
  namePlaceholder: 'some-placeholder-for-Name'; // custom placeholder for name input form
  locale: 'en'; // language code for translations - available: en, fr, es, pt, hu, pl, cs, sk - default to en

Generally avoid setting the APIUrl. You should only set this when your app is served from a domain that differs from where the identity endpoint is served. This is common for Cordova or Electron apps where you host from localhost or a file.

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 zero configuration.

E.g. If your Netlify site is served from the olddvdscreensaver.com domain name, enter the following when prompted by the widget when in development mode:

https://olddvdscreensaver.com

List of Alternatives

Lowest level JS Library: If you want to use the official Javascript bindings to GoTrue, Netlify's underlying Identity service written in Go, use https://github.com/netlify/gotrue-js

React bindings: If you want a thin wrapper over Gotrue-js for React, react-netlify-identity is a "headless" library, meaning there is no UI exported and you will write your own UI to work with the authentication. https://github.com/sw-yx/react-netlify-identity

High level overlay: If you want a "widget" overlay that gives you a nice UI out of the box, with a somewhat larger bundle, check https://github.com/netlify/netlify-identity-widget

High level popup: If you want a popup window approach also with a nice UI out of the box, and don't mind the popup flow, check https://github.com/netlify/netlify-auth-providers

You can also see an example of wrapping netlify-identity-widget in a React Hook here: https://github.com/sw-yx/netlify-fauna-todo/blob/master/src/hooks/useNetlifyIdentity.js

FAQ

  • TypeScript Typings are maintained by @nkprince007 (see PR): npm install @types/netlify-identity-widget and then import * as NetlifyIdentityWidget from "netlify-identity-widget" (or import NetlifyIdentityWidget from "netlify-identity-widget" if you have --allowSyntheticDefaultImports on)

  • 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');
  • See the example for how to integrate this widget with a react app.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Generated by auto-changelog.

v1.9.2

Merged

  • fix: remove identity prefix from site URL if exists #482
  • feat: Czech and Slovak translations #439
  • chore(deps): update dependency gh-release to v5 #422
  • feat: add polish translations #409
  • chore(deps): update dependency webpack-cli to v4 #387
  • chore(renovate): add pr labels #374
  • chore(deps): update dependency eslint-config-prettier to v8 #431
  • chore(deps): bump elliptic from 6.5.3 to 6.5.4 #436
  • chore(deps): bump elliptic from 6.5.3 to 6.5.4 in /example/react #437
  • chore(deps): bump ssri from 6.0.1 to 6.0.2 in /example/react #444
  • chore(deps): bump ssri from 6.0.1 to 6.0.2 #446
  • chore(deps): bump handlebars from 4.7.6 to 4.7.7 #448
  • chore(deps): bump url-parse from 1.4.7 to 1.5.1 #449
  • chore(deps): bump url-parse from 1.4.7 to 1.5.1 in /example/react #450
  • Fix: Replacement typo on white-space property #435
  • Add branded README image #434
  • chore(deps): update actions/setup-node action to v2 #411
  • chore(deps): lock file maintenance #392
  • chore(deps): update babel monorepo #390
  • chore(deps): update babel monorepo #338
  • chore(deps): bump node-fetch from 2.6.0 to 2.6.1 #364
  • chore: module example code typo #359
  • feat: Russian translation #354
  • chore: clarify use of global script version #357
  • feat: Portuguese translation #352
  • fix: add missing translations #353

Fixed

  • chore: clarify use of global script version (#357) #355

Commits

  • Update yarn lockfile ad7d5c6
  • chore(deps): update jest monorepo to v26.6.3 6fadf3b
  • chore(deps): update dependency eslint to v7.26.0 e47ee6f

v1.9.1 - 2020-07-30

Merged

  • fix: increase input size to avoid mobile zoom #334
  • Feat: Hungarian translation #330

Fixed

  • fix: increase input size to avoid mobile zoom #236

Commits

  • hungarian translation ebcf2c9
  • removed '...'s as they're added outside of the translation files 0e4f728
  • fixed indentation and capitalization c54bfaf

v1.9.0 - 2020-07-29

Merged

  • feat: Add the ability to clear an invalid site URL #305

v1.8.1 - 2020-07-29

Merged

  • fix: ReferenceError: window is not defined #319
  • Feat: Update Spanish translation #333
  • Update correct dependencies #332
  • Feat: Add Spanish translation #327
  • Fix: Make "French" into "Français" #328
  • fix(deps): update dependency react-scripts to v3 #312
  • ci: switch to github actions #325
  • chore(deps): remove unused raw-loader dependency #304
  • chore: remove package-lock.json #303
  • fix: prevent double initialization #284
  • chore(deps): lock file maintenance #323
  • chore(deps): update dependency rimraf to v3 #301

Commits

  • feat: adding Spanish translation 54ed057
  • Feat: Update Spanish translation to an informal, friendly tone c27a300
  • chore(deps): update dependency caniuse-lite to v1.0.30001104 ececa82

v1.8.0 - 2020-07-06

Merged

  • feat: add support for unregistering event handlers #283
  • fix: changes the form type to name #286
  • chore(deps): lock file maintenance #302

v1.7.0 - 2020-06-30

Merged

  • feat: Indicate via @autocomplete whether a password field is for new or current values #275
  • chore: update gotrue-js #282
  • chore(deps): lock file maintenance #281
  • chore(deps): update dependency mkdirp to v1 #272
  • chore(deps): lock file maintenance #273
  • chore(deps): lock file maintenance #267
  • feat: add multilingual support #238
  • chore(deps): update babel #261
  • chore(deps): update dependency webpack to v4 #247
  • chore(deps): update dependency css-loader to v3 #253
  • chore(deps): update dependency file-loader to v6 #257
  • chore(deps): lock file maintenance #260
  • chore(deps): update dependency cross-env to v7 #252
  • chore(deps): update dependency auto-changelog to v2 #251
  • chore(deps): lock file maintenance #259
  • chore(deps): lock file maintenance #246
  • chore(deps): update dependency webpack-dev-server to v3 [security] #242
  • chore: add renovate.json #241
  • github tools: add fossa license scanning #239

Commits

  • create github actions workflow files for fossa bf1b9f1
  • fix(docs): change yarn start to yarn dev in CONTRIBUTING.md e18b76e

v1.6.0 - 2020-05-08

Merged

  • added refresh method from gotrue #237
  • Bump handlebars from 4.2.0 to 4.5.3 in /example/react #228
  • Bump handlebars from 4.2.1 to 4.5.3 #227

Commits

v1.5.6 - 2019-12-02

Merged

  • fix: don't open modal when using implicit auth from cms #223
  • Add an example in Svelte #218
  • Bump handlebars from 4.0.12 to 4.2.1 #216
  • Bump lodash from 4.17.11 to 4.17.15 #211
  • Bump clean-css from 4.1.9 to 4.1.11 in /example/react #208
  • Bump handlebars from 4.0.12 to 4.2.0 #205
  • Bump eslint-utils from 1.3.1 to 1.4.2 in /example/react #207
  • Bump lodash.template from 4.4.0 to 4.5.0 in /example/react #213
  • Bump sshpk from 1.13.1 to 1.16.1 in /example/react #212
  • Bump diff from 3.3.1 to 3.5.0 in /example/react #204
  • Bump mixin-deep from 1.3.1 to 1.3.2 #202
  • Bump lodash.template from 4.4.0 to 4.5.0 #209
  • Bump js-yaml from 3.10.0 to 3.13.1 in /example/react #214
  • Bump mixin-deep from 1.3.1 to 1.3.2 in /example/react #210
  • Bump merge from 1.2.0 to 1.2.1 in /example/react #215
  • Bump handlebars from 4.0.10 to 4.2.0 in /example/react #206
  • Bump hoek from 4.2.0 to 4.2.1 in /example/react #203
  • Set the iframe title to comply with accessible best practices. #200

Commits

  • Add Svelte README baf7b77
  • remove identity site from local host list 623be68

v1.5.5 - 2019-06-07

Merged

  • fix: ensure dev settings for url is only visible on dev #191

Commits

  • prettifying 528cdab
  • fix: extrapolate dev mode check into function that saves to store c992ce4
  • fix: ensure isLocal boolean is set on init 5b46e0f

v1.5.4 - 2019-05-21

Commits

  • store access token from external provider as jwt cookie dc45eb1

v1.5.3 - 2019-05-21

Merged

  • Added custom placeholder option #178
  • chore: Add feature to switch out identity url #174
  • add an example in Vue.js #167

Commits

v1.5.2 - 2018-12-06

Merged

  • Remove devDep from deps and fix release flow #170

v1.5.1 - 2018-11-29

Commits

v1.5.0 - 2018-11-29

Merged

  • Semver updates and release tweaks #164
  • Allow display of SAML provider button #150

Commits

  • Update semver deps 738dedb
  • Add a publish script 15ccd0a
  • Allow override of provider names from settings ca4dea6

v1.4.15 - 2019-05-21

Merged

  • Remove redundant with #155
  • fix small copy/paste error (again) #154
  • Fix small copy/paste error #153

Commits

v1.4.14 - 2018-06-07

Commits

v1.4.13 - 2018-06-01

Merged

  • update icon on name field #141

Commits

v1.4.12 - 2018-05-07

Merged

  • Support async loading #126
  • fixed "login" parameter in module API #136
  • Make Close Button bigger and centered #134
  • Clarify local Site URL popup. #124

Commits

v1.4.11 - 2018-02-09

Merged

  • Improve hash cleaning regex #120

Commits

v1.4.10 - 2018-02-08

Commits

v1.4.9 - 2018-01-29

Commits

v1.4.8 - 2018-01-29

Commits

  • Don’t drop console logs ffd15c9

v1.4.7 - 2018-01-29

Commits

v1.4.6 - 2018-01-29

Commits

v1.4.5 - 2018-01-29

Commits

v1.4.4 - 2018-01-25

Merged

  • Fix source maps on UMD #114

v1.4.3 - 2018-01-15

Merged

  • Fix adblock pro blocking #113

Commits

  • Restore production on UMD builds a9f5bb8

v1.4.2 - 2018-01-08

Merged

v1.4.2-beta-2 - 2018-01-08

Commits

v1.4.2-beta - 2018-01-05

Commits

  • Babelify mobx and preact for prerender support d1038f1

v1.4.1 - 2017-12-22

Commits

  • Fix logo option regression b592479

v1.4.0 - 2017-12-21

Merged

  • Add APIUrl option to module API #103

Commits

  • Clarify why are adding this option. e7bdf60
  • Handle error state when settings endpoint fails c58aa6c
  • Fix Typos 36d8789

v1.3.5 - 2017-12-21

Merged

  • Update gotrue and bump the widget version #98

Commits

v1.3.4 - 2017-11-17

Merged

  • Add signup button action to public api #97

Commits

  • Reformat README and add 5afbdf8
  • Remove defer suggestion from readme 0c14ab3

v1.3.3 - 2017-11-10

Merged

  • Add z-index to the iframe and fix #94 #95

Fixed

  • Merge pull request #95 from carlosfaria94/master #94
  • Add z-index to the iframe and fix #94 #94

v1.3.2 - 2017-11-09

Merged

  • Dont start action unless there is a user #93

v1.3.1 - 2017-11-09

Merged

  • Clear saving state when logout is called without a user #92

Commits

v1.3.0 - 2017-11-09

Merged

  • Enable sourcemaps #91

Commits

v1.2.0 - 2017-10-10

Merged

  • Add option to disable logo #79
  • Add devmode instructions #75

Commits

v1.1.2 - 2017-10-09

Commits

v1.1.1 - 2017-10-06

Commits

v1.1.0 - 2017-10-06

Merged

  • Set a cookie when not using cors #74
  • Update README.md #72

Commits

v1.0.2 - 2017-09-27

Merged

  • add FAQ to readme #70
  • removes the relative or #69
  • Partial Revert of "Add init event to netlifyIdentity" #66

Commits

  • Revert "Add init event to netlifyIdentity" 8094df8
  • Minor eslint changes c08e872
  • Fix weird render of OR on HR c32e17d

v1.0.1 - 2017-09-09

Merged

  • Fix module API export #65

Commits

v1.0.0 - 2017-09-08

Merged

  • Draft release doc #63
  • Also make sure we can still build. #64
  • Add init event to netlifyIdentity #62
  • Replace GitHub ribbon with simpler link #61
  • Don’t kick off loading state when linking to providers #59
  • Add links/badges to demo site #60
  • Release with more fixes #58
  • Enable providers on accept invite page #56
  • Turn off hmr in devmode #52
  • Upgrade gotrue-js #55
  • Created new release for version v1 #54
  • Enable travis #51
  • console.error instead of log #50
  • Fix lint/format #45
  • Fix logged in state styling #44
  • Demo page refactor #43
  • Fix readme #41
  • [WIP] Style Dev Mode #31
  • Fix race condition on currentUser call #38
  • Fix remaining listing errors. Prettier should work now. #35
  • Add aria-hidden attribute to dialog #36
  • Fix viewport warnings on safari #33
  • Add some css to the template so rafa can go play later #32
  • Fix a race condition #34
  • Remove unused packages and fix lint errors #30

Fixed

  • Don’t kick off loading state when linking to providers #47

Commits

  • Fix format of webpack config 4094ce8
  • Add styling to markup a9c5fc7
  • Use recommended rules, disable prettier-eslint a3616aa