vanilla/js-connect-php

Vanilla的jsConnect SSO系统的客户端库。

v4.0.1 2023-03-27 21:56 UTC

This package is auto-updated.

Last update: 2024-08-28 00:50:06 UTC


README

注意: Vanilla最近已经更新了jsConnect协议,以适应当前浏览器对第三方cookie的拦截。请确保更新您的库以使用该协议。完成此操作后,您需要配置Vanilla在仪表板下的jsConnect设置中启用此协议。

关于jsConnect

jsConnect协议是一个简单的单点登录(SSO)框架,允许您轻松地使用自己的网站登录到Vanilla网站。它旨在尽可能减少编程工作量。您需要执行以下操作

  1. 编写一个页面,该页面响应当前登录用户的信息。
  2. 您的登录主页应能够重定向到查询字符串中提供的URL。
  3. 您可以提供注册页面,但该页面也必须能够通过查询字符串参数进行重定向。

安装

安装jsConnect有两种方法。

  1. 您可以通过composer安装此库。您需要要求vanilla/js-connect-php
  2. 您可以使用提供的functions.jsconnect.php。这是安装Vanilla的旧方法。它仍然有效,但我们建议过渡到composer安装。

使用方法

使用此jsConnect库有两种方法。有一种面向对象的方法和一种函数式方法。

面向对象的使用

如果您是jsConnect的新手,我们建议使用面向对象的使用方式。以下是您的页面可能的样子。

$jsConnect = new \Vanilla\JsConnect\JsConnect();

// 1. Add your client ID and secret. These values are defined in your dashboard.
$jsConnect->setSigningCredentials($clientID, $secret);

// 2. Grab the current user from your session management system or database here.
$signedIn = true; // this is just a placeholder

// YOUR CODE HERE.

// 3. Fill in the user information in a way that Vanilla can understand.
if ($signedIn) {
    // CHANGE THESE FOUR LINES.
  	$jsConnect
        ->setUniqueID('123')
      	->setName('Username')
      	->setEmail('user@example.com')
      	->setPhotoUrl('https://example.com/avatar.jpg');
} else {
  $jsConnect->setGuest(true);
}

// 4. Generate the jsConnect response and redirect.
$jsConnect->handleRequest($_GET);

函数式使用

函数式使用主要用于向后兼容。如果您目前正在使用此方法,则可以继续使用。但是,当您有时间时,您可能希望将代码移植到面向对象的方法。

以下是函数式使用的示例

// 1. Get your client ID and secret here. These must match those in your jsConnect settings.
$clientID = "1234";
$secret = "1234";

// 2. Grab the current user from your session management system or database here.
$signedIn = true; // this is just a placeholder

// YOUR CODE HERE.

// 3. Fill in the user information in a way that Vanilla can understand.
$user = array();

if ($signedIn) {
    // CHANGE THESE FOUR LINES.
    $user['uniqueid'] = '123';
    $user['name'] = 'John PHP';
    $user['email'] = 'john.php@example.com';
    $user['photourl'] = '';
}

// 4. Generate the jsConnect string.

// This should be true unless you are testing.
// You can also use a hash name like md5, sha1 etc which must be the name as the connection settings in Vanilla.
$secure = true;
writeJsConnect($user, $_GET, $clientID, $secret, $secure);