GithubHelp home page GithubHelp logo

stuyk / altv-discord-auth Goto Github PK

View Code? Open in Web Editor NEW
32.0 4.0 13.0 191 KB

alt:V Discord Authentication with optional role whitelisting.

Home Page: https://stuyk.com/

License: GNU General Public License v3.0

JavaScript 62.61% HTML 15.55% Less 21.84%
altv discord fivem ragemp discord-developers discord-bot discord-authentication whitelist role-whitelist hacktoberfest

altv-discord-auth's Introduction

alt:V - Discord Authentication with Optional Whitelist

❤️ Become a Sponsor of my Open Source Work

⌨️ Learn how to script for alt:V

⭐ This repository if you found it useful!

Summary

If you ever wanted to go with a passwordless login or authentication method. This is allows you to utilize Discord to help with user logins by utilize the oAuth2 Service provided by Discord.

This was meant to be as intuitive as possible but also as secure as we can make it. Which means we utilize an express server to help mitigate fake client-side data.

This resource requires opening port 7790.

This resource requires a Discord Developer Application. (Free)

If you plan on using the whitelist it requires a Discord Bot. (Free)

This also comes with an optional whitelist where you can give players a role in order to get into the server. The whitelist automatically updates when you add or remove a role from the user. It also automatically re-parses the whitelist every 60 seconds.

Looking for a traditional login with Username and Password? Try out alt:V OS Auth.

Prerequisites

I cannot stress this enough. Ensure you have NodeJS 13+ or you will have problems.

  • NodeJS 13+
  • An Existing or New Gamemode
  • General Scripting Knowledge

After simply add the name of this resource to your server.cfg resource section. It must be named exactly like this.

altv-discord-auth

Then simply clone this repository into your main server resources folder.

cd resources
git clone https://github.com/Stuyk/altv-discord-auth

After cloning, navigate to resources/altv-discord-auth directory and simply run npm install to install needed dependencies.

Port Forward

Open Port 7790 for your express server.

Generating Your Bot

Create a Discord Bot at https://discord.com/developers/

Click here to see a .gif of the process.

Add oAuth2 Redirect

You will need to add a specific file path to the oAuth2 bot. This setting can be found in the oAuth2 category of the discord developers application.

Append the Following for Redirects

http://127.0.0.1:7790/authenticate

http://<your_server_ip>:7790/authenticate

Make sure to save!

Inviting Your Bot

You simply visit the oAuth2 section of the discord developers application. Tick bot in scopes. Then tick administrator in bot permissions (optional just make sure your bot can see all users). After you copy the link and paste it in your browser.

Updating Environment Variables

MAKE SURE TO READ ALL OF THIS. IT'S VERY IMPORTANT.

Open your .env file which should be located in the same directory as your altv-server file. Running this resource without the .env will generate it automatically. If you are not sure what a .env file is please use google.

ENABLE_WHITELIST=false
CLIENT_ID=<client_id>
CLIENT_SECRET=<client_secret>
BOT_SECRET=
SERVER_ID=
ROLE_WHITELIST_ID=
REDIRECT_IP=127.0.0.1

Anything marked with * is OPTIONAL.

*=Optional

ENABLE_WHITELIST

This will automatically default to false. Meaning it will not turn on the whitelist.

Set this to true to enable the whitelist.

ENABLE_WHITELIST=true
ENABLE_WHITELIST=false

CLIENT_ID

This can be found under the general information of the discord developers app. Copy the ID that is in plain text.

CLIENT_SECRET

This can be found under the general information of the discord developers app. Reveal or copy the secret.

REDIRECT_IP

Running Locally?

If you are running your server locally and just for yourself. Use localhost.

ie. 127.0.0.1

Running in Production?

If you are running your server for players to join use the IP for your machine.

Ensure you port forward 7790 in your router, firewall, etc.

BOT_SECRET *

This can be found under the bot section of the discord developers app. Copy the secret for your bot.

SERVER_ID *

This can be found by enabling developer mode for Discord.

Discord App -> Settings -> Appearance -> Scroll Down to Advanced

Toggle ON Developer Mode

Right-click your server on the left-side and Copy ID

ROLE_WHITELIST_ID *

This can be found by enabling developer mode for Discord.

Discord App -> Settings -> Appearance -> Scroll Down to Advanced

Toggle ON Developer Mode

Go to your Server Roles for your Server.

Create a role called whatever you want. I used whitelist

Right-Click the role and select Copy ID

Showing the Authentication Window

When a player first joins your server you will want to emit this event. We do not do it for you.

Server-Side

alt.on('playerConnect', playerConnect);

function playerConnect(player) {
    alt.emit('discord:BeginAuth', player);
}

After Authentication

The users general Discord Information will be sent to you through an alt event. You will also recieve the player which it belongs to. The window will automatically close for that user.

Server-Side

alt.on('discord:AuthDone', playerAuthDone);

function playerAuthDone(player, discordInfo) {
    console.log(discordInfo);
}

altv-discord-auth's People

Contributors

durtyfree avatar leonmrbonnie avatar stuyk avatar svnoak 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

altv-discord-auth's Issues

Whitelist doesn't fetch current whitelisted users on startup

As of Discord.js v12, there was something that messed up how bots interact with cache.
The below fix makes the function refreshWhitelist() assynchronous and adds await server.members.fetch();, which updates the cache that the bot reads.
Here's a quick fix to bot.js:

async function refreshWhitelist() { alt.log(Refreshing Whitelist`);

whitelist = [];

const server = discordClient.guilds.cache.get(`${config.serverId}`);
if (!server) {
    console.error(`Did you forget to invite the bot to your server?`);
    return;
}

await server.members.fetch();

const members = server.roles.cache.get(config.roleWhitelistId).members.array();

for (let i = 0; i < members.length; i++) {
    const member = members[i];
    if (!member) {
        continue;
    }

    if (!member.user) {
        continue;
    }

    whitelist.push(member.user.id);
}

if (members.length <= 0) {
    alt.log(`No members are whitelisted at this time.`);
    return;
}

alt.log(`Refreshed Whitelist. Whitelisted Members: ${members.length}`);

}
`

Don't rely that strong on .env

When I run your auth in a (let's say) Docker container, I can set my env inside my DOCKERFILE. So there is no need to check if a .env File exists. So please remove the hardcoded error. (better check if env vars has been set)

Not Pulling Whitelist from Guild

Hola, currently using your Auth to learn the ropes, but for some reason it seems like it just doesn't work. git cloned the Repository and added my credentials to the .env

any known bug? will check out the latest changes anyway to check if the update broke something.

Your tutorials are my savior ngl
screenlog-1609068404.log
server.log

issues with login

Hello, I have 2 issues.

  1. Wrong example emit event
  2. Nothing happends after the button click

1.

Its not discord:BeginAuth its discord:Auth.

2.

When I click on the button to log in, nothing happens.
In app.js, the value "this.url" in the function "beginAuth" is null. According to this, nothing happens anymore. Tested with the current alt:V release.

error at loading

@Stuyk#0864 i have a little problem with your resource "discord-auth", if i run the altv-server.exe i get this error. Yes i have read the readme.md
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.