Skip to content

Sample application for Auth0's tutorial on how to use JWT authentication with observables

License

Notifications You must be signed in to change notification settings

auth0-blog/auth-observables

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Authenticable Observables

This is the sample application for Auth0's tutorial on doing authentication with observables. The app calls a NodeJS backend for public and private Chuck Norris quotes, all through the use of observables.

Installation

  1. Do git submodule update --init to pull the NodeJS API into the server directory
  2. cd into server and do npm install
  3. Run the Node server by doing node server.js
  4. cd into client and serve app with a local web server
  5. Navigate to the app in your browser

Important Snippets

In this example app, we create streams for nearly everything. We also create new streams from others and project values using map and flatMap.

var secretQuoteStream = secretQuoteClickStream
  .map(function() {
    var jwt = localStorage.getItem('id_token');
    return {
      url: 'http://localhost:3001/api/protected/random-quote',
      jwt: jwt
    }
  });

We use the Fetch API for data retrieval and for POSTing credentials to get a JWT back.

function getJwt(credentials) {

  var config = {
    method: 'POST',
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body: credentials
  }

  return fetch('http://localhost:3001/sessions/create', config).then(function(data) {
    return data.json().then(function(jwt) {
      return jwt;
    });
  });
}

After all the streams are setup, we subscribe to them and observe the events that are emitted. We can do whatever we want with the emitted events, and for the case of the JWTs, we set them in local storage. For the retrieved quotes, we simply display them on the screen.

getJwtStream.subscribe(function(jwt) {
  localStorage.setItem('id_token', jwt.id_token);
});

quoteResponseStream.subscribe(function(text) {
  document.querySelector('.container h1').innerHTML = text;
});

What is Auth0?

Auth0 helps you to:

  • Add authentication with multiple authentication sources, either social like Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, amont others, or enterprise identity systems like Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider.
  • Add authentication through more traditional username/password databases.
  • Add support for linking different user accounts with the same user.
  • Support for generating signed Json Web Tokens to call your APIs and flow the user identity securely.
  • Analytics of how, when and where users are logging in.
  • Pull data from other sources and add it to the user profile, through JavaScript rules.

Create a Free Auth0 Account

  1. Go to Auth0 and click Sign Up.
  2. Use Google, GitHub or Microsoft Account to login.

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.

Author

Auth0

License

This project is licensed under the MIT license. See the LICENSE file for more info.

About

Sample application for Auth0's tutorial on how to use JWT authentication with observables

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published