search
HomePHP FrameworkLaravelNew password confirmation process for user login in Laravel 6.2 (code example)

New password confirmation process for user login in Laravel 6.2 (code example)

Laravel has released v6.2, which adds a new password confirmation feature that allows you to require logged in users to re-enter their password before they can access routes.

This function is similar to the GitHub confirmation dialog box when you perform sensitive operations. You can easily set this up in Laravel, so let's try out the new feature so you can better understand how it works:

SETUP

First, in order to understand this new feature more intuitively, we create a new Laravel application:

laravel new confirm-app
cd confirm-app
composer require laravel/ui --dev

As you know, the make:auth command was removed in Laravel 6, and the same functionality has been migrated to laravel/ui. In the official expansion package. Let us use a new command to generate user authentication related code:

 php artisan ui vue --auth
 yarn install
 yarn dev

Next, we configure the SQLite database (of course you can choose the database you want to use):

touch database/database.sqlite

We have created the default configuration file required by Laravel when using the sqlite driver, but you still need to update the .env file to ensure the database connection and path are correct:

DB_CONNECTION=sqlite
# ...
# 使用 sqlite 驱动程序的默认路径
# DB_DATABASE=laravel

Next, let’s run the migration and then create a test user:

php artisan migrate

We can create it in the console through the factory() method A test user:

 php artisan tinker
 >>> $user = factory(App\User::class)->create([
 ... 'password' => bcrypt('secret'),
 ... 'email' => 'admin@example.com'
 ... ]);

Write the controller.

Assume you want the user to view administrative actions such as adding an SSH key before Re-verify their password. We want users to re-enter their passwords within a configured window (default is three hours).

We will create a fake /settings/ssh/create route, in which we need the new password.confirm middleware before the user can create a new key:

php artisan make:controller Settings/SSHController

Next, create the method create() in this controller:

 namespace App\Http\Controllers\Settings;
  
  use App\Http\Controllers\Controller;
  use Illuminate\Http\Request;
  
  class SSHController extends Controller
  {
  public function create()
  {
 return view('secret');
 }
 }

We will stub the secret template and place it at the root of the view path In the directory resources/views/secret.blade.php:

  @extends('layouts.app')
  @section('content')
  <div class="container">
  <div class="row justify-content-center">
  <div class="col-md-8">
  <h1 id="Add-nbsp-a-nbsp-New-nbsp-SSH-nbsp-Key">Add a New SSH Key</h1>
  <p>This page is only shown after password confirmation.</p>
  </div>
  </div>
 </div>
 @endsection

When coding, you should copy the file auth/passwords/confirm.blade.php to your project . You can get the file to copy here: ui/confirm.stub. Copy this file and add it to your project in the following path:

resources/views/auth/passwords/confirm.blade.php

Next, we need to define the routes, in routes/ At the end of the web.php file I mention the need for this middleware:

 Route::namespace(&#39;Settings&#39;)
 ->middleware([&#39;auth&#39;])
 ->group(function () {
 Route::get(&#39;/settings/ssh/create&#39;, &#39;SSHController@create&#39;)->middleware(&#39;password.confirm&#39;);
 });

Note: Typically, you can aggregate all routes that require authentication through the auth middleware. In this demo , we created a controller in the Settings namespace.

With it, once you log in, you will be redirected to /home. There, navigate to /settings/ssh/create and you will be prompted for your password:

New password confirmation process for user login in Laravel 6.2 (code example)

If you follow this tutorial, enter secret, submit the form, and then enter the create view. Once you confirm your password, you can refresh this page without prompting.

Use the new ddd() helper function, add it to your SSHController::create() method, the method will determine the value of session in auth.password_confirmed_at the next time you are prompted:

public function create()
 {
 ddd(session(&#39;auth&#39;));
 return view(&#39;secret&#39;);
 }

New password confirmation process for user login in Laravel 6.2 (code example)

This is the last time the password was verified. By default, users will not be reminded to verify their password again within 3 hours. Of course, you can customize it by modifying the config('auth.password_timeout') configuration item (the configuration item is defined in config/auth. of Laravel v6.2.0. php configuration file).

For more technical articles related to the laravel framework, please visit the laravel tutorial column!

The above is the detailed content of New password confirmation process for user login in Laravel 6.2 (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:cnblogs. If there is any infringement, please contact admin@php.cn delete
Laravel's Impact: Simplifying Web DevelopmentLaravel's Impact: Simplifying Web DevelopmentApr 21, 2025 am 12:18 AM

Laravel stands out by simplifying the web development process and delivering powerful features. Its advantages include: 1) concise syntax and powerful ORM system, 2) efficient routing and authentication system, 3) rich third-party library support, allowing developers to focus on writing elegant code and improve development efficiency.

Laravel: Frontend or Backend? Clarifying the Framework's RoleLaravel: Frontend or Backend? Clarifying the Framework's RoleApr 21, 2025 am 12:17 AM

Laravelispredominantlyabackendframework,designedforserver-sidelogic,databasemanagement,andAPIdevelopment,thoughitalsosupportsfrontenddevelopmentwithBladetemplates.

Laravel vs. Python: Exploring Performance and ScalabilityLaravel vs. Python: Exploring Performance and ScalabilityApr 21, 2025 am 12:16 AM

Laravel and Python have their own advantages and disadvantages in terms of performance and scalability. Laravel improves performance through asynchronous processing and queueing systems, but due to PHP limitations, there may be bottlenecks when high concurrency is present; Python performs well with the asynchronous framework and a powerful library ecosystem, but is affected by GIL in a multi-threaded environment.

Laravel vs. Python (with Frameworks): A Comparative AnalysisLaravel vs. Python (with Frameworks): A Comparative AnalysisApr 21, 2025 am 12:15 AM

Laravel is suitable for projects that teams are familiar with PHP and require rich features, while Python frameworks depend on project requirements. 1.Laravel provides elegant syntax and rich features, suitable for projects that require rapid development and flexibility. 2. Django is suitable for complex applications because of its "battery inclusion" concept. 3.Flask is suitable for fast prototypes and small projects, providing great flexibility.

Frontend with Laravel: Exploring the PossibilitiesFrontend with Laravel: Exploring the PossibilitiesApr 20, 2025 am 12:19 AM

Laravel can be used for front-end development. 1) Use the Blade template engine to generate HTML. 2) Integrate Vite to manage front-end resources. 3) Build SPA, PWA or static website. 4) Combine routing, middleware and EloquentORM to create a complete web application.

PHP and Laravel: Building Server-Side ApplicationsPHP and Laravel: Building Server-Side ApplicationsApr 20, 2025 am 12:17 AM

PHP and Laravel can be used to build efficient server-side applications. 1.PHP is an open source scripting language suitable for web development. 2.Laravel provides routing, controller, EloquentORM, Blade template engine and other functions to simplify development. 3. Improve application performance and security through caching, code optimization and security measures. 4. Test and deployment strategies to ensure stable operation of applications.

Laravel vs. Python: The Learning Curves and Ease of UseLaravel vs. Python: The Learning Curves and Ease of UseApr 20, 2025 am 12:17 AM

Laravel and Python have their own advantages and disadvantages in terms of learning curve and ease of use. Laravel is suitable for rapid development of web applications. The learning curve is relatively flat, but it takes time to master advanced functions. Python's grammar is concise and the learning curve is flat, but dynamic type systems need to be cautious.

Laravel's Strengths: Backend DevelopmentLaravel's Strengths: Backend DevelopmentApr 20, 2025 am 12:16 AM

Laravel's advantages in back-end development include: 1) elegant syntax and EloquentORM simplify the development process; 2) rich ecosystem and active community support; 3) improved development efficiency and code quality. Laravel's design allows developers to develop more efficiently and improve code quality through its powerful features and tools.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.