コード例 #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()  #Returns None
        # if no user is found
        if user and bcrypt.check_password_hash(
                user.password, form.password.data):  #If user is
            # found and the passwords match
            login_user(user, remember=form.remember.data)  #If the user checked
            # the Remember Me box, that information is stored in form.remember
            next_page = request.args.get(
                'next')  #This allows us to redirect to
            # the intended page after being forced to login
            # request.args is a dictionary, so using the method .get() returns
            # the key if it is present but returns None if not. This means no
            # KeyError is raised if the key (next) is not in the dictionary
            return redirect(next_page) if next_page else redirect(
                url_for('main.home'))  #Ternary conditional
        else:
            flash('Incorrect Login. Please check email and password', 'danger')

    return render_template('login.html', title='Login', form=form)
コード例 #2
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.share'))
    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.index'))
        else:
            flash('Login Unsuccessful. Please check email and password', 'danger')
    return render_template('login.html', title='Login', form=form)