Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

1.0 KB
Raw Blame
<?php

namespace App\Traits;

use App\Actions\User\RevokeUserTeamTokens;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

trait DeletesUserSessions
{
    /**
     * Delete all sessions for this user.
     * This will force the user to log in again on all devices.
     */
    public function deleteAllSessions(): void
    {
        // Invalidate the current session only when it belongs to this user, not to an admin who changes it
        if (Auth::id() === $this->id) {
            Session::invalidate();
            Session::regenerateToken();
        }
        DB::table('sessions')->where('user_id', $this->id)->delete();
        RevokeUserTeamTokens::forUser($this->id);
    }

    /**
     * Boot the trait.
     */
    protected static function bootDeletesUserSessions()
    {
        static::updated(function ($user) {
            // Check if password was changed
            if ($user->wasChanged('password')) {
                $user->deleteAllSessions();
            }
        });
    }
}