Skip to main content

Email Verification in laravel

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

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

Popular posts from this blog

generate database dynamically taking user input as table name field name

public function create_table ( $table_name , $table_field , $field_type ) { // dd($field_type); $servername = "localhost" ; $username = "root" ; $password = "" ; $dbname = "db_fepb" ; // Create connection $conn = mysqli_connect ( $servername , $username , $password , $dbname ); // Check connection if (! $conn ) { die ( "Connection failed: " . mysqli_connect_error ()); } // sql to create table $sql = " CREATE TABLE $table_name (id INT ( 6 ) UNSIGNED AUTO_INCREMENT PRIMARY KEY , " ; for ( $i = 0 ; $i < count ( $table_field ); $i ++) { $sql = $sql . $table_field [ $i ]. ' ' . $field_type [ $i ]. ', ' ; } $sql = $sql . 'created_at TIMESTAMP' ; $sql = $sql . ')' ; // dd($sql); ...

Convert Html PAge to PDF using Canvas

Link :https://www.quora.com/How-we-export-html-page-as-pdf-using-pure-javascript app.js ( function (){ var form = $ ( '. printme ' ), cache_width = form . width (), a4 =[ 595.28 , 841.89 ]; // for a4 size paper width and height $ ( '#create_pdf' ). on ( 'click' , function (){ $ ( 'body' ). scrollTop ( 0 ); createPDF (); }); //create pdf function createPDF (){ getCanvas (). then ( function (canvas){ var img = canvas. toDataURL ( "image/png" ), doc = new jsPDF ({ unit : 'px' , format : 'a4' }); doc . addImage ( img , 'JPEG' , 20 , 20 ); doc . save ( 'FinalPrint.pdf' ); form . width ( cache_width ); }); } // create canvas object function getCanvas (){ form . width (( a4 [ 0 ]* 1.33333 ) ...

key press function to perform system function

document.onkeyup = function(e) {   if (e.which == 77) {     alert("M key was pressed");   } else if (e.ctrlKey && e.which == 66) {     alert("Ctrl + B shortcut combination was pressed");   } else if (e.ctrlKey && e.altKey && e.which == 89) {     alert("Ctrl + Alt + Y shortcut combination was pressed");   } else if (e.ctrlKey && e.altKey && e.shiftKey && e.which == 85) {     alert("Ctrl + Alt + Shift + U shortcut combination was pressed");   } }; < script > $ ( document ). ready ( function () { $ ( '[data-toggle="tooltip"]' ). tooltip (); $ ( document ). keyup ( function ( e ) { var name = e . key ; // console.log(e.which); // if(e.shiftKey && e.which == 80) // { // } // alert(name); switch ( e . key ){ ...