Home > OS >  Sending an email consisting of a verification code and verify that e-mail with a countdown clock in
Sending an email consisting of a verification code and verify that e-mail with a countdown clock in

Time:08-25

To check users' email addresses, I want to email a verification code to the user and confirm it within a minute. I tried with the script below, but the script stops after printing "t". It could be due to async function I used. But only with that library, I could run two functions simultaneously.

email_form = st.form(key='my_email_form',clear_on_submit=False)
email=email_form.text_input(label='Please enter your email address')
submit_e_button = email_form.form_submit_button(label='Send')

if submit_e_button:
    with st.form(key='my_code'):
        code = st.text_input(label='Enter code')
        submit_button = st.form_submit_button(label='Confirm')
    # global key
    # global code
    global t1
    t1=time.time()
    fixed_digits = 4
    key=random.randrange(1111, 9999, fixed_digits)
    sentmail2()
    t=0
    async def clocktime():
        global t
        ph = st.empty()
        N = 1*60
        for secs in range(N,0,-1):
            mm, ss = secs//60, secs`
            ph.metric("Countdown", f"{mm:02d}:{ss:02d}")
            time.sleep(1)
            t2=time.time()
            t=t2-t1
            if ss==1:
              ph.metric("Countdown", "Time is out")
    async def submittion():
        print(t)
        if submit_button:
            if float(t) > 1*60:
                st.text('Enter before the time limit.')
                b = False
          # print("You have run out of time!")
            if float(t) < 1*60: 
                print("code "   str(code))
                print(key)
                if submit_button:
                    if b == True:
                      print("code"   code)
                      print(key)
                      if str(code)==str(key):
                        st.text('succcess')
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    asyncio.ensure_future(clocktime())
    asyncio.ensure_future(submittion())
    loop.run_forever()

CodePudding user response:

Is it intentional time.sleep(1) in async def clocktime?

With asyncio you usually want to await asyncio.sleep(1) instead, allowing the execution of other coroutines while awaiting.

CodePudding user response:

Not directly about the code, but from a design perspective this doesn't seem well designed. Mainly the part of waiting for the user to do something, and wasting compute power on it.

I would suggest an alternate approach, just generate their code/token and date of when it was issued into a database (or if small use, a temp file - this is usually done in a protected environment e.g. server). And generate a separate function/endpoint (API) for verifying those codes/tokens (usually in a form of a button if end user application, or a direct link), which just checks if the provided info and database info matches in the given time constraints.

Would suggest checking out a tutorial on how it is usually done as in this geeksfogeeks website.

  • Related