trwl/socialite-mastodon

Socialite for Mastodon
56,030
Install
composer require trwl/socialite-mastodon
Latest Version:1.2.4
PHP:>=7.0.0
Laravel:6 – 13
License:MIT
Last Updated:Sep 19, 2022
Links: GitHub  ·  Packagist
Maintainer: HerrLevin_

Socialite for Mastodon

https://github.com/tootsuite/mastodon

Install

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

Create App and get the client_id & client_secret

  1. Go to your Mastodon's user preferences page.
  2. Go to development page.
  3. Create new application.
  4. Get Client key and Client secret

Usage

Use one instance

routes/web.php

Route::get('/', 'MastodonController@index');
Route::get('callback', 'MastodonController@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);
    }
}

Set scopes

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

Customize domain example

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);
    }

LICENCE

MIT

Related Packages

revolution/socialite-mastodon

Socialite for Mastodon

302,817 13
revolution/laravel-mastodon-api

Mastodon API for Laravel

92,342 27
cali/socialite

Laravel extension for OAuth libraries

26 0
revolution/socialite-amazon

Socialite for Login with Amazon

187,713 13
revolution/socialite-wordpress

Socialite for WordPress

13,785 4

Version History

Version Released PHP License
1.2.4 >=7.0.0 MIT
1.2.3 >=7.0.0 MIT
1.2.2 >=7.0.0 MIT
1.2.1 >=7.0.0. MIT
1.2.0 >=7.0.0. MIT