https://www.5balloons.info/user-email-verification-and-account-activation-in-laravel-5-5/
email send ko process first maa garni ... then i.t setup email sending variable in .env
web.php
email send ko process first maa garni ... then i.t setup email sending variable in .env
web.php
Route::get('/user/verify/{token}', 'Auth\RegisterController@verifyUser');
verifyuser.php model
protected $guarded = [];
public function user() { return $this->belongsTo('App\User', 'user_id'); }
User.php
public function verifyUser()
{
return $this->hasOne('App\VerifyUser');
}
create migration for verify_users table:
Schema::create('verify_users', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->string('token'); $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade'); $table->timestamps();});
Also modify the migration file for the users table to include a new boolean field to maintain the status of account verification.
$table->boolean('verified')->default(false);
VerifyMail.php inside of Mail folder (created using php artisan make:mail VerifyMail )
<?php namespace App\Mail; use Illuminate\Bus\Queueable;use Illuminate\Mail\Mailable;use Illuminate\Queue\SerializesModels;use Illuminate\Contracts\Queue\ShouldQueue; class VerifyMail extends Mailable{ use Queueable, SerializesModels;public $user;/** * Create a new message instance. * * @return void */ public function __construct($user) { $this->user = $user; } /** * Build the message. * * @return $this */public function build(){ return $this->view('email.verifyUser'); }}
Now In Registration Controller
public function verifyUser($token) { $verifyUser = VerifyUser::where('token', $token)->first(); if(isset($verifyUser) ){ $user = $verifyUser->user; if(!$user->verified) { $verifyUser->user->verified = 1; $verifyUser->user->save(); $status = "Your e-mail is verified. You can now login."; }else{ $status = "Your e-mail is already verified. You can now login."; } }else{ return redirect('/login')->with('warning', "Sorry your email cannot be identified."); } return redirect('/login')->with('status', $status); }
also create function of registration controller
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$verifyUser = VerifyUser::create([
'user_id' => $user->id,
'token' => str_random(40)
]);
Mail::to($user->email)->send(new VerifyMail($user));
return $user;
}
add this also
public function authenticated($request, $user)
{
if (!$user->verified) {
auth()->logout();
return back()->with('warning', 'You need to confirm your account. We have sent you an activation code, please check your email.');
}
return redirect()->intended($this->redirectPath());
}
email/VerifyUser.blade.php
<!DOCTYPE html> <html> <head> <title>Welcome Email</title> </head> <body> <h2>Welcome to the site {{$user['name']}}</h2> <br/> Your registered email-id is {{$user['email']}} , Please click on the below link to verify your email account <br/> <a href="{{url('user/verify', $user->verifyUser->token)}}">Verify Email</a> </body> </html>
finallly add this code to login controller :
public function authenticated($request, $user) { if (!$user->verified) { auth()->logout(); return back()->with('warning', 'You need to confirm your account. We have sent you an activation code, please check your email.'); } return redirect()->intended($this->redirectPath()); }
Add following in login blade
@if (session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif @if (session('warning')) <div class="alert alert-warning"> {{ session('warning') }} </div> @endif
Comments
Post a Comment