You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
38 lines
1.1 KiB
from typing import Optional
|
|
|
|
from flask import blueprints
|
|
from flask import make_response
|
|
from flask import request
|
|
from flask_api import status
|
|
from flask_login import login_user
|
|
from monsun_backend import error
|
|
from monsun_backend import marshmallow_schemas
|
|
from monsun_backend import models
|
|
|
|
bp = blueprints.Blueprint("login", __name__)
|
|
|
|
|
|
@bp.route(
|
|
rule="/login",
|
|
methods=["POST"],
|
|
)
|
|
def login():
|
|
email_: Optional[str] = request.form.get("email")
|
|
if email_ is None:
|
|
raise error.BadRequest("email is missing")
|
|
|
|
user_: models.User = models.User.query.filter_by(email=email_).first()
|
|
if not user_:
|
|
raise error.BadRequest("User does not exist")
|
|
|
|
if user_.check_password(password=request.form.get("password")):
|
|
if login_user(user_):
|
|
return make_response(
|
|
marshmallow_schemas.user_schema.jsonify(user_),
|
|
status.HTTP_200_OK,
|
|
)
|
|
else:
|
|
# this shouldn't happen, but one never knows...
|
|
raise error.InternalServerError("Could not login user")
|
|
else:
|
|
raise error.Unauthorized("Password is incorrect")
|
|
|