UNPKG

@auth0/auth0-spa-js

Version:

Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE

186 lines (138 loc) 5.2 kB
# @auth0/auth0-spa-js Auth0 SDK for Single Page Applications using [Authorization Code Grant Flow with PKCE](https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce). [![CircleCI](https://circleci.com/gh/auth0/auth0-spa-js.svg?style=svg)](https://circleci.com/gh/auth0/auth0-spa-js) [![License](https://img.shields.io/:license-mit-blue.svg?style=flat)](https://opensource.org/licenses/MIT) ## Table of Contents Make sure this is updated based on the sections included: - [Documentation](#documentation) - [Installation](#installation) - [Getting Started](#getting-started) - [Contributing](#contributing) - [Support + Feedback](#support--feedback) - [Frequently Asked Questions](#frequently-asked-questions) - [Vulnerability Reporting](#vulnerability-reporting) - [What is Auth0](#what-is-auth0) - [License](#license) ## Documentation - See the [API reference](https://auth0.github.io/auth0-spa-js/) ## Installation From the CDN: ```html <script src="https://cdn.auth0.com/js/auth0-spa-js/1.1.1/auth0-spa-js.production.js"></script> ``` Using [npm](https://npmjs.org): ```sh npm install @auth0/auth0-spa-js ``` Using [yarn](https://yarnpkg.com): ```sh yarn add @auth0/auth0-spa-js ``` ## Getting Started ### Creating the client > Ideally, you should have only one instance of the client. Create one > before rendering / initializing your application. ```js //with async/await const auth0 = await createAuth0Client({ domain: '<AUTH0_DOMAIN>', client_id: '<AUTH0_CLIENT_ID>' }); //with promises createAuth0Client({ domain: '<AUTH0_DOMAIN>', client_id: '<AUTH0_CLIENT_ID>' }).then(auth0 => { //... }); ``` ### 1 - Login ```html <button id="login">Click to Login</button> ``` ```js //with async/await document.getElementById('login').addEventListener('click', async () => { await auth0.loginWithPopup(); //logged in. you can get the user profile like this: const user = await auth0.getUser(); console.log(user); }); //with promises document.getElementById('login').addEventListener('click', () => { auth0.loginWithPopup().then(token => { //logged in. you can get the user profile like this: auth0.getUser().then(user => { console.log(user); }); }); }); ``` ### 2 - Calling an API ```html <button id="call-api">Call an API</button> ``` ```js //with async/await document.getElementById('call-api').addEventListener('click', async () => { const accessToken = await auth0.getTokenSilently(); const result = await fetch('https://myapi.com', { method: 'GET', headers: { Authorization: `Bearer ${accessToken}` } }); const data = await result.json(); console.log(data); }); //with promises document.getElementById('call-api').addEventListener('click', () => { auth0 .getTokenSilently() .then(accessToken => fetch('https://myapi.com', { method: 'GET', headers: { Authorization: `Bearer ${accessToken}` } }) ) .then(result => result.json()) .then(data => { console.log(data); }); }); ``` ### 3 - Logout ```html <button id="logout">Logout</button> ``` ```js import createAuth0Client from '@auth0/auth0-spa-js'; document.getElementById('logout').addEventListener('click', () => { auth0.logout(); }); ``` ## Contributing We appreciate feedback and contribution to this repo! Before you get started, please see the following: - [Auth0's general contribution guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md) - [Auth0's code of conduct guidelines](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md) - [This repo's contribution guide](https://github.com/auth0/auth0-spa-js/blob/master/CONTRIBUTING.md) ## Support + Feedback This SDK is in Early Access with selected stakeholders. We process feedback and provide support via private channels. ## Frequently Asked Questions For a rundown of common issues you might encounter when using the SDK, please check out [the FAQ](https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md). ## Vulnerability Reporting Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://auth0.com/whitehat) details the procedure for disclosing security issues. ## What is Auth0? Auth0 helps you to easily: - implement authentication with multiple identity providers, including social (e.g., Google, Facebook, Microsoft, LinkedIn, GitHub, Twitter, etc), or enterprise (e.g., Windows Azure AD, Google Apps, Active Directory, ADFS, SAML, etc.) - log in users with username/password databases, passwordless, or multi-factor authentication - link multiple user accounts together - generate signed JSON Web Tokens to authorize your API calls and flow the user identity securely - access demographics and analytics detailing how, when, and where users are logging in - enrich user profiles from other data sources using customizable JavaScript rules [Why Auth0?](https://auth0.com/why-auth0) ## License This project is licensed under the MIT license. See the [LICENSE](https://github.com/auth0/auth0-spa-js/blob/master/LICENSE) file for more info.