User Registration API Flow: Image and SMS Verification Implementation

User Registration Interface

Image Code Verification Endpoint (Returns JPG Image)

The UUID generation occurs automatically when the page loads. This implementation has been resolved by generating the UUID via JavaScript on page initialization.

Endpoint:

/api/v1_0/image_codes/{image_code_id}

The image_code_id parameter is a UUID generated on the frontend and passed through the URL path.

Implementation Logic:

Call the captcha generation function:

code_text, code_value, image_bytes = captcha.generate_captcha()

Store the generated code in Redis with expiration:

redis_client.setex(f'image_code_{image_code_id}', 300, code_value)

Return the image data with proper headers:

response = make_response(image_bytes)
response.headers['Content-Type'] = 'image/jpeg'
return response

SMS Code Verification Endpoint (Returns Mobile Verification Code)

Request URL:

GET /api/v1_0/sms_codes/17612345678?image_code=werz&image_code_id=61242

Request Parameters:

captcha_input = request.args.get('image_code')
captcha_id = request.args.get('image_code_id')

Validation Steps:

Retrieve the stored captcha from Redis:

stored_captcha = redis_client.get(f'image_code_{captcha_id}')

If stored_captcha is None, the code has expired. Compare case-insensitively:

if stored_captcha.lower() != captcha_input.lower():
    return error_response('Invalid captcha code')

Check if the mobile number is already registered:

existing_user = User.query.filter_by(mobile=mobile_number).first()
if existing_user is not None:
    return error_response('Mobile number already registered')

Generate a 6-digit random SMS code:

sms_verification = f'{random.randint(0, 999999):06d}'

Store the SMS code in Redis:

redis_client.setex(f'sms_code_{mobile_number}', constants.SMS_CODE_EXPIRY, sms_verification)

Send SMS via CCP interface:

sms_provider = CCP()
send_result = sms_provider.send_template_sms(
    mobile_number, 
    [sms_verification, constants.SMS_CODE_EXPIRY / 60], 
    template_id=1
)

Response Handling:

if send_result == 0:
    return jsonify({
        'errno': RET.OK,
        'errmsg': 'SMS sent successfully'
    })
else:
    return jsonify({
        'errno': RET.THIRDERR,
        'errmsg': 'SMS sending failed'
    })

Tags: Flask Redis API User Registration SMS Verification

Posted on Fri, 15 May 2026 09:22:06 +0000 by duane