A Discord provider for Laravel Socialite.
Go to file
Martin Bean d4fc0f73a2 Initial commit 2020-05-22 13:32:55 +01:00
src Initial commit 2020-05-22 13:32:55 +01:00
.editorconfig Initial commit 2020-05-22 13:32:55 +01:00
.gitignore Initial commit 2020-05-22 13:32:55 +01:00
README.md Initial commit 2020-05-22 13:32:55 +01:00
composer.json Initial commit 2020-05-22 13:32:55 +01:00

README.md

Discord provider for Laravel Socialite

A provider for [Laravel Socialite][1] that allows authentication as a Discord user. Authenticating as a bot is currently not supported.

Installation

composer require martinbean/socialite-discord-provider

Usage

The package registers a Socialite driver with the name of discord.

Before using the driver, create an OAuth application in Discords developer portal: https://discord.com/developers/applications

Set your client ID and client secret as environment variables, and then reference them in your config/services.php file. You will also need to add a redirect URL to your application.

<?php

// config/services.php

return [

    // Any other services

    'discord' => [
        'client_id' => env('DISCORD_CLIENT_ID'),
        'client_secret' => env('DISCORD_CLIENT_SECRET'),
        'redirect' => '/auth/discord/callback',
    ],

];

The redirect value will need to match a redirect URL in your Discord application settings. It can be relative as above.

Then, create a controller to redirect and handle the token callback:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Laravel\Socialite\Facades\Socialite;

class DiscordController extends Controller
{
    /**
     * Redirect the user to the Discord authentication page.
     *
     * @return \Illuminate\Http\Response
     */
    public function redirectToProvider()
    {
        return Socialite::driver('discord')->redirect();
    }

    /**
     * Obtain the user information from Discord.
     *
     * @return \Illuminate\Http\Response
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver('discord')->user();

        // $user->token;
    }
}

Scopes

Discord supports various scopes. You can find a list here: https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes To request additional scopes when authenticating, you can use the scopes method before redirecting:

return Socialite::driver('discord')
    ->scopes(['guilds', 'messages.read'])
    ->redirect();

Issues

If you have any problems using this package, please open an issue on the [GitHub repository][2].

[1] https://laravel.com/docs/master/socialite [2]: https://github.com/martinbean/socialite-discord-provider