Sending verification codes through SMS is the most popular and securest way to verify user identities. Currently, SMS verification codes are widely used in various application scenarios such as user registration, password reset, login protection, identity verification, random password generation, and transaction confirmation.
This document uses developing a verification code-enabled login and signup service based on SCF as an example to describe how to implement the SMS verification code feature.
In addition to SCF, you can also use the SendSms API for this purpose.
SDKAppID
of the SMS application.After an SMS signature or body template is submitted, it will be reviewed within two hours generally. You can configure alarm contacts and set template/signature review notifications to receive review result notifications.
Parameter | Sample Value |
---|---|
Signature purpose | For self-use (the signature is a company name, website, product name, or something else verified under the current account) |
Signature type | App |
Signature content | Test demo |
Certificate type | Screenshot of WeChat Mini Program settings page |
Certificate upload |
Parameter | Sample Value |
---|---|
Template Name | Verification code SMS |
SMS type | Regular SMS |
SMS content | Your signup verification code is {1}. Please enter it within {2} minutes. If the signup was not initiated by you, please ignore this message. |
Note:Individual users have no permission to modify the rate limit. To use this feature, change "Individual Identity" to "Organizational Identity". For detailed directions, see Identity Verification Change Guide.
To ensure business and channel security and minimize potential financial losses caused by malicious calls of SMS APIs, we recommend you set the SMS delivery rate limit.
This document uses the default SMS delivery rate limit policy as an example.
By default, SCF is deployed in the public network and can access public network only. If you need to access Tencent Cloud resources such as TencentDB instances, you need to build a VPC to ensure data and connection security.
Note:The CIDRs of the VPC and subnet cannot be modified after creation.
Parameter | Sample Value |
---|---|
Region | South China (Guangzhou) |
Name | Demo VPC |
IPv4 CIDR Block | 10.0.0.0/16 |
Subnet name | Demo subnet |
IPv4 CIDR Block | 10.0.0.0/16 |
Availability Zone | Guangzhou Zone 3 |
The region and subnet AZ of the TencentDB for Redis instance must be the same as those of the VPC configured in step 3.
Parameter | Sample Value |
---|---|
Billing Mode | Pay-as-you-go |
Region | Guangzhou |
Database version | Redis 4.0 |
Architecture | Standard architecture |
Network | Demo VPC and demo subnet |
Instance name | Demo database |
Quantity | 1 |
SCF currently supports development in Python, Node.js, PHP, Java, and Go. This document uses Node.js as an example.
Parameter | Sample Value |
---|---|
Function name | Demo |
Runtime environment | Node.js 8.9 |
Creation method | Template function: helloworld |
Go to the SCF console and select the SMS demo to deploy it.
Set the environment variables of the demo in Advanced Configuration.
Field | Description |
---|---|
REDIS_HOST | Redis database address. |
REDIS_PASSWORD | Redis database password. |
SMS_TEMPLATE_ID | Template ID. You must enter the ID of an approved template, which can be viewed in the SMS console. |
SMS_SIGN | Content of the SMS signature, which should be encoded in UTF-8. You must enter an approved signature, which can be viewed in the SMS console. Note: this parameter is required for Chinese Mainland SMS. |
SMS_SDKAPPID | SMS SdkAppid actually generated after an application is added in the SMS console, such as 1400006666. |
Set the permissions of SCF execution role in Advanced Configuration.
You need to associate the QcloudSMSFullAccess
policy with the SCF_QcsRole
role in the CAM console.
In this way, the ``TENCENTCLOUD_SECRETID,
TENCENTCLOUD_SECRETKEY, and
TENCENTCLOUD_SESSIONTOKEN` environment variables can be obtained in the code, which will be used by the SMS SDK.
Click Complete to deploy the function.
Create an SCF API Gateway trigger and request the trigger address to use SMS capabilities.
Verification codes have a high requirement for timeliness. You can store verification codes in the memory or TencentDB for Redis and use the mobile number as a key to store information such as sending time, verification code, number of verification attempts, and verification result.
Request parameters:
Field | Type | Description |
---|---|---|
method | string | Request method, whose value is getSms |
phone | string | Mobile number in the format of area code + mobile number, such as 86185662466** |
Request parameters:
Field | Type | Description |
---|---|---|
method | string | Request method, whose value is login |
phone | string | Mobile number in the format of area code + mobile number, such as 86185662466** |
code | string | 6-digit verification code |
Field | Description |
---|---|
InValidParam | Missing parameter |
MissingCode | Missing verification code parameter |
CodeHasExpired | The verification code has expired |
CodeHasValid | The verification code is invalid |
CodeIsError | Please check whether the mobile number and verification code are correct |
If you have any questions, contact SMS Helper for assistance.
Was this page helpful?