def signin(): #instantiates a class called form which was defined in forms.py form = UserLoginForm() try: if request.method == "POST" and form.validate_on_submit(): email = form.email.data password = form.password.data #prints user input to terminal for confirmation/debug purposes print(email, password) #creates an instance of the User class for the current user from the first matching email in the email column in the db. logged_user = User.query.filter(User.email == email).first() #if the logged user and the user returned from the matched passwords if logged_user and check_password_hash(logged_user.password, password): #take the method to login from flask_user and login the loggeduser defined above login_user(logged_user) print(logged_user) #do the flash method defined on the home.html (text to display, information for the flash parameter) flash('You were successfully logged in via Email/Password', 'auth-success') #redirect user to home return redirect(url_for('home')) else: flash('Your email/password is incorrect. Please try again', 'auth-failure') #keep them on sign in page return redirect(url_for('signin')) except: raise Exception('Invalid Form Data: Please check your form...') #again, like jwt below, form=form allows template to see the variable and render it return render_template('sign_in.html', form=form)
def signin(): form = UserLoginForm() try: if request.method == 'POST' and form.validate_on_submit(): email = form.email.data password = form.password.data print(email,password) logged_user = User.query.filter(User.email == email).first() if logged_user and check_password_hash(logged_user.password, password): login_user(logged_user) flash('You were successfully logged in: Via Email/Password', 'auth-success') return redirect(url_for('home')) else: flash('Your Email/Password is incorrect.', 'auth-failed') return redirect(url_for('signin')) except: raise Exception('Invalid Form Data: Please Check Your Form') return render_template('signin.html', form=form)