Skip to content

Commit 5059ed6

Browse files
Merge pull request #119 from JakyeRU/feature-soft-delete
Add soft-delete error
2 parents d64e235 + dd1a293 commit 5059ed6

5 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/Http/Controllers/DiscordController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Http\RedirectResponse;
77
use App\Providers\RouteServiceProvider;
88
use Illuminate\Support\Facades\DB;
9+
use Illuminate\Support\Facades\Schema;
910
use Jakyeru\Larascord\Http\Requests\StoreUserRequest;
1011
use Jakyeru\Larascord\Services\DiscordService;
1112

@@ -80,6 +81,14 @@ public function handle(StoreUserRequest $request): RedirectResponse | JsonRespon
8081
return $this->throwError('database_error', $e);
8182
}
8283

84+
// Verifying if the user is soft-deleted.
85+
if (Schema::hasColumn('users', 'deleted_at')) {
86+
if ($user->trashed()) {
87+
DB::rollBack();
88+
return $this->throwError('user_deleted');
89+
}
90+
}
91+
8392
// Verifying if the user has the required roles if "larascord.roles" is set.
8493
if (count(config('larascord.guild_roles'))) {
8594
// Verifying if the "guilds" and "guilds.members.read" scopes are set.

src/Services/DiscordService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Exception;
77
use Illuminate\Http\Client\RequestException;
88
use Illuminate\Support\Facades\Http;
9+
use Illuminate\Support\Facades\Schema;
910
use Jakyeru\Larascord\Types\AccessToken;
1011
use Jakyeru\Larascord\Types\GuildMember;
1112

@@ -187,6 +188,15 @@ public function createOrUpdateUser(\Jakyeru\Larascord\Types\User $user): User
187188
throw new Exception('User access token is missing.');
188189
}
189190

191+
if (Schema::hasColumn('users', 'deleted_at')) {
192+
return User::withTrashed()->updateOrCreate(
193+
[
194+
'id' => $user->id,
195+
],
196+
$user->toArray(),
197+
);
198+
}
199+
190200
return User::updateOrCreate(
191201
[
192202
'id' => $user->id,

src/config/config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@
225225
'message' => 'An error occurred while trying to revoke your access token.',
226226
'redirect' => '/'
227227
],
228+
'user_deleted' => [
229+
'message' => 'Your account is deleted and you can\'t log in.',
230+
'redirect' => '/'
231+
],
228232
],
229233

230234
/*

src/database/migrations/2023_04_06_101123_add_roles_to_users_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public function up(): void
1313
{
1414
Schema::table('users', function (Blueprint $table) {
15-
$table->json('roles')->nullable();
15+
$table->json('roles')->nullable()->after('avatar');
1616
});
1717
}
1818

src/routes/larascord.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
. '&redirect_uri=' . config('larascord.redirect_uri')
2222
. '&response_type=code&scope=' . implode('%20', explode('&', config('larascord.scopes')))
2323
. '&prompt=' . config('larascord.prompt', 'none'))
24+
->middleware(['web', 'guest'])
2425
->name('login');
2526

2627
Route::get('/confirm-password', [ConfirmablePasswordController::class, 'show'])

0 commit comments

Comments
 (0)