<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class SignupActivate extends Notification {
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct() {
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable) {
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable) {
$url = url('/api/auth/signup/activate/' . $notifiable->activation_token);
return (new MailMessage)
->subject('Confirm your account')
->line('Thanks for signup! Please before you begin, you must confirm your account.')
->action('Confirm Account', url($url))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable) {
return [
//
];
}
}
C:\xampp\htdocs\blog\routes\api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::group([
'prefix' => 'auth'
], function () {
Route::post('login', 'AuthController@login');
Route::post('signup', 'AuthController@signup');
Route::get('signup/activate/{token}', 'AuthController@signupActivate');
Route::group([
'middleware' => 'auth:api'
], function() {
Route::get('logout', 'AuthController@logout');
Route::get('user', 'AuthController@user');
});
});
C:\xampp\htdocs\blog\app\User.php
<?php
namespace App;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable {
use Notifiable, HasApiTokens, SoftDeletes;
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'active', 'activation_token',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token', 'activation_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
API Rest with Laravel 5.6 Passport Authentication — Confirm account + notifications (Part 2)
Step 1. Add columns in users table
In first step, we add two columns active and activation_token also we add thesoftDeletes trait in database/migrations/xxxx_create_users_table.php migration file.
<?phpuse Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('active')->default(false);
$table->string('activation_token');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}
...
Next, we add SoftDeletes trait, fillable and hidden attributes in your App\User model.
<?php
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes;class User extends Authenticatable
{
use Notifiable, HasApiTokens, SoftDeletes; protected $dates = ['deleted_at']; protected $fillable = [
'name', 'email', 'password', 'active', 'activation_token'
]; protected $hidden = [
'password', 'remember_token', 'activation_token'
];}
After, open your terminal or command prompt and run bellow command:
php artisan migrate:refresh
Step 2. Create confirm account notification
In your terminal or command prompt run bellow command:
php artisan make:notification SignupActivate
This command will create app/Notifications/SignupActivate.php file, in this file determines on which channels the notification will be delivered. In our case we use mail
public function via($notifiable)
{
return ['mail'];
}
After that we make our email notification.
public function toMail($notifiable)
{
$url = url('/api/auth/signup/activate/'.$notifiable->activation_token); return (new MailMessage)
->subject('Confirm your account')
->line('Thanks for signup! Please before you begin, you must confirm your account.')
->action('Confirm Account', url($url))
->line('Thank you for using our application!');
}
Step 3: Create and send token to confirm account
We have to update app/Http/Controllers/AuthController.php controller and update signup api method. So let’s update AuthController and put bellow code:
<?php...
use App\Notifications\SignupActivate;
class AuthController extends Controller
{
... public function signup(Request $request)
{
$request->validate([
'name' => 'required|string',
'email' => 'required|string|email|unique:users',
'password' => 'required|string|confirmed'
]); $user = new User([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
'activation_token' => str_random(60)
]); $user->save(); $user->notify(new SignupActivate($user)); return response()->json([
'message' => 'Successfully created user!'
], 201);
}
}
When create new account will receive a email with the link to activate account. The next step we create the route and method to activate account.
Step 4. Add Activation Account Route
We will add new route signup/activate/{token} inroutes/api.php file. So, let’s add new route on that file.
We will learn to create deactivated accounts and then confirm and activate by email, in the authentication system with Laravel API Authentication Passport.This tutorial is continuation of the Create API Rest with Laravel 5.6 Passport Authentication (Part 1) tutorial.