PHP code example of cviebrock / discourse-php

1. Go to this page and download the library: Download cviebrock/discourse-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cviebrock / discourse-php example snippets


$sso = new Cviebrock\DiscoursePHP\SSOHelper();

// this should be the same in your code and in your Discourse settings:
$secret = 'super_secret_sso_key';
$sso->setSecret( $secret );

// load the payload passed in by Discourse
$payload = $_GET['sso'];
$signature = $_GET['sig'];

// validate the payload
if (!($sso->validatePayload($payload,$signature))) {
    // invaild, deny
    header("HTTP/1.1 403 Forbidden");
    echo("Bad SSO request");
    die();
}

$nonce = $sso->getNonce($payload);

// Insert your user authentication code here ...

// Required and must be unique to your application
$userId = '...';

// Required and must be consistent with your application
$userEmail = '...';

// Optional - if you don't set these, Discourse will generate suggestions
// based on the email address

$extraParameters = array(
    'username' => $userUsername,
    'name'     => $userFullName
);

// build query string and redirect back to the Discourse site
$query = $sso->getSignInString($nonce, $userId, $userEmail, $extraParameters);
header('Location: http://discourse.example.com/session/sso_login?' . $query);
exit(0);

composer