def signin(): form = UserLoginForm() try: # verifying that the form is posting - see form.html (if the form has a method of POST, and also the email is an actual email, and the password is present, then the form is valid) if request.method == 'POST' and form.validate_on_submit(): # if true, get the email from the form email = form.email.data password = form.password.data # Print just to check to make sure it's working print(email, password) # Similar to a WHERE clause in SQL # Looking for User email that's set equal to what comes from the form # Need to add .first method to secify that we are looking for the user email that has been passed using the form (won't work properly without .first() - just shows SQL code - kind of grabbing the row from the SQL table) 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('site.home')) # If login unsuccessful, show this message # 'auth-failed' is called a category (can name it anything you want, and will reference it on appropriate html page) # example {% if cat == 'user-created' %} else: flash('Your email/password is incorrect', 'auth-failed') return redirect(url_for('auth.signin')) except: raise Exception('Invalid Form Data: Please Check Your Form!') return render_template('signin.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) # just to make sure you have what you think you have logged_user = User.query.filter( User.email == email).first() # similar to WHERE clause in SQL if logged_user and check_password_hash(logged_user.password, password): login_user(logged_user) flash('You have successfully logged in: via email/password', 'auth-success') return redirect(url_for('site.home')) else: flash('Your email/password is incorrect', 'auth-failed') return redirect(url_for('auth.signin')) except: raise Exception('Invalid Form Data: Please Check Your Form!') return render_template('signin.html', form=form)
def signin(): form = UserLoginForm() #Instantiating UserLoginForm try: if request.method == 'POST' and form.validate_on_submit(): #'POST' from form.html #form.validate_on_submit from forms.py(validator) email = form.email.data password = form.password.data print(email, password) logged_user = User.query.filter(User.email == email).first() #flask shell in virtual_env in cmd #logged_user is instantiating a User class (similar to GRABBING A ROW FROM THE SQL TABLE) using the filter email = email. #similar to where clause in sql, Where email in table = email that was entered into this function if logged_user and check_password_hash(logged_user.password, password): #if we find email and the same password login_user(logged_user) #from flask login (the login_user) flash('You were successfully logged in: via email/password', 'auth-success' ) #category of auth-success, which we will use in html return redirect(url_for('site.home')) else: flash('Your email/password is incorrect', 'auth-failed') return redirect(url_for('auth.signin')) except: raise Exception('Invalid Form Data: PLease Check Your Form') return render_template('signin.html', form=form)
def signin(): form = UserLoginForm() #try to get info and validate the form, if it isnt found - give error try: #checks if the request from form submission is set to 'POST' and form is validated if request.method == 'POST' and form.validate_on_submit(): #set variables equal to values entered into signin form email = form.email.data password = form.password.data #print lets us know these things are getting pulled correctly, helps error checking/finding print(email, password) #we want to filter by the email provided by the form (defined above) #.first() asks for first value that comes back under this query logged_user = User.query.filter(User.email == email).first() #want to check password and currently logged user 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('site.home')) else: flash('Your email/password is incorrect', 'auth-failed') return redirect(url_for('auth.signin')) except: raise Exception('Invalid Form Data: Please Check Your Form!') #pass the form = UserLoginForm() to return statement return render_template('signin.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 username/password') return redirect(url_for('auth.signin')) except: raise Exception('Invalid For Data: Please Check Your Form!') return render_template('signin.html')
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 i :via email/password', 'auth-success') else: flash('Your email/password is incorrect', 'auth-failed') return redirect(url_for('auth.signin')) except: raise Exception('Invalid form dtat: please check your form!') return render_template('signin.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) #not needed but verifies things are there 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('site.home')) else: flash('Your Email/Password is incorrect', 'auth-failed') return redirect(url_for('auth.signin')) except: raise Exception('Invalid Form Data: Please Check Your Form!') return render_template('signin.html', form=form)