Skip to content

Instantly share code, notes, and snippets.

@damianesteban
Created August 20, 2019 23:59
Show Gist options
  • Save damianesteban/8ef33eeabfc1fd3c7ad5dcdb5865787a to your computer and use it in GitHub Desktop.
Save damianesteban/8ef33eeabfc1fd3c7ad5dcdb5865787a to your computer and use it in GitHub Desktop.
auth0 example
import * as auth0 from 'auth0';
import axios from 'axios';
// axios client example
const axiosClient = axios.create({
baseURL: 'https://APP_DOMAIN',
timeout: 5000,
maxRedirects: 2,
withCredentials: true,
});
// Fetches token from Auth0
const fetchToken = () => {
const auth0Client = new auth0.AuthenticationClient({
domain: 'AUTH_DOMAIN',
clientId: 'CLIENT_ID',
clientSecret: CLIENT_SECRET,
});
return auth0Client.clientCredentialsGrant({
audience: 'AUDIENCE',
});
});
};
// Sets the Auth0 header
function setAuthHeader(token: string | null): void {
if (token) {
axiosClient.defaults.headers.common['Authorization'] = `Bearer ${token}`;
} else {
delete axiosClient.defaults.headers.common['Authorization'];
}
}
const setClientAccessToken (): Promise<any> => fetchToken().then((res): any => setAuthHeader(res.access_token)),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment