用于 Yii 2 的 AuthClient 扩展--安装

安装扩展

要安装该扩展,请使用 Composer。运行

composer require --prefer-dist yiisoft/yii2-authclient "~2.1.0"

或在你的 composer.json 文件的“require”一节添加以下代码:

"yiisoft/yii2-authclient": "~2.1.0"

配置应用程序

该扩展安装后,你需要设置验证客户端集合应用程序组件:

return [
    'components' => [
        'authClientCollection' => [
            'class' => 'yii\authclient\Collection',
            'clients' => [
                'google' => [
                    'class' => 'yii\authclient\clients\Google',
                    'clientId' => 'google_client_id',
                    'clientSecret' => 'google_client_secret',
                ],
                'facebook' => [
                    'class' => 'yii\authclient\clients\Facebook',
                    'clientId' => 'facebook_client_id',
                    'clientSecret' => 'facebook_client_secret',
                ],
                // etc.
            ],
        ]
        // ...
    ],
    // ...
];

保存授权数据

为了识别由外部服务验证的用户,我们需要保存首次验证时获得的 ID,以及用于接下来验证时检查该 ID。 并不建议将登录选项限于只能用外部登录,自身却不提供一种供用户登录的方法。 最好的做法是既提供原始的账号\密码登录方式,也提供外部验证方式。

如果我们要将用户信息存入数据库,则数据库模式可以是:

class m??????_??????_auth extends \yii\db\Migration
{
    public function up()
    {
        $this->createTable('user', [
            'id' => $this->primaryKey(),
            'username' => $this->string()->notNull(),
            'auth_key' => $this->string()->notNull(),
            'password_hash' => $this->string()->notNull(),
            'password_reset_token' => $this->string()->notNull(),
            'email' => $this->string()->notNull(),
            'status' => $this->smallInteger()->notNull()->defaultValue(10),
            'created_at' => $this->integer()->notNull(),
            'updated_at' => $this->integer()->notNull(),
        ]);

        $this->createTable('auth', [
            'id' => $this->primaryKey(),
            'user_id' => $this->integer()->notNull(),
            'source' => $this->string()->notNull(),
            'source_id' => $this->string()->notNull(),
        ]);

        $this->addForeignKey('fk-auth-user_id-user-id', 'auth', 'user_id', 'user', 'id', 'CASCADE', 'CASCADE');
    }

    public function down()
    {
        $this->dropTable('auth');
        $this->dropTable('user');
    }
}

上述 SQL 中 user 表在高级项目模板中用于保存用户信息。 每个用户可以由多重外部服务验证,因此每个 user 记录可以关联多个 auth 记录。 在 auth 表中 source 是验证提供商的名称 source_id 是外部服务在该用户成功登录后提供的唯一 ID。

使用上述创建的数据表后我们可以生成 Auth 模型。无须进一步调整。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值