Search code examples
pythondjangosms-verification

where to save the verification code sent to the user for signing up


I'm somehow new to Django and it's my first time to implementing a signUp form with sms verification.

I get the user mobile number and generate a random number and send to him; I want the generated code to be expired after 30 minutes and after that I don't need them, so it seems that it is not a good idea to save them in DB and after the expiration time, delete them.

I wonder if anybody can help me with the the question that "what is the best way to implement this?"

Thank you so much in advance


Solution

  • save them in Redis. Redis keys can have a TTL(Time-To-Live), keys with TTL are deleted automatically after the time period.

    import redis
    r = redis.StrictRedis()
    
    # create pin 
    r.set("<phone-number>", <sms-pin>)
    r.expire("<phone-number>", 1800) # 1800 seconds = 1/2 hour
    
    # get pin
    if r.exists("<phone-number>"):
        pin=r.get("<phone-number>")
        ... validate pin
    else:
        ... invalid pin
    

    More docs at http://agiliq.com/blog/2015/03/getting-started-with-redis-py/