Downloads
Stars
Version
This module is a skeleton to handle with users in your API with things generally required. It's has endpoints to register, auth (default and facebook), password resets and confirmation email.
The module was made to works with Laravel Modules package. A great package to organize your Laravel projects structure in modules
instead keep your files into your app
folder.
/password/reset
to show link request form/password/email
to send reset link email/password/reset/{token}
to show reset form/password/reset
to reset passwordPOST
: /v1/users - Create usersPOST
: /v1/oauth/token - Default login and Facebook LoginGET
: /v1/users/{id} - Get one userPOST
: /v1/password/email - Sends password reset emailsPOST
: /v1/password/reset - Resets PasswordsGET
: /v1/account/verify/{token} - Confirm emailMore details on Swagger Docs
Illuminate\Auth\Events\Registered
when user is registeredIlluminate\Auth\Events\PasswordReset
when resets password$ composer require smartins/user-module
The module must be in Modules\User
folder of your project
IMPORTANT: Note that the migrations create_users_table
and create_password_resets_table
already exists in your project by default. To module works correctly delete the default migrations to create users
and password_resets_table
and use the migrations from User module. You can see the tables structure on Modules\User\Database\Migrations
$ php artisan module:publish-migration User
$ php artisan migrate
The migrations to create users
, password_resets
and Laravel Passport migrations will be executed. Congratulations! You have the database structure to Register, Confirm Account, Login (OAuth2 - Default and Facebook) and Resets Password of your Users!
The next step is configure the Laravel Passport
passport:install
command to the encryption keys needed to generate secure access tokens. Copy the "password grant" client which will be used to generate access tokens:$ php artisan passport:install
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 Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
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();
}
}
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
After make the Laravel Passport configuration your Laravel Project must to be ready to authenticate users!
To use too facebook authentication you must make more some configurations.
config/services.php
configuration file, and should use the key facebook, twitter, linkedin, google, github or bitbucket, depending on the providers your application requires. To use facebook place the following code:'facebook' => [
'client_id' => env('FACEBOOK_ID'),
'client_secret' => env('FACEBOOK_SECRET'),
'redirect' => env('FACEBOOK_REDIRECT'),
],
.env
file:FACEBOOK_ID=YourFacebookId
FACEBOOK_SECRET=YourFacebookSecret
FACEBOOK_REDIRECT=YourFacebookRedirectUrl
Well, it's all the required configurations to add support to social login to your API!
To add more social providers you just need update the class Modules\User\Services\SocialUserResolver.php
setting your another providers. More details on Passport Social Grant package.
To resets password and email confirmation works correctly you need configure the sending email
and queue
of your Laravel API.
Don't worry, it's pretty easy!
.env
file set your email configurations. You can use mailtrap on development.MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
OBS: More instructions to configure the sending email in your laravel application on Laravel Docs
Queue
For me, the easy way to configure Queues
on Laravel is using the database
driver.
Basically you just need prepare your database and... it's ready!
database
table to hold the jobs. To generate a migration that creates this table, run the queue:table
Artisan command. Once the migration has been created, you may migrate your database using the migrate command:$ php artisan queue:table
$ php artisan migrate
queue:work
Artisan command:$ php artisan queue:work
You can see a skeleton project with User module configured. Know the Laravel Robust!