Search code examples
oauth-2.0google-oauthphpbb3

External login using Google for PHPBB 3.1


I currently allow users to login into my website using their PHPBB credentials. I use the method described here:

https://wiki.phpbb.com/Practical.External_login

However, I would like to upgrade to PHPBB 3.1 and also allow logins by placing a "Login with Google", "Login with Facebook" buttons on the form as an alternative.

I have the "Login with Google" working on the forum itself using the new PHPBB 3.1 feature, but I have no idea how to implement that as an external login on my website.

The biggest issue I run into is that if the login is successful using "Google", my user is redirected to the forum. However, I would like the user to be redirected to a specific page on my website.


Solution

  • I figured out how to do this:

    Create a "Login with Google" button for example and link it to:

    http://www.example.com/forum/loginoauth.php?mode=login&login=external&oauth_service=google

    Here is my loginoauth.php file:

    <?php
    
    // phpBB inclusion protection
    define('IN_PHPBB', true);
    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    require($phpbb_root_path . 'common.' . $phpEx);
    
    // Start session management
    $user->session_begin();
    $auth->acl($user->data);
    
    if ($user->data['is_registered'])
    {
        redirect('http://www.example.com/profile');
    }
    else
    {
        //$autologin    = $request->is_set_post('autologin');
        $admin      = ($admin) ? 1 : 0;
    
        // Check if the supplied username is equal to the one stored within the database if re-authenticating
        if ($admin && utf8_clean_string($username) != utf8_clean_string($user->data['username']))
        {
            // We log the attempt to use a different username...
            add_log('admin', 'LOG_ADMIN_AUTH_FAIL');
            trigger_error('NO_AUTH_ADMIN_USER_DIFFER');
        }
    
        // If authentication is successful we redirect user to previous page
        // $result = $auth->login($username, $password, $autologin, $viewonline, $admin);
        $result = $auth->login('','');
    
        // The result parameter is always an array, holding the relevant information...
        if ($result['status'] == LOGIN_SUCCESS)
        {
            redirect('http://www.example.com/profile');
        }
    }
    ?>