xin/auth

应用程序为其用户提供了一种通过应用程序进行身份验证和登录的方法

v1.0.5 2022-10-23 04:46 UTC

This package is auto-updated.

Last update: 2024-04-23 07:59:49 UTC


README

介绍

应用程序为其用户提供了一种通过应用程序进行身份验证和登录的方法

安装教程

composer require xin/auth

使用说明

配置文件

<?php

return [

	/*
	|--------------------------------------------------------------------------
	| Authentication Defaults
	|--------------------------------------------------------------------------
	|
	| This option controls the default authentication "guard" and password
	| reset options for your application. You may change these defaults
	| as required, but they're a perfect start for most applications.
	|
	*/

	'defaults' => [
		'guard' => 'user',
	],

	/*
	|--------------------------------------------------------------------------
	| Authentication Guards
	|--------------------------------------------------------------------------
	|
	| Next, you may define every authentication guard for your application.
	| Of course, a great default configuration has been defined for you
	| here which uses session storage and the Eloquent user provider.
	|
	| All authentication drivers have a user provider. This defines how the
	| users are actually retrieved out of your database or other storage
	| mechanisms used by this application to persist your user's data.
	|
	| Supported: "session", "token", "token_session"
	|
	*/

	'guards' => [
		'api' => [
			'driver' => 'token_session',
			'provider' => 'user',
		],
		'user' => [
			'driver' => 'session',
			'provider' => 'user',
			'auth_url' => '/index/login/login',
		],
		'admin' => [
			'driver' => 'session',
			'provider' => 'admin',
			'administrator_id' => 1,
			'auth_url' => '/admin/login/login',
		],
	],

	/*
	|--------------------------------------------------------------------------
	| User Providers
	|--------------------------------------------------------------------------
	|
	| All authentication drivers have a user provider. This defines how the
	| users are actually retrieved out of your database or other storage
	| mechanisms used by this application to persist your user's data.
	|
	| If you have multiple user tables or models you may configure multiple
	| sources which represent each model / table. These sources may then
	| be assigned to any extra authentication guards you have defined.
	|
	| Supported: "database", "model"
	|
	*/

	'providers' => [
		'user' => [
			'driver' => 'model',
			'model' => \app\common\model\User::class,
		],

		'admin' => [
			'driver' => 'model',
			'model' => \app\admin\model\Admin::class,
		],
	],

];

实例化认证器

<?php
$authManager = new \Xin\Auth\AuthManager($config);
$authManager->loginUsingId(1);
$authManager->login(User::find(1));
$authManager->loginUsingCredential(['username'=>'admin']);

$authManager->check();
$authManager->guest();

$authManager->getUser();
$authManager->getUserId();
$authManager->user();

$authManager->isAdministrator();

$authManager->temporaryUser();

$authManager->extend('api',function ($config){
 return new class() implements \Xin\Auth\Auth\Guard{
 
 }
});

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md