Laravel — Dual(2 factor) authentication using email

Route::post('login', '[email protected]');
php artisan make:controller LoginController
public function authenticate(Request $request) {        $this->validate($request, [            'email' => 'required|email',            'password' => 'required',        ]);        $email  = $request->email;        $password = $request->password;        $auth  = User::authenticate($email)->first();        if (!empty($auth) && Hash::check($password, $auth->password)) {         $auth->update(['email_token' => bin2hex(openssl_random_pseudo_bytes(4))]);            Notification::send($auth, new AuthenticateUser($auth));            return view('auth.login-verify');        }        return redirect(route('login'))                ->withInput($request->only('email', 'remember'))                ->withErrors([                    'password' => 'Invalid Credentials',        ]); }
$auth = User::authenticate($email)->first();
use App\User;
public function scopeAuthenticate($query, $email) { return $query->where('email', '=', $email); }
if (!empty($auth) && Hash::check($password, $auth->password))
use Hash; $auth->update(['email_token' => bin2hex(openssl_random_pseudo_bytes(4))]);
Notification::send($auth, new AuthenticateUser($auth));
use App\Notifications\AuthenticateUser;
php artisan make:notification AuthenticateUser
return (new MailMessage) ->line('Here is your email token') ->line('To login, enter the code in the web page where you requested it.') ->line($notifiable->email_token') ;
return view('auth.login-verify');
Route::post('/verify', '[email protected]')->name('email-verify');
public function emailVerify() { $token = request('email_token'); if (!$token) { return redirect('login')->withErrors(['email' => 'Email Verification Token not provided!']); } $user = User::where('email_token', $token)->first(); if (!$user) { return redirect('login')->withErrors(['flash-error' => 'Invalid Email Verification Token!']); } Auth::login($user); return redirect('home'); }

--

--

2Hats Logic is a technology service company provide customized web and mobile application development.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
2Hats Logic Solutions

2Hats Logic Solutions

2Hats Logic is a technology service company provide customized web and mobile application development.