d4nd3v/auth

Laravel 5 Auth 生成器

dev-master 2017-08-11 16:12 UTC

This package is not auto-updated.

Last update: 2024-09-29 00:52:17 UTC


README

使用说明

通过 Composer 安装

composer require d4nd3v/auth:dev-master

添加服务提供者

app/Providers/AppServiceProvider.php 中添加提供者

public function register()
{
    ...
	if ($this->app->environment() !== 'production') {
		$this->app->register('D4nd3v\Auth\AuthServiceProvider');
	}
}

生成 Auth

php artisan generate:auth

创建表

php artisan migrate --path=/database/migrations/auth/

这将创建 userspassword_resetsactivations 表。

如果你使用 API Auth

https://github.com/tymondesigns/jwt-auth/wiki/Installation
在头部必须设置:Accept: application/json

设置模型

前往 config/auth.php 并将 App\User:class 改为 App\Models\User::class

路由

对于 'middleware' => 'guest',在 \app\Http\Middleware\RedirectIfAuthenticated.php 中设置 return redirect(route('home'));

Web 路由

    Route::group(['middleware' => 'guest'], function () {

        Route::get('register', 'AuthController@showRegisterForm')->name('registerForm');
        Route::post('register', 'AuthController@register')->name('register');

        Route::get('login', 'AuthController@showLoginForm')->name('loginForm');
        Route::post('login', 'AuthController@authenticate')->name('login');

    });

    Route::get('account/activate/', 'AuthController@showActivateMessage')->name('activate');
    Route::get('account/activate/{token}', 'AuthController@activate');

    Route::get('account/reactivate/', 'AuthController@showResendActivationCode')->name('reactivateForm');
    Route::post('account/reactivate/', 'AuthController@resendActivationCode')->name('reactivate');

    Route::get('logout', 'AuthController@logout')->name('logout');

    Route::get('password/reset', 'AuthController@showLinkRequestForm');
    Route::post('password/email', 'AuthController@sendEmailWithResetPasswordLink');
    Route::get('password/reset/{token}', 'AuthController@showResetForm')->name('password.reset');
    Route::post('password/reset', 'AuthController@resetPassword');

    Route::group(['middleware' => 'auth'], function () {
        Route::get('password/change', 'AuthController@showChangePasswordForm')->name('showChangePasswordForm');
        Route::post('password/change', 'AuthController@changePassword')->name('changePassword');
    });

API 路由

    Route::post('login', 'AuthController@authenticate');
    Route::get('logout', 'AuthController@logout');
    Route::post('register', 'AuthController@register');
    Route::post('password/forgot', 'AuthController@sendEmailWithResetPasswordLink');
    Route::post('password/reset', 'AuthController@resetPassword');
    Route::post('activate/send', 'AuthController@resendActivationCode');
    Route::group(['middleware' => 'auth.jwt'], function () {
        Route::post('password/change', 'AuthController@changePassword');
    });

API 异常

\app\Exceptions\Handler.php

.....
public function render($request, Exception $exception)
{
    if ($exception instanceof APIException) {
        return $exception->apiExceptionResponse;
    }
.....
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        throw new ApiException("unauthenticated");
    }
.....

流程

  
> Register (/register)  
    > ActivateAccount Notification (Send Mail)  
        > Activate (From Mail) (GET /account/activate/token)   

> Forgot password? (GET /password/reset)
        > PasswordReset Notification (Send Mail)     
            > Change password form (From Mail) (GET /password/reset/token)
                > Action change password (/password/reset)



> Login Form (GET /login)
    > Login (POST /login)
        > Form Resend activation code   (GET /account/reactivate/)
            > Action Resend activation code   (POST /account/reactivate/)
    > Change Password (GET /password/change)
        > Set New Password (POST /password/change)
         
 
> Logout (GET /logout)

            

有用

Test users:
<a href="javascript:;" onclick="$('#email').val('test@test.test'); $('#password').val('xxx');">test@test.test</a>