baka/auth

Baka 用户认证组件

v0.2.5.5 2019-11-29 19:51 UTC

README

MC Auth Library,用于避免为应用程序重新实现用户注册流程。

测试

codecept run

JWT

将 JWT 添加到您的配置中

'jwt' => [
    'secretKey' => getenv('JWT_SECURITY_HASH'),
    'expirationTime' => '1 hour', #strtotime
    'payload' => [
        'exp' => 1440,
        'iss' => 'phalcon-jwt-auth',
    ],
    'ignoreUri' => [
        'regex:auth',
        'regex:webhook',
        'regex:/users',
    ],
],

数据库

我们使用 phinx 进行迁移,要更新此项目的数据库结构,只需运行

vendor/bin/phinx-migrations generate

要从您的项目运行此迁移,请将数据库位置的路径添加到您的 phinx.php 路径中

<?php

return [
    'paths' => [
        'migrations' => [
            getenv('PHINX_CONFIG_DIR') . '/db/migrations',
            '/home/baka/auth/db/migrations',
        ],

vendor/bin/phinx migrate -e (环境:开发 | 生产)

使用

将此添加到您的 service.php

/**
* UserData dependency injection for the system
*
* @return Session
*/
$di->set('userData', function () use ($config, $auth) {
    $data = $auth->data();

    $session = new Baka\Auth\Models\Sessions();
    $request = new Phalcon\Http\Request();

    if (!empty($data) && !empty($data['sessionId'])) {
        //user
        if (!$user = Baka\Auth\Models\Users::getByEmail($data['email'])) {
            throw new Exception('User not found');
        }

        return $session->check($user, $data['sessionId'], $request->getClientAddress(), 1);
    } else {
        throw new Exception('User not found');
    }
});

生成迁移文件

$ phalcon migration --action=run --migrations=migrations --config=</path/to/config.php>

将迁移导入项目

$ phalcon migration --action=run --migrations=vendor/baka/auth/migrations/

路由器

$router->post('/auth', [
    '\Your\NameSpace\AuthController',
    'login',
]);

$router->post('/auth/signup', [
    '\Your\NameSpace\AuthController',
    'signup',
]);

$router->post('/auth/logout', [
    '\Your\NameSpace\AuthController',
    'logout',
]);

# get email for new password
$router->post('/auth/recover', [
    '\Your\NameSpace\AuthController',
    'recover',
]);

# update new password
$router->put('/auth/{key}/reset', [
    '\Your\NameSpace\AuthController',
    'reset',
]);

# active the account
$router->put('/auth/{key}/activate', [
    '\Your\NameSpace\AuthController',
    'activate',
]);

社交登录

"hybridauth/hybridauth": "dev-3.0.0-Remake",
<?php
'social_config' => [
    // required
    "callback" => getenv('SOCIAL_CONNECT_URL'),
    // required
    "providers" => [
        "Facebook" => [
            "enabled" => true,
            "callback" => getenv('SOCIAL_CONNECT_URL').'/Facebook',
            "keys" => ["id" => getenv('FB_ID'), "secret" => getenv('FB_SECRET')], //production
        ]
    ],
],

并配置链接和回调链接(SOCIAL_CONNECT_URL)到 http://site.com/users/social/{site} 示例: http://site.com/users/social/Facebook

您需要将其添加到注册过程中以识别社交登录

{% if socialConnect %}
    <input type="hidden" name="socialConnect" value="1">
{% endif %}