Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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