Пример #1
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            if not user.active:
                flash('In order to log in please confirm your account!',
                      'warning')
                return render_template('users/login.html',
                                       title='Login',
                                       form=form)
            # remember refers to the Remember me on Login page.
            login_user(user, remember=form.remember.data)
            # When we try to access a page that requires login, eg. account page.
            # It redirects us to login page, and in the URL there is parameter of the page
            # that we wanted to visit:
            # http://127.0.0.1:5000/login?next=%2Faccount
            # Here we are getting that parameter and redirecting user to that page.
            next_page = request.args.get('next')
            # current_app.logger.info('Next page parameter: %s', next_page)
            return redirect(next_page) if next_page else redirect(
                url_for('main.home'))
        else:
            flash('Login Unsuccessful. Please check email and password.',
                  'danger')
    return render_template('users/login.html', title='Login', form=form)
Пример #2
0
def login():

    # If user is already logged in, return to home page
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    # Set form variable to login form
    form = LoginForm()

    # Only run following code if form response passes checks (in users/forms.py)
    if form.validate_on_submit():

        user = User.query.filter(User.email.ilike(
            f'%{form.email.data}%')).first()  # check for email in db

        # Check that account info is correct, and email is verified
        if user and bcrypt.check_password_hash(
                user.password, form.password.data):  # check password
            if user.confirm_account:  # check email verification
                login_user(user, remember=form.remember.data)  # login user
                prev_page = request.args.get('next')
                return redirect(prev_page) if prev_page else redirect(
                    url_for('main.home'))  # return user to home page
            else:  # if email not yet verified
                flash(
                    f'Account not verified. Please check emails for instructions.',
                    'warning')
        else:  # if login details are incorrect
            flash(f'Login unsuccessful. Please check details.', 'danger')

    return render_template('login.html', title='Login',
                           form=form)  # key variables for .html
Пример #3
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(url_for('main.home'))
        else:
            flash('Login unsuccessful. Please check email and password', 'danger')
    return render_template('login.html', title='Login', form=form)
Пример #4
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            login_user(user, form.remember.data)
            return redirect(url_for('main.home'))
        else:
            flash('Incorrect email or password.', 'danger')
    return render_template('users/login.html', form=form)
Пример #5
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.users'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and sha256_crypt.verify(form.password.data, user.password):
            login_user(user, remember=form.remember.data)
            flash('You are now logged in', 'success')
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(
                url_for('main.home'))
        else:
            flash('Email or Password are wrong', 'danger')
            return redirect(url_for('users.login'))

    return render_template('login.html', title='Login', form=form)
Пример #6
0
def login():
    # Create instance for LoginForm
    form = LoginForm()

    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user:
            # Check if user password equals hashed password when loggin in
            if check_password_hash(user.password, form.password.data):
                login_user(user, remember=form.remember.data)
                # Redirect to dashboard
                return redirect(url_for('users.dashboard'))
        else:
            # Return a flash message if user password is incorrect
            flash("Invalid username or password", "danger")

    return render_template('login.html', form=form)
Пример #7
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user is None or not bcrypt.check_password_hash(
                user.password, form.password.data):
            flash('Invalid User email or Password,Please Check Caps Lock',
                  'Fail')
            return redirect(url_for('users.login'))

        flash('You have successfully login', 'success')
        login_user(user)

        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('main.dashboard')
        return redirect(next_page)
    return render_template('login.html', form=form)
Пример #8
0
def login():
    if current_user.is_authenticated:  # already a user redirect from login to home
        return redirect(url_for('main.home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(
            email=form.email.data).first()  # check user exists from db
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            login_user(user,
                       remember=form.remember.data)  # login user from flask
            next_page = request.args.get(
                'next'
            )  # in case query param in url like account page accessed directly
            return redirect(next_page) if next_page else redirect(
                url_for('main.home'))
        else:
            flash('Login Unsuccessful. Please check email and password',
                  'danger')  # else warning
    return render_template('login.html', title='Login', form=form)