7

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

Far
  • 420
  • 5
  • 14

1 Answers1

8

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/

pragman
  • 1,564
  • 16
  • 19
  • Thank you so much; but I have a question : my database is django (pycharm) is sqlite3; is it possible to have Redis at the same time or I have to choose one of them? (shall I migrate all my tables which are in sqlite3 right now, to Redis ? ) @Bitonator – Far Nov 14 '16 at 07:48
  • 1
    Its possible to have redis at the same time. Keep the pin in redis, and keep the database as it is in sqllite3, no migration required. – pragman Nov 14 '16 at 07:50