Implementing Auth0

I am trying to implements Auth0 to allow front end login without using Kirby users. I have it basically working but its its rough and ready and i dont think ive gone about it in the best way.

What im struggling with is passing the logged in sesson back to the page so that the users login profile can be retrived. Whilst i can this in raw code on a page, this really needs to be available on evey page of the site.

Right now i have a site method that setsup the auth0 login, which works and i can dump the users name, email, gravatar image etc in the page, but what i think i really need to do is to make a route or routes that pass that though to a a controller.

Essentially im trying to translate this quick guide into the “Kirby” way of doing things. Auth0 PHP SDK Quickstarts: Login so basically using the Kirby router instead of a stand alone one.

Could some please point me in the right direction?

i will post a plugin version with routes based on your example code soonish.

<?php

Kirby::plugin('auth0/auth0', [
    'siteMethods' => [
        'auth0' => function () {
            return new \Auth0\SDK\Auth0([
                'domain' => $_ENV['AUTH0_DOMAIN'],
                'clientId' => $_ENV['AUTH0_CLIENT_ID'],
                'clientSecret' => $_ENV['AUTH0_CLIENT_SECRET'],
                'cookieSecret' => $_ENV['AUTH0_COOKIE_SECRET']
            ]);
        }
    ],
    'routes' => [
        [
            'pattern' => 'auth0-test', // or any other template
            'action' => function () {
                $auth0 = site()->auth0();
                $session = $auth0->getCredentials();

                if ($session === null) {
                    // The user isn't logged in.
                    echo '<p>Please <a href="auth0/login">log in</a>.</p>';
                    return;
                }

                // The user is logged in.
                echo '<pre>';
                print_r($session->user);
                echo '</pre>';

                echo '<p>You can now <a href="auth0/logout">log out</a>.</p>';
            }
        ],
        [
            'pattern' => 'auth0/login',
            'action' => function () {
                $auth0 = site()->auth0();
                $auth0->clear();
                go($auth0->login(url('auth0/callback')));
            }
        ],
        [
            'pattern' => 'auth0/callback',
            'action' => function () {
                $auth0 = site()->auth0();
                $auth0->exchange(url('auth0/callback'));
                go(url('auth0-test'));
            }
        ],
        [
            'pattern' => 'auth0/logout',
            'action' => function () {
                $auth0 = site()->auth0();
                go($auth0->logout(url('auth0-test'))));
            }
        ],
    ],
]);

Thanks @bnomei this set me on the right “route” :rofl: :slight_smile:

1 Like