承接 midorikocak/nanoauth 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

midorikocak/nanoauth

Composer 安装命令:

composer require midorikocak/nanoauth

包简介

Nano Auth is a library that allows you to create authentication for your apps.

README 文档

README

Nano Auth

Nano Auth

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Nano Auth is a library that allows you to create authentication for your apps.

Requirements

Strictly requires PHP 7.4.

Install

Via Composer

$ composer require midorikocak/nanoauth

Usage

Authentication

The Authentication class has 4 public methods that you can use:

<?php

declare(strict_types=1);

namespace midorikocak\nanoauth;

interface AuthenticationInterface
{
    public function login(string $username, string $password): bool;

    public function logout(): void;

    public function isLogged(): bool;

    public function getLoggedUser();
}

To use it you should supply with a user repository.

$db = new Database(new PDO('sqlite::memory:'));
$userRepository = new UserRepository($db);
$auth = new Authentication($userRepository);

User implements UserInterface

A user object that we can authenticate should implemennt UserInterface.

<?php

declare(strict_types=1);

namespace midorikocak\nanoauth;

interface UserInterface
{
    public function __construct(?string $id, string $username, string $email, string $password);

    public function getPassword(): string;

    public function getUsername(): string;

    public function getEmail(): string;

    public function getId(): ?string;

    public function setId(string $id): void;
}

UserRepository

To interact with user data, we need to supply user repository to our authentication object. If you want to use your own implementation of user repository, you can implement your own UserInnterface. Here repository constructor expects a NanoDB object.

$userRepository = new UserRepository($db);
$auth = new Authentication($userRepository);

Authorization

To add authentication and authorization capabilities, you can use AuthorizationTrait in your App classes.

Let's say we created our app in this way:

<?php

declare(strict_types=1);

use midorikocak\nanodb\Database;
use midorikocak\nanoauth\UserRepository;
use midorikocak\nanoauth\Authentication;

$db = new Database(new PDO('sqlite::memory:'));

$userRepository = new UserRepository($db);

$auth = new Authentication($userRepository);
$entryRepository = new Journal($db);
$app = new App($entryRepository);

$this->app->setAuthentication($this->auth);

To check login we should trigger checkLogin() method in our public methods.

<?php 

declare(strict_types=1);

use midorikocak\nanodb\Database;
use midorikocak\nanoauth\UserRepository;
use midorikocak\nanoauth\Authentication;
use midorikocak\nanoauth\AuthorizationTrait;

 class App
{
    use AuthorizationTrait;

    private Journal $journal;

    public function __construct(Journal $journal)
    {
        $this->journal = $journal;
    }

    public function addEntry(string $content)
    {
        $this->checkLogin();

        $entry = new Entry($content);
        $this->journal->save($entry);
    }
}

Motivation

Mostly educational purposes.

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email mtkocak@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固