Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anonymous UUID to unauthinticated API Calls #554

Merged
merged 2 commits into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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