silasjoisten/sonata-oauth2-login-bundle

在 Sonata 项目中通过电子邮件验证登录 Google

6.1.0 2023-06-15 19:38 UTC

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

安装

composer require silasjoisten/sonata-oauth2-login-bundle

app/AppKernel.php 中注册 Bundle

 class AppKernel extends Kernel
 {
     public function registerBundles()
     {
        // ..
        new Http\HttplugBundle\HttplugBundle(),
        new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),
        new SilasJoisten\Sonata\Oauth2LoginBundle\SonataOauth2LoginBundle(),
        // ..
     }

    // ..
}

配置

包含路由

SonataOauth2LoginBundle:
    resource: "@SonataOauth2LoginBundle/Resources/config/routing.xml"

配置 Bundle

sonata_oauth2_login:
    valid_email_domains: ["@your-domain.com"]
    default_user_roles: ["ROLE_SONATA_ADMIN"]
    #optional value
    custom_emails: 
        test@example.com: ["ROLE_SUPER_ADMIN"]
        test2@example.de: ["ROLE_SONATA_ADMIN"]

您可以根据需要添加任意多的有效域名

配置 HWIOauthBundle

# app/config/security.yml

security:
   providers:
      hwi:
         id: sonata_oauth2_login.user.provider

         #...

   firewalls:
      YOUR_FIREWALL:
         #pattern: /admin(.*) REMOVE THIS LINE IF YOU ARE USING SONATA ADMIN
         oauth:
            resource_owners:
               google:         "/login/check-google"
            login_path:        /admin/login                 # For Sonata Admin
            use_forward:       false
            default_target_path: /admin/dashboard           # For Sonata Admin
            failure_path:      /admin/login                 # For Sonata Admin
            oauth_user_provider:
               service:  sonata_oauth2_login.user.provider
# app/config/config.yml

hwi_oauth:
    firewall_names: [YOUR_FIREWALL]
    resource_owners:
        google:
            type:                "google"
            client_id:           "YOUR_CLIENT_ID"
            client_secret:       "YOUR_CLIENT_SECRET"
            scope:               "email profile"
            options:
                csrf: true
                access_type:     offline

使用方法

要使用 OAuth2 登录,您只需调用 Twig 函数,在登录模板中渲染按钮,如下所示

{{ render_login_button() }}

可选:您可以在其中传递一个 数组 来设置自定义的 classvalue

外观和感觉

Look and Feel