🤖 Merge PR #46854 Add types for netlify-identity-widget 1.9 by @marierigal

This commit is contained in:
Marie Rigal 2020-09-19 18:09:10 +02:00 committed by GitHub
parent 3910835543
commit a37b7dd958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 2 deletions

View File

@ -1,4 +1,4 @@
// Type definitions for netlify-identity-widget 1.4
// Type definitions for netlify-identity-widget 1.9
// Project: https://github.com/netlify/netlify-identity-widget, https://identity.netlify.com
// Definitions by: Naveen Kumar Sangi <https://github.com/nkprince007>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@ -14,6 +14,12 @@ export interface InitOptions {
// identity endpoint is served.This is common for Cordova or Electron
// apps where you host from localhost or a file.
APIUrl?: string;
// Enable Netlify logo
logo?: boolean;
// Initial language
locale?: string;
}
export interface Token {
@ -41,6 +47,8 @@ export interface User {
confirmed_at: string;
created_at: string;
updated_at: string;
invited_at: string;
recovery_sent_at: string;
email: string;
id: string;
role: string;
@ -80,8 +88,22 @@ export function on(event: 'login', cb: (user: User) => void): void;
export function on(event: 'logout' | 'open' | 'close', cb: () => void): void;
export function on(event: 'error', cb: (err: Error) => void): void;
/**
* Unregisters callbacks to corresponding events on the widget.
* Set the callback argument to remove only the specified one.
*/
export function off(event: 'init', cb?: (user: User | null) => void): void;
export function off(event: 'login', cb?: (user: User) => void): void;
export function off(event: 'logout' | 'open' | 'close', cb?: () => void): void;
export function off(event: 'error', cb?: (err: Error) => void): void;
/**
* Logs out the current user. Returns a Promise<void> when a user is
* logged in, else returns undefined.
*/
export function logout(): Promise<void> | undefined;
/**
* Set current language
*/
export function setLocale(locale: string): void;

View File

@ -15,10 +15,18 @@ NetlifyIdentityWidget.init({ container: 'body' });
// Type 3: Initialize with a specific APIUrl
NetlifyIdentityWidget.init({ APIUrl: 'https://www.example.com/.netlify/functions/identity' });
// Type 4: Initialize with both the options specified
// Type 4: Initialize with a logo option
NetlifyIdentityWidget.init({ logo: true });
// Type 5: Initialize with a specific locale
NetlifyIdentityWidget.init({ locale: 'en' });
// Type 6: Initialize with all specified options
NetlifyIdentityWidget.init({
APIUrl: 'https://www.example.com/.netlify/functions/identity',
container: 'body',
logo: true,
locale: 'en',
});
// Open widget modal to let users login
@ -26,6 +34,8 @@ NetlifyIdentityWidget.open();
NetlifyIdentityWidget.on('open', () => {
// Widget is open and ready to login
});
NetlifyIdentityWidget.off('open');
NetlifyIdentityWidget.off('open', () => {});
// Open wigdet modal with signup tab selected
NetlifyIdentityWidget.open('signup');
@ -35,27 +45,37 @@ NetlifyIdentityWidget.close();
NetlifyIdentityWidget.on('close', () => {
// Widget is closed
});
NetlifyIdentityWidget.off('close');
NetlifyIdentityWidget.off('close', () => {});
// Event handling after login
NetlifyIdentityWidget.on('login', (user) => {
// You can now use User info after a successful login
});
NetlifyIdentityWidget.off('login');
NetlifyIdentityWidget.off('login', (user) => {});
// Event handling after logout
NetlifyIdentityWidget.on('logout', () => {
// You can now notify that the logout was successful
});
NetlifyIdentityWidget.off('logout');
NetlifyIdentityWidget.off('logout', () => {});
// Event handling after login on page refresh
NetlifyIdentityWidget.on('init', (user) => {
// Now the widget is ready to use
// If a user was already logged in, the value is returned else null is passed via callback
});
NetlifyIdentityWidget.off('init');
NetlifyIdentityWidget.off('init', (user) => {});
// Event handling on errors
NetlifyIdentityWidget.on('error', (err) => {
// The error occured during operation is passed in via callback
});
NetlifyIdentityWidget.off('error');
NetlifyIdentityWidget.off('error', (err) => {});
// Use the current logged in user
const user = NetlifyIdentityWidget.currentUser();