Skip to content

[NFR]: Phalcon Authentication #16273

@sinbadxiii

Description

@sinbadxiii

Hi Guys!

I want to add Auth to Phalcon. I made this library - Phalcon Auth.
It works well.

The basis is built on guards and adapters.
As guards, there can be Sessions for the web and Tokens for the api.

Adapters can be application Models , such as App\Models\Users, Stream such as json file, or just an array of users in Memory.

Because There are no usual middleware in the Phalcon, I implemented an Access layer, which allows you to separate user access into guests and users (also you can create custom access, for example for admins).

To start authentication, you need to create a service provider auth, for example, standard sessions and users model.

use Phalcon\Auth\Manager;
use App\Models\User;
use Phalcon\Auth\Adapter\Model;
use Phalcon\Auth\Guard\Session;


$di->setShared("auth", function () use ($config, $di) {
    $manager = new Manager();
    
    $configAdapter = [
        'model' => User::class,
    ];
    
    $adapter = new Model($this->getSecurity(), $configAdapter);
    $guard   = new Session(
        $adapter,
        $this->getSession(),
        $this->getCookies(),
        $this->getRequest(),
        $this->getEventsManager()
    );
    
    $manager->addGuard("web", $guard, true);
    
    return $manager;
});

And in the controller restrict access to only authenticated users

<?php

declare(strict_types=1);

namespace App\Controllers;

class ProfileController extends Phalcon\Mvc\Controller
{
    public function onConstruct()
    {
        $this->auth->access("auth");
    }

    public function indexAction()
    {
    }
}

There is support for HTTP Basic Auth,
and you can also connect JWT token authentication using a third-party library Phalcon Auth JWT.

If everyone likes it, I can create a PR :)

Metadata

Metadata

Assignees

Labels

new feature requestPlanned Feature or New Feature Request

Type

No type
No fields configured for issues without a type.

Projects

Status
Implemented

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions