We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21943
    • 6 Posts
    I’m trying to write a snippet that validates the user with LightOpenID, and then logs them in with their MODx user, or creates a new user if necessary.

    Bob’s post on creating users has been very useful,
    http://modxcms.com/forums/index.php/topic,55944.msg322276.html#msg322276

    But I still can’t figure out how to login the dear user, can you give some suggestive API examples?

    The snippet code so far is below. LightOpenID is really light, they have these quick examples for google and for the others.
    <?php
    
    if (!$modx) die ('MODx is missing.');
    $basepath = $modx->getOption('base_path');
    set_include_path($basepath.'assets/includes/lightopenid/');
    require_once('openid.php');
    
    // is the user logged in already?
    $loginContext = isset($_REQUEST['login_context']) && !empty($_REQUEST['login_context']) ? $_REQUEST['login_context'] : $modx->context->get('key');
    $authenticated = $modx->user->isAuthenticated($loginContext);
    if ($authenticated) {
        $currentUser = $modx->user;
        $currentUserId = $currentUser->get('id');
        $currentUserName = $currentUser->get('username');
    }
    
    // output variable.
    $o = '';
    
    try {
        if (!isset($_GET['openid_mode'])) {
            if (isset($_POST['btnLoginGoogle'])) {
                // if the user clicked the Google button, other options later
                $openid = new LightOpenID;
                $openid->identity = 'https://www.google.com/accounts/o8/id';
                // the attributes we will use to find the user in MODx users, or register a new user for them.
                $openid->required = array('namePerson', 'namePerson/first', 'namePerson/last', 'contact/email');
                header('Location: ' . $openid->authUrl());
            }
        } elseif ($_GET['openid_mode'] == 'cancel') {
            $o .= 'Login Cancelled.';
            return $o;
        } else {
            $openid = new LightOpenID;
            if ($openid->validate()) {
                $identity = $openid->identity;
                $attr = $openid->getAttributes();
                // the array of requested OpenID attributes.
                // $email = $attr['contact/email'];
                // $first_name = $attr['namePerson/first'];
                // etc.
    /******************
     * TODO: see if we have this user already, then login or register the user with email address
     ******************/
            }
        }
    } catch(ErrorException $e) {
        $o .= $e->getMessage();
        return $o;
    }
    
    if ($authenticated) {
        $o .= '<p>Welcome, '.$currentUserName.'</p>';
    } else {
        // It's a one-trick pony at the moment.
        $o .= '<form method="POST" action="" >
    <input type="submit" name="btnLoginGoogle" value="Login with Google" /><br />
    </form>';
    }
    
    return $o;
    
    ?>
    
      • 21943
      • 6 Posts
      Quote from: nyiti at Nov 27, 2010, 04:10 AM

      I’m trying to write a snippet that validates the user with LightOpenID, and then logs them in with their MODx user, or creates a new user if necessary.

      OK, figured out from Jason’s login code that login with the MODx API works like this:
      http://modxcms.com/forums/index.php/topic,56443.msg330473.html#msg330473