PHPackages                             stru/stru-hyperf-auth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. stru/stru-hyperf-auth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

stru/stru-hyperf-auth
=====================

集成到Hyperf中类似 Laravel 的 Auth 组件， 配合 stru/stru-hyperf-ui 使用可提供界面

v0.1.3(4y ago)19MITPHP

Since Jan 25Pushed 4y ago1 watchersCompare

[ Source](https://github.com/stru001/stru-hyperf-auth)[ Packagist](https://packagist.org/packages/stru/stru-hyperf-auth)[ RSS](/packages/stru-stru-hyperf-auth/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (5)Versions (13)Used By (0)

stru-hyperf-auth
================

[](#stru-hyperf-auth)

#### Description

[](#description)

> 类似laravel auth,实现了session,jwt登录验证，其他方式可自行实现。jwt使用了[hyperf-ext/jwt](https://github.com/hyperf-ext/jwt) 现成的方法进行了集成，在此表示感谢。

#### Installation

[](#installation)

```
composer require stru/stru-hyperf-auth

```

#### Publish

[](#publish)

```
php bin/hyperf.php vendor:publish hyperf/session
php bin/hyperf.php vendor:publish stru/stru-hyperf-auth
// jwt 新增发布
php bin/hyperf.php vendor:publish hyperf-ext/jwt

```

#### Database

[](#database)

```
php bin/hyperf.php migrate

```

#### Config

[](#config)

```
// config/authload/middlewares.php 添加session
\Hyperf\Session\Middleware\SessionMiddleware::class,
\Hyperf\Validation\Middleware\ValidationMiddleware::class,

// config/authload/exceptions.php 添加异常处理
\Stru\StruHyperfAuth\AuthExceptionHandler::class,

// 添加模型 App\Model\User.php  [数据库在上面设置中通过migrate发布]
1. 如果要实现注册在模型下添加
protected $fillable = ['name','account','email','password','mobile'];
2. 要使用验证 需实现接口 Authenticatable，（use Stru\StruHyperfAuth\Authenticatable;）
3. User模型中添加如下代码
public function getAuthIdentifierName(): string
{
    return $this->getKeyName();
}

public function getAuthIdentifier()
{
    return $this->getKey();
}

public function getAuthPassword(): string
{
    return $this->password;
}
4. 补充jwt实现后在原基础上还需要实现 HyperfExt\Jwt\Contracts\JwtSubjectInterface 并添加如下方法
  public function getJwtIdentifier()
    {
        return $this->getKeyName();
    }

    public function getJwtCustomClaims(): array
    {
        return [];
    }

```

#### Use

[](#use)

```
// 在使用登录注册视图之前要先安装 “stru/stru-hyperf-ui” 具体使用方法详见其文档，同时要取消如下注释
1. resources/views/layouts/app.blade.php
    38，45，48，58 行注释取消
2. resources/views/home.blade.php
    69，71，74 行的注释取消  (如果无需使用该模板，则无需此操作)

// 添加控制器（LoginController），在控制器编写如下代码
