Home Web Front-end JS Tutorial Node makes image verification code when logging in

Node makes image verification code when logging in

Jun 07, 2018 am 11:05 AM
node Image verification code Verification code

This time I will bring you node to create the image verification code when logging in. What are the precautions for node to create the image verification code when logging in? The following is a practical case, let's take a look.

To implement the graphical verification code here, I use the svg-captcha module in node, which can support all characters and numbers. It is supported by all platforms and is very simple to use.

1. Install

cnpm i svg-captcha --save
Copy after login

2. Import

var svgCaptcha = require('svg-captcha');
Copy after login

3. Get the verification code

3-1 Install cookie-parser, which is used to save the obtained session to cookie to facilitate front-end access verification

cnpm i cookie -parser --save

3-2 Use cookie-parser

const cookieParase = require('cookie-parser');
app.use(cookieParase());
Copy after login

so that you can use cookies in the project

3 -3 Get the verification code

// 获取验证码
 getCaptcha(req, res, next){
  var captcha = svgCaptcha.create({ 
   // 翻转颜色 
   inverse: false, 
   // 字体大小 
   fontSize: 36, 
   // 噪声线条数 
   noise: 2, 
   // 宽度 
   width: 80, 
   // 高度 
   height: 30, 
  }); 
  // 保存到session,忽略大小写 
  req.session = captcha.text.toLowerCase(); 
  console.log(req.session); //0xtg 生成的验证码
  //保存到cookie 方便前端调用验证
  res.cookie('captcha', req.session); 
  res.setHeader('Content-Type', 'image/svg+xml');
  res.write(String(captcha.data));
  res.end();
 },
Copy after login

This only implements the function of generating the verification code, so what about access?

4. Write background routing

// 获取验证码
router.get('/api/getCaptcha', function(req, res, next) {
 return api.getCaptcha(req, res, next);
})
Copy after login

When the front end calls this interface /api/getCaptcha, the verification code information is returned, which is in svg format

5. Front-end access

<img src="/api/getCaptcha" alt="captcha" >
Copy after login

But now we have needs again. When this verification code is clicked, the verification code It will be refreshed

6. Click the verification code to refresh

Change the result of the verification code just now, add an outer a label to it, and bind it to Click event, I used vue here, so it is @click, the same is true for other frameworks.

<a href="#" rel="external nofollow" @click="editCaptcha">
  <img src="/api/getCaptcha" alt="" ref="imgYzm">
</a>
Copy after login

Click eventeditCaptcha

editCaptcha () {
  this.$refs.imgYzm.src = '/api/getCaptcha?d='+Math.random()
},
Copy after login

This realizes the problem of clicking the verification code to refresh

7. Front-end verification verification code

We just got the verification code in the background, but the front-end needs How to verify?

Remember that we installed cookie-parser in 3-2, and saved the generated session in a cookie in 3-3. Here our front-end can access this cookie Got the value of the verification code.

Why should it be stored in cookies? Because the session generated by the backend cannot be accessed by the frontend, if we wait to access it, there is no security at all, so we save a copy in a cookie for frontend access.

let captcha = document.cookie.split('=')[1]
  if(this.yzm != captcha){
   return this.$message.warning('验证码错误')
  }
Copy after login

I won’t go into details about the final front-end input of account and password verification code for login verification, etc. The specific idea is this.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Use classes in ES6 to imitate Vue to make two-way binding

##vue.js element-ui Create a menu tree structure

The above is the detailed content of Node makes image verification code when logging in. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? Mar 13, 2024 pm 08:55 PM

What should I do if Google Chrome does not display the verification code image? Sometimes you need a verification code to log in to a web page using Google Chrome. Some users find that Google Chrome cannot display the content of the image properly when using image verification codes. What should be done? The editor below will introduce how to deal with the Google Chrome verification code not being displayed. I hope it will be helpful to everyone! Method introduction: 1. Enter the software, click the "More" button in the upper right corner, and select "Settings" in the option list below to enter. 2. After entering the new interface, click the "Privacy Settings and Security" option on the left. 3. Then click "Website Settings" on the right

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

Can virtual numbers receive verification codes? Can virtual numbers receive verification codes? Jan 02, 2024 am 10:22 AM

The virtual number can receive the verification code. As long as the mobile phone number filled in during registration complies with the regulations and the mobile phone number can be connected normally, you can receive the SMS verification code. However, you need to be careful when using virtual mobile phone numbers. Some websites do not support virtual mobile phone number registration, so you need to choose a regular virtual mobile phone number service provider.

Why can't I receive the verification code on my phone? Why can't I receive the verification code on my phone? Aug 17, 2023 pm 02:49 PM

Failure to receive the verification code on your mobile phone is caused by network problems, mobile phone settings problems, mobile phone operator problems and personal settings problems. Detailed introduction: 1. Network problems. The network environment where the mobile phone is located is unstable or the signal is weak, which may cause the verification code to be unable to be delivered in time; 2. Mobile phone setting problems. The text message or voice function of the mobile phone is accidentally turned off, or the The verification code sending number is added to the blacklist, resulting in the verification code not being received normally; 3. Mobile phone operator issues, the mobile phone operator may have malfunctions or maintenance, resulting in the verification code not being delivered in time, etc.

PHP image processing case: How to implement the verification code function of images PHP image processing case: How to implement the verification code function of images Aug 17, 2023 pm 12:09 PM

PHP image processing case: How to implement the verification code function of images. With the rapid development of the Internet, verification codes have become one of the important means to protect website security. Verification code is a verification method that uses image recognition technology to determine whether the user is a real user. This article will introduce how to use PHP to implement the verification code function of images, and come with code examples. Introduction A verification code is a picture containing random characters. The user needs to enter the characters in the picture to pass the verification. The main process of implementing verification code includes generating random characters and drawing characters into pictures.

PHP Development Guide: Implementing Verification Code Login PHP Development Guide: Implementing Verification Code Login Jul 01, 2023 am 09:27 AM

With the development of the Internet and the popularity of smartphones, the verification code login function is adopted by more and more websites and applications. Verification code login is a login method that verifies the user's identity by entering the correct verification code to improve security and prevent malicious attacks. In PHP development, implementing a simple verification code login function is not complicated and can be completed through the following steps. Create a database table First, we need to create a table in the database to store verification code information. The table structure can contain the following fields: id: auto-incrementing primary key phon

Verification codes can't stop robots! Google AI can accurately identify blurry text, while GPT-4 pretends to be blind and asks for help Verification codes can't stop robots! Google AI can accurately identify blurry text, while GPT-4 pretends to be blind and asks for help Apr 12, 2023 am 09:46 AM

“The most annoying thing is all kinds of weird (or even perverted) verification codes when you log into a website.” Now, there is good news and bad news. The good news is: AI can do this for you. If you don’t believe me, here are three real cases of increasing recognition difficulty: And these are the answers given by a model called “Pix2Struct”: Are they all accurate and word for word? Some netizens lamented: Sure, the accuracy is better than mine. So can it be made into a browser plug-in? ? Yes, some people said: Even though these cases are relatively simple, if you just fine-tune it, I can't imagine how powerful the effect will be. So, the bad news is - the verification code will soon be unable to stop the robots! (Danger danger danger...) How to do it? Pix2St

How to generate refreshable image verification code using PHP How to generate refreshable image verification code using PHP Sep 13, 2023 am 11:54 AM

How to use PHP to generate refreshable image verification codes. With the development of the Internet, in order to prevent malicious attacks and automatic machine operations, many websites use verification codes for user verification. One common type of verification code is the image verification code, which generates a picture containing random characters and requires the user to enter the correct characters before proceeding. This article will introduce how to use PHP to generate refreshable image verification codes and provide specific code examples. Step 1: Create a verification code image First, we need to create a verification code image

See all articles