custom login

I'm developing a plugin that will use a custom login page using Elgg 2.3.14.  Currently I'm simply displaying a "Login" link with a hard-coded <a href> in an attempt to point to a login.php page at the root of my plugin directory.  I'm using the walled_garden.php and have checked the administrative setting to "only allow logged in users to view content".  Below are the relevant plugin files that I have modified:

/var/www/html/mod/auth0_login/start.php

<?php
elgg_register_event_handler('init', 'system', 'auth0_init');
 
function auth0_init() {
    elgg_register_page_handler('auth0_login', 'auth0_page_handler');
    elgg_extend_view('css/elgg', 'auth0_login/css');
}
 
function auth0_page_handler() {
    echo elgg_view_resource('auth0_login');
}
 

/var/www/html/mod/auth0_login/views/default/page/walled_garden.php

<?php
/**
 * Walled garden page shell
 *
 * Used for the walled garden index page
 *
 * @uses $vars['head']        Parameters for the <head> element
 * @uses $vars['body']        The main content of the page
 * @uses $vars['sysmessages'] A 2d array of various message registers, passed from system_messages()
 */
// render content before head so that JavaScript and CSS can be loaded. See #4032
 
$messages = elgg_view('page/elements/messages', array('object' => $vars['sysmessages']));
$content = $vars["body"];
ob_start(); ?>
<div class="elgg-page elgg-page-walledgarden">
        <div class="elgg-page-messages">
                <?php echo $messages ?>
        </div>
        <div class="elgg-body-walledgarden">
                <a href="auth0_login/login.php">Login</a>
        </div>
</div>
<?php
$body = ob_get_clean();
$body .= elgg_view('page/elements/foot');
$head = elgg_view('page/elements/head', $vars['head']);
echo elgg_view("page/elements/html", array("head" => $head, "body" => $body));
 
/var/www/html/mod/auth0_login/login.php
<?php
  require __DIR__ . '/vendor/autoload.php';
  
  // ... other use declarations
  use josegonzalez\Dotenv\Loader;
  
  // Setup environment vars
  $Dotenv = new Loader(__DIR__ . '/.env');
  $Dotenv->parse()->putenv(true);
  
  use Auth0\SDK\Auth0;
  
  $domain        = getenv('AUTH0_DOMAIN');
  $client_id     = getenv('AUTH0_CLIENT_ID');
  $client_secret = getenv('AUTH0_CLIENT_SECRET');
  $redirect_uri  = getenv('AUTH0_CALLBACK_URI');
  $audience      = getenv('AUTH0_AUDIENCE');
  
  if($audience == ''){
    $audience = 'https://&#39; . $domain . '/userinfo';
  }
  
  $auth0 = new Auth0([
    'domain' => $domain,
    'client_id' => $client_id,
    'client_secret' => $client_secret,
    'redirect_uri' => $redirect_uri,
    'persist_id_token' => true,
    'persist_access_token' => true,
    'persist_refresh_token' => true,
  ]);
  $auth0->login();
 
The login.php isn't accessible because the walled_garden config is true.  When I click the "Login" link, I get the error:
 
"You must be logged in to view the requested page"
 
How do I open access to this page in this situation?  If I put the login.php in my elgg document root at /var/www/html/ everything works fine, but from within my plugin root, it doesn't.
  • You must to expose your custom URL throught Walled Garden in start.php:

    elgg_register_plugin_hook_handler('public_pages', 'walled_garden', 'my_plugin_walled_garden_public_pages');
    
    function my_plugin_walled_garden_public_pages($hook, $type, $pages) {
       $pages[] = 'auth0_login';
       return $pages;
    }

    Learn more.

    Also, you can use this code in the mod/auth0_login/views/default/page/walled_garden.php:

    echo elgg_view('output/url', [
       'href' => 'auth0_login/login',
       'text' => elgg_echo('login'),
    ]);
     
    instead of:
    <a href="auth0_login/login.php">Login</a>
  • Thank you.  I tried that and it worked!

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking