OctoberCMS 外掛 第三方登入

octck發表於2021-04-14

Plus.Socialite

基於 overtrue/socialite 實現的 OctoberCMS 第三方登入外掛
可以繫結和解綁。支援自定義擴充套件平臺。

細節

參考學習的外掛

Flynsarmy.SocialLogin
主要參考其配置的方法。使用如下的欄位可以獲取到和所需配置結構一致的資料,只是只能在編輯器中編寫 fields.yaml ,不能用 Builder 外掛直接生成了。

providers[feishu][client_id]:
    label: 'App ID'
    span: left
    type: text
    tab: 飛書

Ceyleon.SocialAuth
主要參考其授權後使用者的處理。

根據平臺標識和使用者唯一標識(如 open_id)確定授權資訊,實現多平臺繫結解綁

用到的技術

overtrue/socialite

    public function authorize($profile = 'default')
    {
        $config=SocialHelper::getProfileConfigs($profile);
        $socialite = new SocialiteManager($config);
        if($config['is_extend']){
            $nameCustomProvider='Plus\Socialite\Classes\Providers\\'.ucfirst($profile).'Provider';
            $socialite->extend($profile, function(array $config)use($nameCustomProvider){
                return new $nameCustomProvider($config);
            });
        }
        $url  = $socialite->create($profile)->redirect();
        return redirect($url);
    }

如上示例,自定義擴充套件的服務放在 Plus\Socialite\Classes\Providers
當前自定義的有:
直接複製修改的 支付寶沙箱。
因為不符合我的需求複製修改的 飛書。
練習學習寫的 Gitee 和 Gitlab。
以及實踐寫的 小米。(小米平臺用code獲取token這一步是get提交的,和預設的不一樣,是一個很好的實踐例子)

截圖

後臺設定


註冊繫結

登入繫結

登入

使用者繫結

使用者解綁

內容管理元件

用法說明

安裝

本外掛需要提前安裝 RainLab.User 外掛

專案地址:https://github.com/FollowZero/oct-plugin-socialite.git

下載後解壓放在 plugins 資料夾下

頁面建立

在內容管理系統建立頁面。登入、繫結、使用者繫結。每個頁面都要加上 socialite 元件。每個頁面都用預設的就行,因為跟據不同場景都加有判斷。也可以使用元件的部件。

這些頁面建立後在 socialite 的通用設定中設定一下,用於登入成功後的跳轉和登入繫結的跳轉

登入頁面

在登入頁,寫上如下程式碼即可新增上開啟的平臺連結。

{% partial socialite ~ '::social_login_link' %}

裡邊就是將連結迴圈輸出。也可以跟據需求替換連結樣式或新增顯示平臺logo

{% for link in social_links %}
    <a href="{{ link.url }}" class="btn btn-default">{{ link.title }}</a>
{% endfor %}{% for link in social_links %}
    {% if link.cover_path %}
        <img src="{{ link.cover_path }}" alt="">
    {% endif %}
    <a href="{{ link.url }}" class="btn btn-default">{{ link.title }}</a>
{% endfor %}
繫結頁面
{% component 'socialite' %}

參考 RainLab.User 的登入註冊頁面。新增已授權資訊的id(follow_id)的隱藏表單

使用者繫結和解綁
{% partial socialite ~ '::user_bind' %}
其他

小米授權登入需要在授權域名下能訪問指定的頁面。
建立同名的 URL 的頁面,將下載的頁面內容複製到建立的頁面中。

demo地址

本作品採用《CC 協議》,轉載必須註明作者和本文連結
鄭州最帥的php程式設計師

相關文章