Esempio n. 1
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(userName=username).first()

        user_error = ""
        password_error = ""

        if not user:
            user_error = "That user doesn't exist"
            username = ""
            return render_template("login.html", user_error=user_error)
        if not check_pw_hash(password, user.pw_hash):
            password_error = "Incorrect password"
            return render_template("login.html",
                                   user_error=user_error,
                                   password_error=password_error,
                                   username=username)

        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            return redirect('/newpost')

    return render_template('login.html', title="Log in")
Esempio n. 2
0
def login():
    # setting error variables plus username so the typed in username will remain if error occurs
    username_error = ''
    password_error = ''
    username = ''
    # conditional for once user submits form
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        # conditional for if the username and password are correct using hashing
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            return redirect('/newpost')
        # else with errors
        else:
            if not user:
                username_error = 'Username does not exist'
            elif not check_pw_hash(password, user.pw_hash):
                password_error = 'Username and password do not match.'

    return render_template('login.html',
                           password_error=password_error,
                           username_error=username_error,
                           username=username)
Esempio n. 3
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        # if user exists and password is valid, login and redirect
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            return redirect('/newpost')
        else:
            #TODO add error messages on validation
            if user and not check_pw_hash(password, user.pw_hash):
                flash('password is incorrect', 'error')
            if not user:
                flash('username does not exist', 'error')
            if not username and not password:
                # error message for both fields blank
                flash('input fields blank are blank, please enter a value',
                      'error')
            if not username or not password:
                if not username:
                    # error message for username blank or invalid
                    flash('username field is blank, please enter a value',
                          'error')
                if not password:
                    # error message for password blank or invalid
                    flash('password field is blank, please enter a value',
                          'error')
            return redirect('/login')

    return render_template('login.html')
Esempio n. 4
0
def login():
    error_u = ''
    error_p = ''
    session_name = 'guest'
    if 'username' in session:
        session_name = session['username']
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        if not user:
            error_u = 'User does not exist.'
            flash('User does not exist. Please signup for an account.',
                  'flash-alert')
            return redirect('/signup')
        elif user and not check_pw_hash(password, user.pw_hash):
            error_p = 'Password is incorrect.'
            flash('Password is incorrect.', 'flash-alert')
        elif user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            flash("Logged in successfully.", 'flash-success')
            return redirect('/newpost')
        else:
            error_u = 'Incorrect username.'
            error_p = 'Incorrect password.'
            flash("Incorrect username and password combination.",
                  'flash-alert')
    return render_template('login.html',
                           title="Login",
                           session=session_name,
                           error_u=error_u,
                           error_p=error_p)
Esempio n. 5
0
def login():
    page_title = 'Login'
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        if user and check_pw_hash(password, user.pw_hash):
            # User enters a username that is stored in the database with the correct password and is redirected to the /newpost page with their username being stored in a session.
            session['username'] = username
            flash('Logged in.', 'success')
            return redirect('/newpost')
        elif user and not check_pw_hash(password, user.pw_hash):
            # User enters a username that is stored in the database with an incorrect password and is redirected to the /login page with a message that their password is incorrect.
            flash('Password is incorrect.', 'danger')
            return render_template('login.html',
                                   page_title=page_title,
                                   username=username,
                                   logged_in=logged_in())
        elif not user:
            # User tries to login with a username that is not stored in the database and is redirected to the /login page with a message that this username does not exist.
            flash('Username does not exist.', 'warning')
            return render_template('login.html',
                                   page_title=page_title,
                                   username=username,
                                   logged_in=logged_in())
    if logged_in():
        flash('Already logged in as ' + session['username'] + '.', 'info')
    return render_template('login.html',
                           page_title=page_title,
                           logged_in=logged_in())
Esempio n. 6
0
def login():    
    if request.method == "GET":
        return render_template('login.html', page_title="login")
    
    username = request.form["username"]
    password = request.form["password"]
    user = User.query.filter_by(username=username).first()
    
    if username == "":
        username_error = "Announce yourself!"
    elif not user: 
        username_error = "Sorry! We don't know you yet!"
    else:        
        username_error = ""    
    if password == "" and user: 
        password_error = "What's the password?" 
    elif user: 
        if not check_pw_hash(password, user.pw_hash):
            password_error = "Tsk! Tsk!"
        else:
            password_error = ""
    elif not user: 
        password_error = ""    
    if username_error == "" and  password_error == "":
        if request.method == 'POST':
            if user and check_pw_hash(password, user.pw_hash):
                session['username'] = username
                flash("logged in")
                return redirect('/newpost')    
    else:
        return render_template('login.html', page_title = "login", username = username, 
            username_error = username_error, password_error = password_error)
Esempio n. 7
0
def login():
    # when a GET request return an empty form to collect login information
    if request.method == 'GET':
        return render_template('login.html')

    # when a PUSH with form information request infomation input through form
    elif request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        # check the user db for the first instance of the supplied username
        user = User.query.filter_by(username=username).first()

        # if the user is valid and the password provided matches the hashed password in the db, flash the message to the / route
        if user and check_pw_hash(password, user.pw_hash):
            session['user'] = username
            return redirect('/newpost')
    # otherwise return the error and redirect to the login page
    if not user:
        username_error = "Username does not exist."
        return render_template('login.html', username_error=username_error)

    if not check_pw_hash(password, user.pw_hash):
        password_error = "User password incorrect"
        return render_template('login.html',
                               username=username,
                               password_error=password_error)
Esempio n. 8
0
def login():
    if request.method == 'GET':
        return render_template('login.html')
    elif request.method == 'POST':
        email = request.form['email']
        password = request.form['password']
        users = User.query.filter_by(email=email)
        if not is_email(email):
            flash('Please enter a valid email')
            return render_template('login.html',email=email)
        if users.count() == 1:
            user = users.first()
        else:
            flash(email + ' does not have an account. Feel free to create one by clicking the link below!')
            return render_template('login.html',email=email)
        if check_pw_hash(password, user.pw_hash):
                session['user'] = user.email
                flash('welcome back, '+user.email)
                return redirect('/newpost')
        elif password == '':
            flash('Please enter a password.')
            return render_template('login.html',email=email)
        elif not check_pw_hash(password,user.pw_hash):
            flash('opps, it looks like you have provided a wrong password. Please try again.')
            return render_template('login.html', email=email)
    return render_template('login.html', email=email)
Esempio n. 9
0
def login():
    if 'username' in session:
        flash('Already logged in.')
        return redirect('/blog')
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        user = User.query.filter_by(username=username).first()
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            flash("Welcome! You're Logged in!")
            # test

            ##########################
            # test complete
            return redirect('/newpost')
        elif user and (
                check_pw_hash(password, user.pw_hash) == False
        ):  ### need to insert user into template so they dont have to retype.
            flash('Incorrect password.')
            return redirect('/login')
        elif not user:
            flash('Username does not exist.')
            return redirect('/login')

    return render_template('login.html')
Esempio n. 10
0
def login():
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']
        user = User.query.filter_by(email=email).first()

        if user and check_pw_hash(password, user.pw_hash):
            session['email'] = email
            flash('Logged in')
            return redirect('/newpost')
        elif not email:
            flash('No Email entered')
            email = ''
        elif not password:
            flash('No Password entered')
            password = ''
        elif check_pw_hash(password, user.pw_hash) != password:
            flash('Incorrect Password')
            email = email
            return redirect('/login')
        elif not user:
            flash('User does not exist')
            return redirect('/signup')

    return render_template('login.html')
Esempio n. 11
0
def login():

    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = user.username
            flash('Logged in', 'success')
            if 'Day_ID' in session:
                return render_template('clockout.html',
                                       page_title='Enter your day end mileage',
                                       user=user)
            return render_template('clockin.html',
                                   page_title='Enter your beginning mileage',
                                   user=user)
        elif not user:
            flash(
                'That username does not exist, please retry or visit the signup page',
                'error')
            return redirect('/login')
        elif not check_pw_hash(password, user.pw_hash):
            flash('Incorrect password', 'error')
            return render_template('login.html', username=username)

    return render_template('login.html',
                           page_title='Log in to track your mileage')
Esempio n. 12
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            newpost_redirect = "/newpost?user="******"username does not exist or is incorrect"
                return render_template("login.html",
                                       title='Blogz!',
                                       login_check=login_check,
                                       error=error)
            elif not check_pw_hash(password, user.pw_hash):
                error = "password is incorrect, try again"
                return render_template("login.html",
                                       title='Blogz!',
                                       login_check=login_check,
                                       error=error)

    return render_template("login.html",
                           title='Blogz!',
                           login_check=login_check)
Esempio n. 13
0
def login():
    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]
        user = User.query.filter_by(username=username).first()
        if user and check_pw_hash(password, user.pw_hash):
            session["username"] = username
            return redirect('/newpost')
        elif user and not check_pw_hash(password, user.pw_hash):
            flash("Incorrect password, please try again.", "error")
        else:
            flash("Username does not exist. Please try again or click the 'Create Adventures' button to Sign Up.", "error")

    return render_template("login.html")
Esempio n. 14
0
def login():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        user = User.query.filter_by(username=username).first()
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            flash("Logged in")
            return redirect('/newpost')
        elif user and not check_pw_hash(password, user.pw_hash):
            flash('user password is incorrect', 'error')
        elif not user:
            flash('user does not exist', 'error')
    return render_template('login.html')
Esempio n. 15
0
def log_in():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            return redirect('/newpost')
        elif user and not check_pw_hash(password, user.pw_hash):
            flash('Incorrect password.', 'error')
            return redirect('/login')
        elif not user:
            flash('Username does not exist.', 'error')
            return redirect('/login')
    return render_template('login.html')
Esempio n. 16
0
def login():
    #if req method is post the user has filled out the form and now its being validated
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        users = User.query.filter_by(username=username)
        if users.count() == 1:
            user = users.first()
            if check_pw_hash(password, user.pw_hash):
                #password checks
                session['user'] = user.username
                flash('welcome back, ' + user.username)
                return redirect("/newpost")

            #if password is blank
            if password == '':
                flash('please enter a password', category='error')
                return redirect("/login")

            #if password does not match user.password
            flash('password is incorrect', category='error')
            return redirect("/login")

        #if username not in db
        flash('username not found, please register', category='error')
        return redirect("/register")

    #if req method is get it means its your first time to the page
    else:
        return render_template('login.html')
Esempio n. 17
0
def login():
    # If the user submits a POST, i.e. tries to log in
    if request.method == 'POST':
        # Request the data from the form fields
        username = request.form['username']
        password = request.form['password']
        # Go try to find the user in the database
        user = User.query.filter_by(username=username).first()
        # If the username exists in the database and the password matches,
        # Make a new session using their username, flash the logged in,
        # and then redirect them to "/newpost"
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            session['id'] = user.id
            flash("Logged in")
            return redirect('/newpost')

        elif user == None:
            flash("You don't exist or you may have mistyped your username.")

        elif user.password != password:
            flash("Your passwords don't match!")

        return render_template('login.html', title="Log In", username=username)

    return render_template('login.html', title="Log In")
Esempio n. 18
0
def login():
    #retrieve user info from login page
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()

        #if user or password field is empty send message
        if username == "" or password == "":
            flash('One or more field is empty, please try again.', 'error')
            return redirect('/login')
        else:
            #user not in db, return to login page
            if not user:
                flash('User does not exist.', 'error')
                return redirect('login')

        #everything is good, begin session
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            flash(username + ":" + " Logged In", 'info')
            return redirect('/newpost')
        else:  # incorrect password, return to login page
            flash("Password is incorrect.", 'error')
            return redirect('/login')

    return render_template("login.html")
Esempio n. 19
0
def sign_in():
    if request.method == "GET":
        return render_template("sign-in.html")
    if request.method == "POST":
        username = request.form['username']
        password = request.form["password"]
        user = User.query.filter_by(username=username).first()

        name_error = ""
        pass_error = ""

        if not user:
            name_error = "Incorrect Username"
            username = ''
        if user and check_pw_hash(password, user.pw_hash):
            session["username"] = username
            return redirect('/')
        else:
            pass_error = "Incorrect Password"
            password = ""
            return render_template("sign-in.html",
                                   name_error=name_error,
                                   pass_error=pass_error,
                                   username=username,
                                   password=password)
Esempio n. 20
0
def login():
    error = False

    #get form data
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        #query the database to find the user record.
        user = User.query.filter_by(username=username).first()
        #if the user exsist, and the password matches the one in the database, log the user in.
        if (user and (check_pw_hash(password, user.pw_hash))):
            session['username'] = username
            flash("Logged in")
            return redirect("/new-post")
        #otherwise, display an error message
        elif (not user):
            flash("User doesn't exsist.", "error")
            error = True
        else:
            flash("Password incorrect.", "error")
            error = True

    return render_template('login.html',
                           title="login",
                           username="",
                           ferror=error)
Esempio n. 21
0
def login():

    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        user = User.query.filter_by(username=username).first()

        # no errors
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            return redirect('/newpost')

        # errors
        elif not user:
            flash('Username not found', 'error_username')

        # errors
        else:
            flash('Username Password combo not found', 'error_password')

        # provide state of attempted form
        return render_template('login.html',
                               title='login',
                               username=username,
                               password=password)

    return render_template('login.html', title='login')
Esempio n. 22
0
File: main.py Progetto: ctam91/blogz
def login():
    '''
    Shows login page and login form
    '''
    # If it is a post method, the user is trying to login and we need to verify email, password, and if user exists in database
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()

        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username   
            flash("Successfully logged in",'info')
            return redirect('/newpost')

        if username == "" and password == "":
            flash('Please enter a username and password','error')

        if user and user.pw_hash != password:
            flash('User password incorrect', 'error')

        if not user and len(password) > 1:
            flash('Please enter a username', 'error')

        if not user and len(username) > 1:
            flash('User does not exist', 'error')

        return render_template('login.html', username=username)

    return render_template('login.html')
Esempio n. 23
0
def login():
    if 'username' in session:
        login_name = session['username']
        return render_template('status.html', login_name=login_name)

    if request.method == 'GET' and 'username' not in session:
        return render_template('login.html', login_name=False)

    if request.method == 'POST' and 'username' not in session:
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()

        # If this username exists and the hashed password is correct, go ahead and log the user in.
        if request.method == 'POST' and user and check_pw_hash(
                password, user.pw_hash):
            session['username'] = username
            return redirect('/newpost')

        else:
            # If the username does not exist, show the username alert.
            if request.method == 'POST' and not user:
                username_status = "The username you entered does not exist."
                return render_template('login.html',
                                       username_alert=username_status)

            # If the username exists, but the password is incorrect, show the password alert.
            if request.method == 'POST' and user and user.password != password:
                password_status = "The password you entered is incorrect."
                return render_template('login.html',
                                       username_hold=username,
                                       password_alert=password_status)
Esempio n. 24
0
def login():
    user_email = ""
    if request.method == 'POST':
        user_email = request.form['email']
        user_password = request.form['password']

        user = User.query.filter_by(name=user_email).first()

        if not user_email.strip():
            flash('Username cannot be empty', category='error_email')
            return redirect('/login')

        elif not user_password.strip():
            flash('Password cannot be empty', category='error_password')
            return redirect('/login')

        elif not user:
            flash(
                'User does not exits. Please check the user name or regiter new.',
                category='error_email')
            return redirect('/login')

        elif not check_pw_hash(user_password, user.password):
            flash('Password is incorrect.', category='error_password')
            return redirect('/login')

        else:
            session['email'] = user_email
            flash('Logging successful.', category='success')
            return redirect('/newpost')

    return render_template("login.html", email=user_email)
Esempio n. 25
0
def login():
    if request.method == "GET":
        try:
            if session['username']:
                flash("You're already logged in!", "positive")
                return redirect("/view-updates")
        except KeyError:
            return render_template('login.html')

    elif request.method == "POST":
        username = request.form['username']
        password = request.form['password']
        users = User.query.filter_by(username=username)

        if users.count() == 1:
            user = users.first()
            if user and check_pw_hash(password, user.pw_hash):
                session['username'] = username
                return render_template('edit.html', username=username)
            else:
                flash("Wrong, try again!", "negative")
                return redirect('/login')
        else:
            flash(
                "Maybe you don't have an account, you can sign up for one with the link below.",
                "negative")
            return redirect('/login')
Esempio n. 26
0
def login():

    form = LoginForm()
    user_theme = "dark-theme"

    if request.method == "GET":
        return render_template("login.html", title="Log In", form=form)

    else:

        email = form.email.data.lower()
        password = form.password.data

        if not User.query.filter_by(email=email).first():

            flash("Invalid username or password.", "error")
            return render_template("login.html", title="Log In", form=form)

        user = User.query.filter_by(email=email).first()

        if not check_pw_hash(password, user.pw_hash):

            flash("Invalid username or password.", "error")
            return render_template("login.html", title="Log In", form=form)

        session["user"] = email
        return redirect(url_for("home"))
Esempio n. 27
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        if (not username) or (username.strip() == ""):
                flash("Please specify username.")
                error_exists=True
        else:

            user = User.query.filter_by(username=username).first()
            if user:
                if check_pw_hash(password,user.pw_hash):
                    flash('User password incorrect!')
                    return render_template('login.html')
                else:
                    session['username'] = username
                    flash("Logged in")
                    return redirect('/newpost')
     
             
            else:
                flash('user does not exists!')
                return render_template('login.html')
        
    return render_template('login.html')
Esempio n. 28
0
def login():
    #page error message initization
    username_error = ""
    password_error = ""
    #runs when the user sends a post request to server
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        #if the username that the user entered is not in the database they're made aware that
        #they haven't entered a valid username
        if not user:
            flash("Username does not exist", "error")
            username_error = "Invalid Username"
            #rerender the page with appropriate error messages
            return render_template('login.html',
                                   title="Login Page",
                                   session=session,
                                   page_title="Log in",
                                   username_error=username_error,
                                   password_error=password_error)
        #if they entered a password that does not match the password on file
        #the user is alerted that they have not entered the correct password
        if user and not check_pw_hash(password, user.pw_hash):
            flash("Incorrect Password", "error")
            password_error = "Incorrect Password"
            #rerender the page with appropriate error messages
            return render_template('login.html',
                                   title="Login Page",
                                   session=session,
                                   page_title="Log in",
                                   username_error=username_error,
                                   password_error=password_error,
                                   username=username)
        #if the username exist and they have entered the password for that user correctly then
        #they are logged in and alerted that it worked
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            flash("Logged in")
            return redirect('/newpost')
        else:
            flash('User password incorrect, or user does not exist', 'error')
    #this loads the page initially for the user
    return render_template('login.html',
                           title="Login Page",
                           session=session,
                           page_title="Log in")
Esempio n. 29
0
def login():

    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]
        user = User.query.filter_by(username=username).first()
        if user and check_pw_hash(password, user.pw_hash):
            session["username"] = username
            flash("Logged In!") 
            return redirect("/newpost")
        elif not user:
            flash("Username does not exist.", 'error')
            return render_template("login.html")
        elif not check_pw_hash(password, user.pw_hash):
            flash("Password is incorrect.", 'error')
            return render_template("login.html", username=username)
        
    return render_template("login.html")
Esempio n. 30
0
def login():

    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        if user and check_pw_hash(password, user.pw_hash):
            session['username'] = username
            flash('Logged in', 'success')
            return redirect('/newpost')
        if not user or user == '':
            flash('User don\'t exist, duder.', 'error')
            return render_template('login.html')
        if user.pw_hash != check_pw_hash(password,
                                         user.pw_hash) or password == '':
            flash('Wrong password', 'error')
            return render_template('login.html')

    return render_template('login.html')
Esempio n. 31
0
def login():

    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        usererror = passerror = ""

        if not user:
            usererror = "That user doesn't exist yet."
            return render_template('login.html',
                                   title='Login',
                                   user=get_user(),
                                   username=username,
                                   usererror=usererror,
                                   blogs=get_blogs(),)

        if not check_pw_hash(password, user.pw_hash):
            passerror = "That password is incorrect."

        if any([usererror, passerror]):
            return render_template('login.html',
                                   title='Login',
                                   user=get_user(),
                                   username=username,
                                   usererror=usererror,
                                   passerror=passerror,
                                   blogs=get_blogs(),)

        session['user'] = username
        flash('Welcome back, ' + username + '!', 'confirmation')
        return redirect('/blog')

    return render_template('login.html',
                           title='Login',
                           user=get_user(),
                           blogs=get_blogs(),)