Coder Social home page Coder Social logo

socialite-discord-provider's Introduction

Discord provider for Laravel Socialite

A provider for Laravel Socialite that allows authentication as a Discord user or bot.

Installation

composer require martinbean/socialite-discord-provider:^1.2

Usage

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

Before using the driver, create an OAuth application in Discord’s 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 if you intend to authenticate as a user.

<?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.

Authenticating as a user

Create a controller to redirect and handle the access 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 when authenticating as a user. 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();

Authenticating as a bot

Discord allows you to add “bots” to guilds (servers). This is a modified OAuth flow, where you are redirected to Discord to confirm the guild you wish to add a bot to. There is no redirect back to your application when you authorize the request.

You can authenticate as a bot by using the bot method before redirecting:

<?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')->bot()->redirect();
    }
}

If you know the guild ID you wish to add your bot to, you may specify it with the guild method:

return Socialite::driver('discord')
    ->bot()
    ->guild($guildId)
    ->redirect();

Additionally, you can disable the guild select:

return Socialite::driver('discord')
    ->bot()
    ->guild($guildId)
    ->disableGuildSelect()
    ->redirect();

Note: if you try and disable guild selection without specifying a guild, the package will throw a GuildRequiredException instance.

Issues

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

socialite-discord-provider's People

Contributors

lorenzosapora avatar martinbean avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

socialite-discord-provider's Issues

Discord drive always requests 'email' scope

No matter what I do the discord driver always requests the email scope. While this is not a breaking issue, it's in poor taste to ask for more information than I need.
image
image

Both of the code snippets return an user with approvedScopes: 'identify' and 'email'
image

MAJ

Hello, I was wondering if you could update the plugin for the current version of Azuriom.
Thanks!

Can't seem to get this working

I have Google OAuth working perfectly, however I get a 401 Unauthorized when I'm trying to fetch the Discord user from the callback.

Route::get('/auth/discord', function () {
    return Socialite::driver('discord')->redirect();
});

Route::get('/auth/discord/cb', function () {
    try {
        $user = Socialite::driver('discord')->user();
        dd($user);
        return;
    } catch (Exception $e) {
        dd($e);
        return Redirect::to('/auth/discord');
    }
});

Here's the error message -

    Client error: `POST https://discord.com/api/oauth2/token` resulted in a `401 Unauthorized` response:
    {"error": "invalid_client"}

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.