Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have multiple error when i try to sign-up with email and another error when i try to login

  1. when i register i must not succeed in doing it several times with the same e-mail i must return ‘Email already exists’
  2. when i logged i have an error ‘Email does not exists’ and i can not connect

here are my login and sign-up functions

@auth.route('/login', methods=['GET','POST']) 
def login() :
    if request.method == 'POST':
        email = request.form.get('email')
        password = request.form.get('password')

        user = User.query.filter_by(email=email).first()
        if user:
            if check_password_hash(user.password, password):
                flash('Logged in successfully!', category='success')
                login_user(user, remember=True)
                return redirect(url_for('views.home'))
            else:
                flash('Incorrect password. Try again!',category='error')
        else:
            flash('Email does not exist.', category='error')


    
    return render_template("login.html", boolean=True)

@auth.route('/sign-up',methods=['GET','POST'])
def sign_up() : 
    if request.method == 'POST':
        email = request.form.get('email')
        first_name = request.form.get('firstName')
        password1 = request.form.get('password1')
        password2 = request.form.get('password2')

        user = User.query.filter_by(email=email).first()
        if user:
            flash('Email already exists.', category='error')
        elif len(email) < 4 :
            flash('Email must be greater then 3 characters.', category='error')
        elif len(first_name) < 2 :
            flash('First name must be greater then 1 characters.', category='error')
        elif password1 != password2 :
            flash('Password don\'t match.', category='error')
        elif len(password1) < 7 :
            flash('Password must be greater then 6 characters.', category='error')
        else :
            new_user = User(email=email, first_name=first_name,password=generate_password_hash(password1, method='sha256'))
            db.session.add(new_user)
            db.session.commit
            login_user(user, remember=True)
            flash('Account created!', category='success')
            return redirect(url_for('views.home'))

    return render_template("sign_up.html") 

I want to sign up with one email and also one for each user and to resolve the login error please.“`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To resolve the issue of multiple sign-ups with the same email, you need to update your code to check if the user already exists before creating a new account. To do this, you can modify the if user: condition to include a check for whether the email is already registered. Here’s the updated code:

user = User.query.filter_by(email=email).first()
if user:
    flash('Email already exists.', category='error')
elif len(email) < 4 :
    flash('Email must be greater then 3 characters.', category='error')
elif len(first_name) < 2 :
    flash('First name must be greater then 1 characters.', category='error')
elif password1 != password2 :
    flash('Password don\'t match.', category='error')
elif len(password1) < 7 :
    flash('Password must be greater then 6 characters.', category='error')
else :
    new_user = User(email=email, first_name=first_name,password=generate_password_hash(password1, method='sha256'))
    db.session.add(new_user)
    db.session.commit()
    login_user(new_user, remember=True)
    flash('Account created!', category='success')
    return redirect(url_for('views.home'))

Note that the login_user function should be called with the new_user object instead of the user object that was retrieved from the database. Also, you should add parentheses to the commit method call.

To resolve the login error, you can modify your login function to check if the email exists in the database before attempting to authenticate the user. Here’s the updated code:

@auth.route('/login',methods=['GET','POST'])
def login() : 
    if request.method == 'POST':
        email = request.form.get('email')
        password = request.form.get('password')

        user = User.query.filter_by(email=email).first()
        if not user:
            flash('Email does not exist.', category='error')
        elif not check_password_hash(user.password, password):
            flash('Incorrect password.', category='error')
        else:
            login_user(user, remember=True)
            return redirect(url_for('views.home'))

    return render_template("login.html")

Since you did not post any issue with your login_user function, it is referenced in these updates with the assumption that it already works as you wish.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading