Beispiel #1
0
def login_route():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')

        if not UserService.authenticate(username, password):
            flash('Invalid user key or password. Please try again.', 'danger')
            current_app.logger.warn(
                "Invalid credentials attempted for username '{0}'.".format(
                    username))

            return redirect(url_for('auth.login_route'))

        # Find or create user
        user = UserService.find_by(username=username, fetch_one=True)
        if user:
            UserService.update(user.id, authenticated=True)
        else:
            user = UserService.create(username=username, authenticated=True)

        # Log in user in Flask
        login_user(user)

        # Go to next page if defined
        next_page = session.pop('next') or request.args.get('next')
        if next_page:
            return redirect(next_page)

        return redirect(url_for('auth.login_route'))
    else:
        if current_user.is_authenticated:
            return redirect(url_for('user.me_route'))
        else:
            return render_template('pages/auth/authenticate.html')
 def authenticate_user(self, username, password):
     user = UserService.find_by(username=username, fetch_one=True)
     if user and UserService.authenticate(username, password):
         return user