huxe-2020-auth0-example

This is an Auth0 example application using Vue.js (and Express), following the two official quick start guides by Auth0.

Stars
1

HUXE 2020 Auth0 Example

This is an Auth0 example application using Vue.js (and Express), following the two official quick start guides by Auth0.

  1. Auth0 Quick Starts > Single-Page App > Vue > Login
  2. Auth0 Quick Starts > Single-Page App > Vue > Calling an API

Start the client and server, then open http://localhost:8080.

cd client
npm install
npm run start
cd server
npm install
npm run start

Serverless

❗️ The server can be changed to a cloud function, so you don’t have to host the server yourself. The following is an example using Netlify Functions (with lot’s of error handling skipped, so it’s easier to read).

import jwt from 'jsonwebtoken';
import jwksClient from 'jwks-rsa';

const authClient = jwksClient({
  cache: true,
  jwksUri: 'https://manuelwieser.eu.auth0.com/.well-known/jwks.json',
  audience: 'https://huxe2020.example.com',
  issuer: 'https://manuelwieser.eu.auth0.com/'
});

function checkAuth(event) {
  return new Promise((resolve, reject) => {
    const authToken = event.headers.authorization.substring(7);
    const decodedToken = jwt.decode(authToken, { complete: true });

    const kid = decodedToken.header.kid;
    authClient.getSigningKey(kid, (signError, key) => {
      const publicKey = key.getPublicKey();
      const options = { algorithms: 'RS256' };
      
      jwt.verify(authToken, publicKey, options, (verifyError, decoded) => {
        return resolve(decoded);
      });
    });
  });
}

exports.handler = (event, context, callback) => {
  checkAuth(event)
    .then(user => {
      return callback(null, {
        statusCode: 200,
        body: JSON.stringify({
          msg: 'Your Access Token was successfully validated!'
        })
      });
    })
    .catch(error => {
      return callback(null, {
        statusCode: 401,
        body: JSON.stringify({
          error: error.message
        })
      });
    });
};

Manuel Wieser https://manu.ninja https://twitter.com/manuelwieser https://www.paypal.me/manuninja