How to delete authentication code from database after a time period

Hi All,

I am hoping someone can help me here.

I need to set up a schedule that regularly runs to delete an authentication code sent to user’s email. I have setup data base table setup correctly with a time stamp when the code is sent. But I am not sure how to run the schedule and delete the code if the user does not use the code in xx minutes.

Any help is appreciated - thanks in advance :slight_smile:

Hi seriatim,

The workflow could look something like this:

  1. Add another timestamp field to store a value for the valid time limit - this can be based on the timestamp when an email is sent. So, if you want the user to use the code within 15 minutes of sending the email, then add 15 minutes to the email timestamp and store this in the new field
  2. Add a new field to store values 1 or 0. The field can be named as CodeUsed. When an email has been successfully sent, update the value to 0. This means the code has not yet been used.
  3. Add a condition in the user login API to check whether the user’s login time is before the expiry time. If true, user can be successfully logged. Upon successful login, update the value for CodeUsed field to 1, meaning the user has logged in within the xx minutes and used the authentication code.
  4. In your scheduler, you can check these conditions - current time is past the code valid time and CodeUsed==0, delete the authentication code else no action is required.
1 Like

What language are you using. Node has a scheduler built in, for PHP you will need a cron job

Yes, in PHP I used python cron jobs for multiple tasks regarding making changes initiated from the server. Node scheduler even better.

I would opt for @guptast solution, that’s how I do it all the time. I would use two dates, one for created and another for used (instead of 0 and 1) and to not delete code, this allows you also to create reports on how much time customers take to pick up the mai / code

2 Likes

Thanks @guptast. It worked :slight_smile:

1 Like

Hey Brian, I am using NodeJS.

Thanks @HeikoK. Not deleting codes is a good idea from reporting perspective.