PHP 8.2.30
Preview: LoginLinkController.php Size: 3.66 KB
/home/byroehnu/cnggold.com.ng/app/Http/Controllers/LoginLinkController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Spatie\LoginLink\Exceptions\DidNotFindUserToLogIn;
use Spatie\LoginLink\Exceptions\InvalidUserClass;
use Spatie\LoginLink\Exceptions\NotAllowedInCurrentEnvironment;
use Spatie\LoginLink\Http\Requests\LoginLinkRequest;

class LoginLinkController extends Controller
{
    public function __invoke(LoginLinkRequest $request)
    {
        $this->ensureAllowedEnvironment();

        $authenticatable = $this->getAuthenticatable($request);

        $this->performLogin($request->guard, $authenticatable);

        $redirectUrl = $this->getRedirectUrl($request);

        return Inertia::location($redirectUrl);
    }

    protected function ensureAllowedEnvironment(): void
    {
        $allowedEnvironments = config('login-link.allowed_environments');

        if (! app()->environment($allowedEnvironments)) {
            throw NotAllowedInCurrentEnvironment::make($allowedEnvironments);
        }
    }

    protected function getAuthenticatable(LoginLinkRequest $request): Authenticatable
    {
        $attributes = $this->getUserAttributes($request);

        $authenticatableClass = $this->getAuthenticatableClass($request->guard);

        $user = $authenticatableClass::query()
            ->when(count($attributes), fn (Builder $query) => $query->where($attributes))
            ->first();

        if ($user) {
            return $user;
        }

        if (! config('login-link.automatically_create_missing_users')) {
            throw DidNotFindUserToLogIn::make();
        }

        return $this->createUser($authenticatableClass, $attributes);
    }

    protected function performLogin(?string $guard, Authenticatable $authenticatable): void
    {
        auth($guard)->login($authenticatable);
    }

    protected function getUserAttributes(LoginLinkRequest $request): array
    {
        $attributes = $request->userAttributes();

        if ($identifier = $this->getAuthenticatableIdentifier($request)) {
            $attributes = array_merge([
                $identifier['attribute'] => $identifier['value'],
            ], $attributes);
        }

        return $attributes;
    }

    protected function getAuthenticatableIdentifier(LoginLinkRequest $request): ?array
    {
        if ($request->key) {
            $userClass = new ($this->getAuthenticatableClass($request->guard));

            return [
                'attribute' => ($userClass)->getKeyName(),
                'value' => $request->key,
            ];
        }

        if ($request->email) {
            return [
                'attribute' => 'email',
                'value' => $request->email,
            ];
        }

        return null;
    }

    protected function getAuthenticatableClass(?string $guard): string
    {
        $provider = $guard === null
            ? config('auth.guards.web.provider')
            : config("auth.guards.{$guard}.provider");

        return config('login-link.user_model')
            ?? config("auth.providers.{$provider}.model")
            ?? throw InvalidUserClass::notFound();
    }

    protected function createUser(string $authenticatableClass, array $attributes): Authenticatable
    {
        return $authenticatableClass::factory()->create($attributes);
    }

    protected function getRedirectUrl(LoginLinkRequest $request): string
    {
        if ($request->redirect_url) {
            return $request->redirect_url;
        }

        if ($routeName = config('login-link.redirect_route_name')) {
            return route($routeName);
        }

        return redirect()->intended()->getTargetUrl();
    }
}

Directory Contents

Dirs: 4 × Files: 6

Name Size Perms Modified Actions
Admin DIR
- drwxr-xr-x 2025-07-19 07:29:54
Edit Download
Auth DIR
- drwxr-xr-x 2025-05-08 11:48:26
Edit Download
Debug DIR
- drwxr-xr-x 2025-04-08 04:12:56
Edit Download
Frontend DIR
- drwxr-xr-x 2025-07-19 07:29:54
Edit Download
336 B lrw-r--r-- 2025-04-19 04:22:18
Edit Download
3.48 KB lrw-r--r-- 2025-07-19 11:19:37
Edit Download
8.61 KB lrw-r--r-- 2025-07-19 11:26:12
Edit Download
3.66 KB lrw-r--r-- 2025-05-04 10:26:05
Edit Download
1.48 KB lrw-r--r-- 2025-04-08 04:12:56
Edit Download
3.97 KB lrw-r--r-- 2025-07-19 12:08:07
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).