Description
It would be nice if you could toggle a setting to add users to a discord server utilizing the existing oauth features, especially during registration to the website. I've managed to work this work, additionally disabled email registration, forcing everyone on the website to register via discord and join our server.
Changing /register.php
if (Session::exists('oauth_register_data')) {
$data = json_decode(Session::get('oauth_register_data'), true);
$auto_verify_oauth_email = $data['email'] === Input::get('email')
&& NamelessOAuth::getInstance()->hasVerifiedEmail($data['provider'], $data['data']);
Session::delete('oauth_register_data');
}
to
if (Session::exists('oauth_register_data')) {
$data = json_decode(Session::get('oauth_register_data'), true);
NamelessOAuth::getInstance()->saveUserProvider(
$user_id,
$data['provider'],
$data['id'],
);
if ($data['provider'] == 'discord' && isset($data['access_token'])) {
add_user_to_discord_guild($data['access_token'], $data['id'], "GUILDID", "ACCESSTOKEN");
}
$auto_verify_oauth_email = $data['email'] === Input::get('email')
&& NamelessOAuth::getInstance()->hasVerifiedEmail($data['provider'], $data['data']);
Session::delete('oauth_register_data');
}
Also need to add a function to actually make them join the server.
function add_user_to_discord_guild($userOAuthAccessToken, $userId, $guildId, $botToken) {
$url = "https://discord.com/api/v8/guilds/$guildId/members/$userId";
$headers = [
"Authorization: Bot $botToken",
"Content-Type: application/json"
];
$payload = json_encode([
'access_token' => $userOAuthAccessToken,
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] != 204) {
return false;
}
return true;
}
Changing /oauth.php
Session::put('oauth_register_data', json_encode([
'provider' => $provider_name,
'id' => $provider_id,
'email' => $oauth_user['email'],
'data' => $oauth_user
]));
to
Session::put('oauth_register_data', json_encode([
'provider' => $provider_name,
'id' => $provider_id,
'email' => $oauth_user['email'],
'data' => $oauth_user,
'access_token' => $token->getToken()
]));
Changing /NameLessOAuth.php
public function getProvidersAvailable(): array {
$providers = [];
foreach ($this->_providers as $provider_name => $provider_data) {
if (!$this->isSetup($provider_name)) {
continue;
}
$provider = $this->getProviderInstance($provider_name);
$scopes = [
$provider_data['scope_id_name'],
'email',
];
to
public function getProvidersAvailable(): array {
$providers = [];
foreach ($this->_providers as $provider_name => $provider_data) {
if (!$this->isSetup($provider_name)) {
continue;
}
$provider = $this->getProviderInstance($provider_name);
$scopes = [
$provider_data['scope_id_name'],
'email',
];
if ($provider_name == 'discord') {
$scopes[] = 'guilds.join';
}
In addition to the changes above, I've also deleted certain things in template files to remove all ways of possibly creating an account without using discord. We have been happy with it and I wanted to share it with the rest of the NamelessMC community, hopefully y'all can better integrate it with toggles and such in the panel.
Activity