Skip to content

Commit

Permalink
Set strategy on Auth0 SDK creation, update to spa-js v2 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
evansims authored Jun 26, 2023
2 parents 2f7c4b3 + aab18ce commit 75e1ef5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
5 changes: 4 additions & 1 deletion app/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
},
"config": {
"optimize-autoloader": true,
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"scripts": {
"app": [
Expand Down
1 change: 1 addition & 0 deletions app/src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function setupAuth0(
'clientSecret' => $env['AUTH0_CLIENT_SECRET'] ?? null,
'audience' => ($env['AUTH0_AUDIENCE'] ?? null) !== null ? [trim($env['AUTH0_AUDIENCE'])] : null,
'organization' => ($env['AUTH0_ORGANIZATION'] ?? null) !== null ? [trim($env['AUTH0_ORGANIZATION'])] : null,
'strategy' => SdkConfiguration::STRATEGY_API,
]);

// Setup the Auth0 SDK.
Expand Down
2 changes: 1 addition & 1 deletion app/src/ApplicationTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function renderTemplate(
extract($variables);

if (file_exists($templatePath) === false) {
throw new \Exception("Template file not found: ${template}");
throw new \Exception("Template file not found: {$template}");
}

try {
Expand Down
38 changes: 22 additions & 16 deletions app/templates/spa.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="//unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<script src="https://cdn.auth0.com/js/auth0-spa-js/1.13/auth0-spa-js.production.js"></script>
<script src="https://cdn.auth0.com/js/auth0-spa-js/2.0/auth0-spa-js.production.js"></script>

<title>Auth0 PHP Quickstart for Backend APIs</title>

Expand Down Expand Up @@ -121,33 +121,39 @@
</main>

<script>
createAuth0Client({
auth0.createAuth0Client({
domain: '<?php echo $config->getDomain(); ?>',
client_id: '<?php echo $config->getClientId(); ?>',
audience: '<?php echo $config->defaultAudience(); ?>',
redirect_uri: '<?php echo $router->getUri('/', ''); ?>',
scope: '<?php echo $config->formatScope() ?>'
}).then(auth0 => {
clientId: '<?php echo $config->getClientId(); ?>',
authorizationParams: {
audience: '<?php echo $config->defaultAudience(); ?>',
redirect_uri: '<?php echo $router->getUri('/', ''); ?>',
scope: '<?php echo $config->formatScope(); ?>'
}
}).then(auth0Instance => {
var hasAuthParams = window.location.href.indexOf('code=') !== -1 && window.location.href.indexOf('state=') !== -1;

document.getElementById('login').addEventListener('click', () => {
auth0.loginWithRedirect().catch(() => {});
auth0Instance.loginWithRedirect().catch(() => {});
});

document.getElementById('logout').addEventListener('click', () => {
auth0.logout({ returnTo: '<?php echo $router->getUri('/', ''); ?>' });
auth0Instance.logout({
logoutParams: {
returnTo: '<?php echo $router->getUri('/', ''); ?>'
}
});
});

auth0.getUser().then(user => {
auth0Instance.getUser().then(user => {
if (user) {
setAppState(true, user);
callBackendApi(auth0);
callBackendApi(auth0Instance);
return;
} else if (hasAuthParams) {
auth0.handleRedirectCallback().then(redirectResult => {
auth0.getUser().then(user => {
auth0Instance.handleRedirectCallback().then(redirectResult => {
auth0Instance.getUser().then(user => {
setAppState(true, user);
callBackendApi(auth0);
callBackendApi(auth0Instance);
});
}).catch(() => {
setAppState(false);
Expand Down Expand Up @@ -198,8 +204,8 @@ function setAppState(loggedIn, user) {
login.classList.remove('hidden');
}

async function callBackendApi(auth0) {
var accessToken = auth0 ? await auth0.getTokenSilently() : false;
async function callBackendApi(auth0Instance) {
var accessToken = auth0Instance ? await auth0Instance.getTokenSilently() : false;
var headers = {};

if (accessToken) {
Expand Down

0 comments on commit 75e1ef5

Please sign in to comment.