This project demonstrates how to implement Single Sign-On (SSO) with Discord in PHP. It uses OAuth2 to authenticate users with their Discord accounts.
- PHP 7.4 or higher
- A Discord application (create one in the Discord Developer Portal)
-
Clone the repository or download the project files.
git clone https://github.com/yourusername/discord-sso-php-example.git cd discord-sso-php-example
Follow these steps to configure your Discord application for Single Sign-On (SSO) using OAuth2.
-
Go to the Discord Developer Portal.
-
Click on the New Application button in the top right corner.
-
Name your application (e.g., "SSO Example") and select your team if applicable.
-
Check the box agreeing to the Discord Developer Terms of Service and Developer Policy.
-
Click Create.
-
After creating the application, navigate to the OAuth2 section on the left sidebar.
-
Copy your Client ID and Client Secret as you'll need these for your application.
-
Scroll down to the Redirects section and add your application's redirect URL. This URL should match the one used in your code (e.g.,
http://your-domain.com/callback
).
-
Generate an OAuth2 authorization URL using the OAuth2 URL Generator at the bottom of the OAuth2 settings page.
-
Select the necessary scopes for your application (e.g.,
identify
to access the user's username, avatar, and banner). -
Copy the generated URL and navigate to it in your browser.
-
You will be prompted to authorize your application to access your Discord account.
By following these steps, your Discord OAuth2 client will be correctly set up and ready to be integrated with your application.
-
Open
index.php
and replace the following placeholders with your actual Discord application credentials:$clientId = 'YOUR_CLIENT_ID'; // Replace with your Client ID $clientSecret = 'YOUR_CLIENT_SECRET'; // Replace with your Client Secret $redirectUri = 'YOUR_REDIRECT_URI'; // Replace with your Redirect URI
-
Ensure your redirect URI is correctly configured in the Discord Developer Portal under your application's OAuth2 settings.
In the index.php file there is an example of how to use the Discord SSO class in your project.
- Initialization: The
DiscordSSO
class is instantiated with your Discord application credentials. - Login: The user clicks the "Log in with Discord" button, which redirects them to Discord for authentication.
- Authorization: After authorizing the application, the user is redirected back to the specified redirect URI with
a
code
. - Access Token: The code is exchanged for an access token, which is then used to fetch the user's Discord information.
This project is licensed under the MIT License. See the LICENSE file for details.