chrisidakwo / laravel-auth-identifiers
Laravel custom authentication with an option to use a custom password validation implementation
Installs: 1 788
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
- illuminate/auth: ^5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
This package is auto-updated.
Last update: 2024-04-24 14:45:44 UTC
README
Laravel Auth Identifier is a small library that allows you to use custom authentication identifiers such as: email, password, phone_number or pin
and a password to authenticate users in your application. It also allows you to implement a custom password validator using a class or closure function.
Installation
composer require chrisidakwo/laravel-auth-identifiers
After installation, add the service provider to the providers array in theconfig/app.php
file. Ensure it's below App\Providers\AuthServiceProvider::class
to avoid Laravel's default AuthServiceProvider
overriding this library's implementation.
ChrisIdakwo\Auth\Providers\CustomAuthServiceProvider::class
Laravel 5.5 uses package auto-discovery, so doesn't require you to manually add the ServiceProvider.
Config
Publish config file only when you need to use a custom password validator
php artisan vendor:publish --tag=custom-auth
User Model
Update the User
model to use the ChrisIdakwo\Auth\Traits\CustomAuthUser
trait.
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; use ChrisIdakwo\Auth\CustomAuthUser; class User extends Authenticatable { use Notifiable, CustomAuthUser; /** * Get the name of the unique identifier for the user. * * You can list as many items as possible in the array, or just one item. * * @return array */ public function getAuthIdentifiersName(): array { return ['email', 'username', 'phone_number', 'pin']; } }
With this you can authenticate a user against either of an email, username, phone number, or a pin code.
Auth Driver
Update the providers driver for users in the config/auth.php
file like below:
<?php return [ // ... 'providers' => [ 'users' => [ 'driver' => 'custom-auth', 'model' => App\Models\Auth\User::class, ], ], // ... ];
Usage
<?php use Illuminate\Support\Facades\Auth; $data = ['identifier' => 'johndoe@desevens.com', 'password' => 'foobar']; if (Auth::attempt($data)) { // Continue with whatever you want to do }