PHPackages                             narakode/fineauth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. narakode/fineauth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

narakode/fineauth
=================

A simple authentication package for Laravel REST APIs.

v1.1.0(1mo ago)04↓71.4%PHPPHP ^8.3

Since Jul 10Pushed 1mo agoCompare

[ Source](https://github.com/narakode/fineauth)[ Packagist](https://packagist.org/packages/narakode/fineauth)[ RSS](/packages/narakode-fineauth/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (3)Dependencies (15)Versions (8)Used By (0)

Fine Auth
=========

[](#fine-auth)

A simple authentication package for Laravel REST APIs.

Features
--------

[](#features)

- Login
- Access Tokens
- Refresh Tokens
- Current Authenticated User

Requirements
------------

[](#requirements)

- PHP ^8.3
- Laravel ^13
- Laravel Sanctum ^4.3

Installation
------------

[](#installation)

Install the package:

```
composer require narakode/fineauth
```

Publish the package configuration:

```
php artisan vendor:publish --provider="Narakode\FineAuth\FineAuthServiceProvider"
```

This package requires Laravel Sanctum. If you haven't installed it yet, run:

```
php artisan install:api
```

Add the `HasApiTokens` and `HasRefreshTokens` traits to your `User` model.

```
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;
use Narakode\FineAuth\HasRefreshTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasRefreshTokens;
}
```

Usage
-----

[](#usage)

Register the authentication routes in your routes file (for example, `routes/api.php`):

```
use Narakode\FineAuth\FineAuth;

FineAuth::routes();

# Custom attributes

FineAuth::routes([
    'as' => 'test.',
    'prefix' => 'test/'
]);
```

You can verify that the routes have been registered by listing your application's routes:

```
php artisan route:list

# Example:
# POST  api/login
# POST  api/refresh-token
# GET  api/me
```

Endpoints
---------

[](#endpoints)

### Login

[](#login)

The `POST /login` endpoint requires the following request parameters:

- `email`
- `password`

The package uses Laravel's authentication system to validate the provided credentials.

If the credentials are invalid, it returns a **401 Unauthorized** response:

```
{
    "message": "The provided credentials do not match our records."
}
```

If authentication succeeds, it returns a **200 OK** response:

```
{
    "access_token": "xxxx",
    "user": {
        "id": "xxx",
        "name": "xxx",
        "email": "xxx"
    },
    "meta": {}
}
```

A refresh token is also returned as an HTTP cookie named `refresh_token` with the following attributes:

- Expires in 1 hour
- `HttpOnly`
- `Secure`
- `SameSite`

### Current User

[](#current-user)

The `GET /me` endpoint returns the authenticated user associated with the provided access token.

Provide the access token in the `Authorization` header using the following format:

```
Authorization: Bearer

```

If the access token is valid, the response will be:

```
{
    "user": {
        "id": "xxx",
        "name": "xxx",
        "email": "xxx"
    },
    "meta": {}
}
```

If the access token is missing or invalid, the endpoint returns a `401 Unauthorized` response.

### Refresh Token

[](#refresh-token)

The `POST /refresh-token` endpoint returns a new access token and the authenticated user using the refresh token stored in a cookie.

If the cookie is exists and has not expired, the response is the same as login. Otherwise, the endpoint returns `401 Unauthorized`.

Customization
-------------

[](#customization)

### Custom Credentials

[](#custom-credentials)

By default, this package uses the `email` and `password` fields as the authentication credentials.

You can customize these credentials by creating a class that implements the `Narakode\FineAuth\Auth\AuthCredentials` interface.

The class should define a `rules` method that returns an array containing the credential fields and their validation rules.

For example, create `App\Auth\AuthCredentials.php`.

```
