I am facing an issue when try to login with Auth0. I create 2 function for this and route for this but when I try to get login it show me login page of auth but when I try to get all data with help of this function “$auth0->exchange(base_url(). ‘callback’);” as per documention but it cause an error and didn’t display any thing. I also share every thing which i try with my code Please help me out with.
Route:-
$route[‘auth_callback’] = ‘index/auth_callback’;
$route[‘callback’] = ‘index/setupAuthCallback’;
Auth0 Login Button:-
Click here to Login with Auth0
Functions of Controller:-
function auth_callback() {
$auth0 = new \Auth0\SDK\Auth0([
‘domain’ => $this->auth[‘AUTH0_DOMAIN’],
‘clientId’ => $this->auth[‘AUTH0_CLIENT_ID’],
‘clientSecret’ => $this->auth[‘AUTH0_CLIENT_SECRET’],
‘cookieSecret’ => $this->auth[‘AUTH0_COOKIE_SECRET’]
]);
$auth0->clear();
header(“Location:” . $auth0->login(base_url(). ‘callback’));
exit;
}
function setupAuthCallback() {
$authConfig = [
'domain' => $this->auth['AUTH0_DOMAIN'],
'clientId' => $this->auth['AUTH0_CLIENT_ID'],
'clientSecret' => $this->auth['AUTH0_CLIENT_SECRET'],
'cookieSecret' => $this->auth['AUTH0_COOKIE_SECRET']
];
// Define the base URL and callback URL
$auth0 = new \Auth0\SDK\Auth0($authConfig);
// Handle callback
$hasAuthenticated = isset($_GET['state']) && isset($_GET['code']);
$hasAuthenticationFailure = isset($_GET['error']);
if ($hasAuthenticated) {
try {
$auth0->exchange(base_url(). 'callback');
} catch (\Throwable $th) {
printf('Unable to complete authentication: %s', $th->getMessage());
exit;
}
}
// When authentication was unsuccessful, the end user will be returned with an ?error in their request.
if ($hasAuthenticationFailure) {
printf('Authentication failure: %s', htmlspecialchars(strip_tags(filter_input(INPUT_GET, 'error'))));
exit;
}
$session = $auth0->getCredentials();
print_r($session);
exit;
}
Please look in this code and let me know where I got wrong?