Skip to content

Commit

Permalink
Add anonymous UUID to unauthinticated API Calls (#554)
Browse files Browse the repository at this point in the history
* Add anonymous UUID to  unauthinticated API Calls

* Update jsdoc
  • Loading branch information
daltonfury42 authored Feb 27, 2021
1 parent 67237fb commit a4b94d5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
3 changes: 1 addition & 2 deletions simplq/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion simplq/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"react-spinners": "^0.9.0",
"react-to-print": "^2.12.2",
"reactour": "^1.18.0",
"styled-components": "^5.2.1"
"styled-components": "^5.2.1",
"uuid": "^8.3.2"
},
"scripts": {
"start": "npm run config && react-scripts start",
Expand Down
35 changes: 25 additions & 10 deletions simplq/src/api/auth.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { useAuth0 } from '@auth0/auth0-react';

import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';

// config.js is generated at runtime, so disabling eslint warning
/* eslint-disable import/no-unresolved, import/extensions */
import { baseURL } from '../config';

const ANONYMOUS_DEVICE_ID_KEY = 'anonymous-device-id';

/**
* Gets the authorization header value to be used to make the request.
*
* @param {Object} auth object returned by useAuth() from @auth0/auth0-react.
*/
const getAuthHeaderValue = async (auth) => {
// If user is logged in, get the token from the login provider.
if (auth.isAuthenticated) {
return `Bearer ${await auth.getAccessTokenSilently({ audience: baseURL })}`;
}

// Generate and store a unique identifier for the device and also persist
// it to local storage for later use.
if (localStorage.getItem(ANONYMOUS_DEVICE_ID_KEY) === null) {
localStorage.setItem(ANONYMOUS_DEVICE_ID_KEY, `anonymous-${uuidv4()}`);
}

return Promise.resolve(`Anonymous ${localStorage.getItem(ANONYMOUS_DEVICE_ID_KEY)}`);
};

/**
* A hook to access the makeAuthedRequest function.
*
Expand All @@ -21,17 +43,13 @@ const useMakeAuthedRequest = () => {
* @returns {Object} request response data as a Promise.
*/
const makeAuthedRequest = async (request) => {
const accessToken = auth.isAuthenticated
? await auth.getAccessTokenSilently({ audience: baseURL })
: 'anonymous';

const authedRequest = axios({
baseURL,
...request,
headers: {
...request.headers,
// Add the Authorization header to the existing headers
Authorization: `Bearer ${accessToken}`,
Authorization: await getAuthHeaderValue(auth),
},
});
const response = await authedRequest;
Expand All @@ -53,16 +71,13 @@ const useMakeAuthedRequest = () => {
* @param {Object} request object created by requestFactory.
*/
const makeAuthedRequest = async (auth, request) => {
const accessToken = auth.isAuthenticated
? await auth.getAccessTokenSilently({ audience: baseURL })
: 'anonymous';
return axios({
baseURL,
...request,
headers: {
...request.headers,
// Add the Authorization header to the existing headers
Authorization: `Bearer ${accessToken}`,
Authorization: await getAuthHeaderValue(auth),
},
}).then((response) => {
return response.data;
Expand Down

0 comments on commit a4b94d5

Please sign in to comment.