refactor(auth): move discord handlers to own file

This commit is contained in:
Miguel Nogueira 2022-08-22 17:06:25 +01:00
parent 1f08ea12c0
commit bcdc4b83e6
No known key found for this signature in database
GPG Key ID: 3C6A7E29AF26D370
4 changed files with 78 additions and 11 deletions

View File

@ -0,0 +1,64 @@
<?php
/*
* Copyright © 2020 Miguel Nogueira
*
* This file is part of Raspberry Staff Manager.
*
* Raspberry Staff Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Raspberry Staff Manager is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Raspberry Staff Manager. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Str;
use Laravel\Socialite\Facades\Socialite;
class DiscordController extends Controller
{
public function discordRedirect() {
return Socialite::driver('discord')
->scopes(['email', 'guilds.join', 'guilds.members.read', 'guilds'])
->redirect();
}
public function discordCallback() {
$discordUser = Socialite::driver('discord')->user();
$appUser = User::upsert([
'uuid' => Str::uuid(),
'name' => $discordUser->getName(),
'email' => $discordUser->getEmail(),
'email_verified_at' => now(), // verify the account since it came from a trusted provider
'username' => $discordUser->getNickname(),
'currentIp' => \request()->ip(),
'discord_user_id' => $discordUser->getId(),
'discord_token' => Crypt::encryptString($discordUser->token),
'discord_refresh_token' => Crypt::encryptString($discordUser->refreshToken)
], [
'email' => $discordUser->getEmail()
]);
Auth::login(User::where('email', $discordUser->getEmail())->first());
return redirect()
->route('dashboard');
}
}

View File

@ -26,8 +26,11 @@ use App\Services\AccountSuspensionService;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use App\Facades\IP;
use Illuminate\Support\Str;
use Laravel\Socialite\Facades\Socialite;
class LoginController extends Controller
@ -108,13 +111,5 @@ class LoginController extends Controller
}
}
public function discordRedirect() {
return Socialite::driver('discord')
->scopes(['email', 'guilds.join', 'guilds.members.read', 'guilds'])
->redirect();
}
public function discordCallback() {
}
}

View File

View File

@ -14,8 +14,13 @@ return new class extends Migration
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('discord_token')->after('remember_token');
$table->string('discord_refresh_token')->after('discord_token');
$table->string('password')->nullable()->change(); // social login requires no pw (still required upon normal reg)
$table->string('registrationIp')->nullable()->change();
$table->unsignedBigInteger('discord_user_id')->after('remember_token')->nullable();
$table->longText('discord_token')->after('discord_user_id')->nullable();
$table->longText('discord_refresh_token')->after('discord_token')->nullable();
});
}
@ -27,7 +32,10 @@ return new class extends Migration
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('discord_token', 'discord_refresh_token');
$table->dropColumn('discord_token', 'discord_refresh_token', 'discord_user_id');
$table->string('password')->nullable(false)->change();
$table->string('registrationIp')->nullable(false)->change();
});
}
};