iamnotstatic/laravel-api-auth

A Laravel Package for easy API authentication setup with passport

v1.0.2 2020-04-10 16:44 UTC

This package is auto-updated.

Last update: 2024-04-11 01:39:03 UTC


README

GitHub issues GitHub stars GitHub license Total Downloads

Introduction

A Laravel Package for easy API authentication setup with passport

Installation

To get the latest version of Laravel Api Auth, simply require it

composer require iamnotstatic/laravel-api-auth

Configuration

You can publish the configuration file using this command:

php artisan vendor:publish --provider="Iamnotstatic\LaravelAPIAuth\LaravelAPIAuthServiceProvider"

Migrate your database after installing the package

php artisan migrate

This command will create the encryption keys needed to generate secure access tokens. In addition, the command will create "personal access" and "password grant" clients which will be used to generate access tokens

php artisan passport:install

Next, you should call the Passport::routes method within the boot method of your AuthServiceProvider. This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}

In your config/auth.php configuration file, you should set the driver option of the api authentication guard to passport. This will instruct your application to use Passport's TokenGuard when authenticating incoming API requests:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

In your config/auth.php configuration file, you should set the model option of the package model. This will provide a few helper methods to allow you to inspect the authenticated user's token and scopes:

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => Iamnotstatic\LaravelAPIAuth\Models\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

Usage

Now, we can simple test by rest client tools (Postman), So I test it and you can see below screenshots.

In this api you have to set two header as listed below:

Accept: application/json

pkg imgs

Register

register

Login

login

Logout

logout

Get User

getuser

Forgotten Password

forgottenpass

Reset Passowrd

passwordreset

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.

How can I thank you?

Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or HackerNews? Spread the word!

Don't forget to follow me on twitter!

Thanks! Abdulfatai Suleiman.

License

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