revolution/socialite-mastodon

Mastodon 的 Socialite

1.4.4 2024-04-04 07:57 UTC

This package is auto-updated.

Last update: 2024-09-06 06:42:58 UTC


README

https://github.com/mastodon/mastodon

需求

  • PHP >= 8.0

没有版本限制。它可能在未来的版本中停止工作。

安装

composer require revolution/socialite-mastodon

config/services.php

    'mastodon' => [
        'domain'        => env('MASTODON_DOMAIN'),
        'client_id'     => env('MASTODON_ID'),
        'client_secret' => env('MASTODON_SECRET'),
        'redirect'      => env('MASTODON_REDIRECT'),
        //'read', 'write', 'follow'
        'scope'         => ['read'],
    ],

.env

MASTODON_DOMAIN=https://mastodon.social
MASTODON_ID=
MASTODON_SECRET=
MASTODON_REDIRECT=https://example.com/callback

创建应用程序并获取 client_id 和 client_secret

  1. 前往你的 Mastodon 用户偏好设置页面。
  2. 前往开发页面。
  3. 创建新应用程序。
  4. 获取 Client keyClient secret

使用方法

使用一个实例

routes/web.php

Route::get('/', [MastodonController::class, 'index']);
Route::get('callback', [MastodonController::class, 'callback']);

MastodonController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Socialite;

class MastodonController extends Controller
{
    public function index()
    {
        return Socialite::driver('mastodon')->redirect();
    }

    public function callback()
    {
        $user = Socialite::driver('mastodon')->user();
        dd($user);
    }
}

设置作用域

return Socialite::driver('mastodon')
           ->setScopes(config('services.mastodon.scope', ['read']))
           ->redirect();

自定义域名示例

Mastodon API for Laravel https://github.com/kawax/laravel-mastodon-api

    public function login(Request $request)
    {
        //input domain by user
        $domain = $request->input('domain');

        //get app info. domain, client_id, client_secret ...
        //Server is Eloquent Model
        $server = Server::where('domain', $domain)->first();

        if (empty($server)) {
            //create new app
            $info = Mastodon::domain($domain)->createApp('my-app', 'https://example.com/callback', 'read');

            //save app info
            $server = Server::create([
                'domain'        => $domain,
                'client_id'     => $info['client_id'],
                'client_secret' => $info['client_secret'],
            ]);
        }

        //change config
        config(['services.mastodon.domain' => $domain]);
        config(['services.mastodon.client_id' => $server->client_id]);
        config(['services.mastodon.client_secret' => $server->client_secret]);

        session(['mastodon_domain' => $domain]);
        session(['mastodon_server' => $server]);

        return Socialite::driver('mastodon')->redirect();
    }
    
    public function callback()
    {
        $domain = session('mastodon_domain');
        $server = session('mastodon_server');
    
        config(['services.mastodon.domain' => $domain]);
        config(['services.mastodon.client_id' => $server->client_id]);
        config(['services.mastodon.client_secret' => $server->client_secret]);
    
        $user = Socialite::driver('mastodon')->user();
        dd($user);
    }

许可

MIT