Package Data | |
---|---|
Maintainer Username: | crynobone |
Maintainer Contact: | crynobone@gmail.com (Mior Muhammad Zaki) |
Package Create Date: | 2016-11-23 |
Package Last Update: | 2023-10-15 |
Home Page: | https://packagist.org/packages/laravie/authen |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-10-31 15:15:46 |
Package Statistics | |
---|---|
Total Downloads: | 58,050 |
Monthly Downloads: | 43 |
Daily Downloads: | 1 |
Total Stars: | 64 |
Total Watchers: | 2 |
Total Forks: | 2 |
Total Open Issues: | 0 |
Imagine you need to login a user with either "email", "username" or "phone number" just like how Facebook allows it. This is not possible with Laravel since you're limited to only one unique username/identifier key. This package attempt to solve the issue by allowing to use a unified key "identifier" and you can customize which attributes Laravel should check during authentication.
To install through composer, simply put the following in your composer.json
file:
{
"require": {
"laravie/authen": "^2.0"
}
}
And then run composer install
from the terminal.
Above installation can also be simplify by using the following command:
composer require "laravie/authen=^2.0"
First you can attach the auth provider on App\Providers\AuthServiceProvider
:
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravie\Authen\BootAuthenProvider;
class AuthServiceProvider extends ServiceProvider
{
use BootAuthenProvider;
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
$this->bootAuthenProvider();
}
}
Secondly, you need to update the related App\User
(or the eloquent model mapped for auth).
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravie\Authen\AuthenUser;
class User extends Authenticatable
{
use Notifiable, AuthenUser;
/**
* Get the name of the unique identifier for the user.
*
* @return array
*/
public function getAuthIdentifiersName()
{
return ['email', 'username', 'phone_number'];
}
}
With this setup, you can now check either email
, username
or phone_number
during authentication.
Lastly, you need to update the config config/auth.php
:
<?php
return [
// ...
'providers' => [
'users' => [
'driver' => 'authen',
'model' => App\User::class,
],
],
// ...
];
Here's an example how to login.
<?php
use Illuminate\Support\Facades\Auth;
$data = ['identifier' => 'crynobone@gmail.com', 'password' => 'foobar'];
if (Auth::attempt($data)) {
// you can logged in, you can also pass your phone number of username to `identifier`.
}