项目作者: setiawanhu

项目描述 :
Laravel package for generating auth controller using laravel/sanctum
高级语言: PHP
项目地址: git://github.com/setiawanhu/sanctum-auth.git
创建时间: 2020-05-19T13:15:31Z
项目社区:https://github.com/setiawanhu/sanctum-auth

开源协议:MIT License

下载


Laravel sanctum-auth Package

Latest Stable Version Total Downloads License

Laravel package for generating user authentication feature using Laravel Sanctum

For information & usage about Laravel Sanctum, please refer to Laravel Sanctum documentation.

Get Started

Include the Sanctum Auth package by calling this composer command in project root:

  1. composer require setiawanhu/sanctum-auth

Note: This package is only supporting Laravel 9

Usage

We may publish the Sanctum Auth migration files using vendor:publish artisan command:

  1. php artisan vendor:publish --provider="Hu\Auth\SanctumAuthServiceProvider"

This command will copy the user role’s migration file to /database/migrations folder and will publish the laravel/sanctum configuration and migration files.

Then, run Sanctum Auth artisan command by running:

  1. php artisan sanctum-auth:controller [{type} --force]

This command will generate the auth routing out of the box.

Note:

  1. The type argument is optional with default value are token. The controller will be generated based on given argument value. Available types are token and spa.

  2. The --force option is optional. We may use it for ignoring existing controller.

Then, make User model to extend AuthModel class:

  1. use Hu\Auth\AuthModel;
  2. class User extends AuthModel {
  3. //...
  4. }

Using Roles

if the User has roles, we may use the HasRole trait to the User model:

  1. use Hu\Auth\HasRole;
  2. class User extends AuthModel {
  3. use HasRole;
  4. //...
  5. }

Then, we add a new field to the users table:

  1. //...
  2. Schema::create('users', function (Blueprint $table) {
  3. //...
  4. $table->unsignedBigInteger('role_id')
  5. //...
  6. }
  7. //...

Then, run the database migration command:

  1. php artisan migrate

Dig Deeper

Please check wiki for more details about sanctum-auth usage