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

Fixed map to clientId and set Auth0 audience #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
62 changes: 36 additions & 26 deletions src/utils/auth0.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let isAvailable = false;
try {
new Auth0Client({
domain: 'example.eu.auth0.com',
client_id: 'some_client_id',
clientId: 'some_client_id',
audience: 'https://example.com/',
cacheLocation: 'memory',
});
Expand Down Expand Up @@ -74,10 +74,8 @@ const init = () => {
try {
client = new Auth0Client({
domain: config.domain,
client_id: config.client_id,
audience: config.audience,
clientId: config.client_id,
cacheLocation: 'localstorage',
advancedOptions: { defaultScope: 'openid' },
});
} catch (e) {
return false;
Expand Down Expand Up @@ -143,27 +141,29 @@ const login = async (queryParams) => {
...getConfig(),
};

let queryString = [];
for (let n in queryParams) {
queryString.push(n + '=' + encodeURIComponent(queryParams[n]));
}

if (queryString.length !== 0) {
try {
let url = new URL(config.redirect_uri);
if (url.search.length > 1) {
url.search = url.search + '&' + queryString.join('&');
} else {
url.search = '?' + queryString.join('&');
}

config.redirect_uri = url.href;
} catch (e) {}
}

const options = {
redirect_uri: config.redirect_uri,
};
let queryString = [];
for (let key in queryParams) {
if (queryParams[key]) {
queryString.push(`${key}=${queryParams[key]}`); // No encoding for keys or values
}
}
if (queryString) {
try {
const url = new URL(config.redirect_uri);
url.search = url.search ? `${url.search}&${queryString}` : queryString;
config.redirect_uri = url.href;
} catch (e) {
console.error('Error constructing redirect URI:', e);
}
}

const options = {
authorizationParams: {
redirect_uri: config.redirect_uri,
scope: 'openid profile email',
audience: config.audience
}
};

try {
await client.loginWithRedirect(options);
Expand Down Expand Up @@ -192,8 +192,18 @@ const getToken = async () => {
return token;
}

const config = {
...getConfig(),
};
const options = {
authorizationParams: {
scope: config.scope,
audience: config.audience
}
};

try {
token = await client.getTokenSilently();
token = await client.getTokenSilently(options);
} catch (error) {
console.error(error);
}
Expand Down