Last active
April 8, 2020 17:58
-
-
Save Porter97/cf667ce318c26d28ab7c617ede890538 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#... | |
from flask_login import login_user, login_required, \ | |
current_user, logout_user | |
from .forms import RegistrationForm, LoginForm | |
from .errors import forbidden | |
#... | |
@auth.route('/login', methods=['GET', 'POST']) | |
def login(): | |
form = LoginForm() | |
if form.validate_on_submit(): | |
user = User.query.filter_by(email=form.email.data.lower()).first() | |
if user is not None and user.verify_password(form.password.data): | |
login_user(user, False) | |
return {'user': current_user.to_json()}, 200 | |
return forbidden('Invalid Credentials') | |
else: | |
return {'errors': {fieldName.title(): errorMessages for fieldName, errorMessages in form.errors.items()}}, 400 | |
@auth.route('/logout') | |
@login_required | |
def logout(): | |
logout_user() | |
return {"success": 200} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment