This is solved finally. The issue was with how Streamlit handles the forms and their submission. Anytime a form is submitted, Streamlit will rerun the app or the code fragment which will reset the value for the variables in that fragment or the global variable if the app is rerun.
To solve this, I removed all the forms from the login module and combined the functions into single function so when the app is rerun, the value of the variable is not reset.