almefy/authentication-bundle

Almefy Authentication Bundle 帮助连接支持 Almefy Authenticator 的 symfony 应用程序。

0.2 2023-03-16 21:25 UTC

This package is auto-updated.

Last update: 2024-09-20 16:38:57 UTC


README

使用 Almefy PHP 客户端集成的 Symfony Authentication Bundle。

快速指南

以下是如何在您的 Symfony 项目中集成 Almefy Authentication Bundle 的快速指南,以便轻松测试。我们正在准备全面的文档,涵盖所有可能的用例和参数。

安装

先决条件

此包包含多个必需的配置选项,这些选项通过环境值公开。

# Example .env Data
# Almefy Entity credentials and api url
ALMEFY_KEY=...
ALMEFY_SECRET=...
ALMEFY_API=...

# Local route to redirect and local route to authenticate
LOGIN_REDIRECT_URL=/sessions
LOGIN_AUTHENTICATE_URL=/almefy/authenticate

配置路由

# config/routes.yaml
almefy_authenticator:
    resource: '@AuthenticationBundle/config/routes.yaml'
    prefix: /almefy

更新 security.yaml

security:
    providers:
        almefy_user_identity_provider:
            id: Almefy\AuthenticationBundle\Security\AlmefyUserIdentityProvider
    firewalls:
        main:
            lazy: true
            provider: almefy_user_identity_provider
            entry_point: Almefy\AuthenticationBundle\Security\AuthenticationEntryPoint
            custom_authenticator:
                - Almefy\AuthenticationBundle\Security\AlmefyAuthenticator
            logout:
                path: app_logout

更新登录页面 twig 以添加 Almefy Widget

{{ include('@!Authentication/almefy_widget.html.twig') }}

登录控制器必须将变量传递给托管 almefy_widget 的 twig 模板

    public function __construct(private Client $client, private AlmefySessionManager $almefySessionManager)
    {
    }
# ...
    #[Route(path: '/login', name: 'app_login')]
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        $error = $authenticationUtils->getLastAuthenticationError();
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('security/login.html.twig', [
            'last_username' => $lastUsername,
            'error' => $error,
            'almefyApi' => $this->client->getApi(), 
            'almefyKey' => $this->client->getKey()
        ]);
    }      

或者您可以使用服务来绑定它们,以自动将它们公开给 twig。

Almefy Authentication Bundle 作为 almefy/authentication-bundle 包在 Packagist 上提供。在终端中,从项目的根目录运行 composer require almefy/authentication-bundle,然后完成。目前支持的最低 PHP 版本为 8.0。

客户端使用

此包使用 almefy/client 作为核心组件与 Almefy API 通信。请确保检查其文档以更好地了解一切是如何工作的。

许可

Almefy PHP SDK 根据 Apache License,版本 2.0 许可。