chidi / laravel-passport-auth

这是一个为使用passport的laravel应用程序提供的认证包。

dev-master 2019-09-24 17:44 UTC

This package is auto-updated.

Last update: 2024-09-25 04:54:12 UTC


README

这是一个为laravel应用程序使用passport提供的简单认证包。

注意

在继续之前,请确保根据此处说明在您的应用程序中设置了laravel passport。

安装

使用composer 安装laravel-passport-auth。

composer require chidi/laravel-passport-auth

使用方法

步骤1. 创建一个类并扩展PassportAuth类

<?php

namespace App\Http\Controllers\Auth;

use Chidi\Laravel_Passport_Auth\PassportAuth;

class MyClassName extends PassportAuth {

}

步骤2. 定义以下三个方法

a

/**
 * This method should return the login validation array
 * @return array
 */
public function LoginValidationArray()
{

}

b.

/**
 * This method should return the registration array
 * @return array
 */
public function RegValidationArray() {

}

c.

/**
 * After validation, this method is called for model creation.
 * It must return the model instance.
 * @param Request $request
 *
 * @return mixed
 */
public function CreateUser(Request $request) {

}

步骤3. 在您的routes/api.php中发布这三个(3)路由

Route::post('login', 'Auth\MyClassName@login');
Route::post('register', 'Auth\MyClassName@register');
Route::post('logout', 'Auth\MyClassName@logout');

注意:请确保将“MyClassName”替换为步骤1中创建的类名。

示例

<?php

namespace App\Http\Controllers\Auth;


use Chidi\Laravel_Passport_Auth\PassportAuth;
use Illuminate\Http\Request;
use App\User;

class MyPassportAuth extends PassportAuth {

	 /**
	 * You must implement the login validation array
	 * @return array
	 */
	public function LoginValidationArray()
	{
		return [
			'email'    => 'required|email',
			'password' => 'required'
		];
	}

	 /**
	 * You must implement the registration validation array
	 * @return array
	 */
	public function RegValidationArray() {
		return [
			'name'             => 'required',
			'email'            => 'required|email|unique:users',
			'password'         => 'required',
			'confirm_password' => 'required|same:password',
		];
	}

	 /**
	 * After validation, this method is called for model creation.
	 * it must be implemented
	 * @param Request $request
	 *
	 * @return mixed
	 */
	public function CreateUser(Request $request) {
		return User::create([
			'name'     => $request->name,
			'email'    => $request->email,
			'password' => bcrypt($request->password)
		]);
	}

	 /**
	 * This method is called after login credentials have been verified.
	 * This is the right place to check if a user is verified or other security checks.
	 *
	 * return true if everything is fine.
	 * return a message string if something is wrong.
	 *
	 * e.g. if a user is not yet verified, the return would look like this,
	 * return 'Account not verified.'
	 * @param $user
	 *
	 * @return bool|string
	 */
	protected function shouldAuthenticate($user)
	{
		if($user->status === 1) return true;
		else return 'Account not active. Please contact the admin.';
	}
}

贡献

欢迎提交拉取请求。对于主要更改,请先打开一个问题来讨论您想进行什么更改。

请确保适当更新测试。

许可证

MIT