署名されたルートと通知によるユーザー起動
22889 ワード
我々は簡単に我々のアプリにユーザー活性化機能を実装するために私たちを支援しているパッケージの多くを見てきました.
まあLaravel内部だけで簡単な方法があります.
ララベル通知 ララベル署名URL ミドルウェアの使い方
注:私はあなたが
まず最初に、クリーンなアプリケーションが必要です.
したら、プロジェクトを設定したら、我々は直接実際の魔法に“”にジャンプすることができます!
しかしまず第一に、アプリケーションに基本的なauthを追加する必要があります.
まあこれは簡単です.我々は、ララベル通知と署名URLを使用して、ユーザーにアカウントを確認させる簡単な方法を提供しようとしています
ユーザーマイグレーションを設定してブールを設定します.この行を追加することで、ユーザーが検証されたかどうか確認できます.
次に、新しい通知が必要です.
次に何をしたいのかは、作成された通知クラスを使用して、ユーザーがクリックする必要があるリンクをユーザーに送信します.
したがって、URLを作成できます.
URLが指定された時間だけ有効であると言うこともできます.
このためにURLからURLを変更する必要があります
次に、我々は我々を修正する必要があります
単に追加
コントローラは以下のようになります.
コマンドで一つを作成します.
オープン
我々の通知では、私たちは彼のメールアドレスを確認するためのユーザーのURLを追加しました.
リンクを使用するには、このルートをWebルートファイルに追加する必要があります.
このルートは、ユーザーをアクティブにし、彼は、彼はアプリケーションを使用することができない知っている.
私は本当にあなたがLaravelから直接簡単なヘルパーとユーザーの活性化について、この短いチュートリアルを気に入ったことを願っています.
もしそうならば、この記事を共有して、愛を広げてください❤️
そしてもちろん、あなたは私に従ってください
この記事のソースは利用可能ですGitHub 😺
まあLaravel内部だけで簡単な方法があります.
この記事では何をカバーするつもりですか
始めましょう!
注:私はあなたが
Laravel/installer
グローバルにあなたのマシンにインストールされます.まず最初に、クリーンなアプリケーションが必要です.
laravel new my-project-name
今すぐあなたの選択のIDEで新しいプロジェクトを開くことができます..したら、プロジェクトを設定したら、我々は直接実際の魔法に“”にジャンプすることができます!
しかしまず第一に、アプリケーションに基本的なauthを追加する必要があります.
php artisan make:auth
待って..何を達成するつもりですか?
まあこれは簡単です.我々は、ララベル通知と署名URLを使用して、ユーザーにアカウントを確認させる簡単な方法を提供しようとしています
我々がここで何をしているかについて、これ以上繰り返されません!
ユーザーマイグレーションを設定してブールを設定します.この行を追加することで、ユーザーが検証されたかどうか確認できます.
$table->boolean('is_activated')->default(false);
その後、ユーザのマイグレーションは次のようになります.<?php
use 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('is_activated')->default(false);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
このようなユーザモデルを設定する必要があります....
protected $fillable = ['name', 'email', 'password', 'is_activated'];
...
protected $casts = ['is_activated' => 'boolean'];
...
ユーザモデルは次のようになります.<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'is_activated'
];
protected $casts = [
'is_activated' => 'boolean'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
今、我々は更新することができますis_activated
直接フィールド.次に、新しい通知が必要です.
php artisan make:notification Users/ActivateEmailNotification
我々の通知に動きましょう..次に何をしたいのかは、作成された通知クラスを使用して、ユーザーがクリックする必要があるリンクをユーザーに送信します.
したがって、URLを作成できます.
<?php
namespace App\Notifications\Users;
use App\User;
use Illuminate\Support\Facades\URL;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class ActivateEmailNotification extends Notification
{
use Queueable;
/**
* User Object
*
* @var App\User
*/
public $user;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* 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::signedRoute('activate-email', ['user' => $this->user->id]);
return (new MailMessage)
->subject('Activate your email address')
->line('In order to use the application, please verify your email address.')
->action('Activate your account', $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 [
//
];
}
}
私たちはLaravel Signed URL Feature ユーザーのURLを作成するには.これは、私たちは、電子メールが送信されたユーザーだけが彼の電子メールアドレスを確認する機能を確認することができます.URLが指定された時間だけ有効であると言うこともできます.
このためにURLからURLを変更する必要があります
$url = URL::signedRoute('activate-email', ['user' => $this->user->id]);
To$url = URL::temporarySignedRoute('activate-email', now()->addMinutes(30), ['user' => $this->user->id]);
今すぐURLはわずか30分の作成後に有効になります.次に、我々は我々を修正する必要があります
Auth\RegisterController
.単に追加
'is_active' => false,
にcreate
コントローラの下部にあるメソッド.同じ方法でユーザに通知する.コントローラは以下のようになります.
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'is_activated' => false,
'password' => Hash::make($data['password']),
]);
$user->notify(new \App\Notifications\Users\ActivateEmailNotification($user));
return $user; }
}
また、ユーザーが活性化されたかどうかを決定するミドルウェアも必要です.コマンドで一つを作成します.
php artisan make:middleware CheckUserActivated
このようなミドルウェアを修正します.<?php
namespace App\Http\Middleware;
use Closure;
class CheckUserActivated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!$request->user()->is_activated) {
return redirect('/not-enabled'); // You can add a route of your choice
}
return $next($request);
}
}
今、新しいミドルウェアをカーネルに登録する必要があります.オープン
App\Http\Kernel.php
そしてこれをrouteMiddleware
クラスの一番下に'active' => \App\Http\Middleware\CheckUserActivated::class,
このファイルに別のクラスを追加する必要があります.'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
今我々はユーザーがアクティブかどうかを判断することができます.私は彼がアクティブではない、私たちは別のページには、ユーザーのためのメモを追加することができます彼をリダイレクトしたい場合は、彼はまだ私たちの電子メールのリンクを使用して自分のアカウントをアクティブにする必要があります.我々の通知では、私たちは彼のメールアドレスを確認するためのユーザーのURLを追加しました.
リンクを使用するには、このルートをWebルートファイルに追加する必要があります.
Route::get('/activate-email/{user}', function (Request $request) {
if (!$request->hasValidSignature()) {
abort(401, 'This link is not valid.');
}
$request->user()->update([
'is_activated' => true
]);
return 'Your account is now activated!';
})->name('activate-email');
あなたが好きならば、あなたは別々のコントローラにこのメソッドを置くことができます.でもここでシンプルにしたいです.このルートは、ユーザーをアクティブにし、彼は、彼はアプリケーションを使用することができない知っている.
結論
私は本当にあなたがLaravelから直接簡単なヘルパーとユーザーの活性化について、この短いチュートリアルを気に入ったことを願っています.
もしそうならば、この記事を共有して、愛を広げてください❤️
そしてもちろん、あなたは私に従ってください
この記事のソースは利用可能ですGitHub 😺
Reference
この問題について(署名されたルートと通知によるユーザー起動), 我々は、より多くの情報をここで見つけました https://dev.to/fwartner/laravel-56---user-activation-with-signed-routes--notifications-oaaテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol