Пример #1
0
def request_password_reset(**kwds):
    """
    Request a password reset.
    """

    email = kwds.get("username")
    if email:
        user = User.query.filter_by(email=email).first()
        if not user:
            reason = "User not found."
            status_code = 404
        else:
            reason = "A password reset request has been sent."
            status_code = 200
            token = create_access_token(
                email,
                expires_delta=timedelta(hours=2),
                user_claims={"token_password_reset": True},
            )
            link_reset = url_for(".password_reset", token=token)
            msg = get_mail_template(
                "request_password_reset.html", link_reset=link_reset
            )
            send_email.delay("Forgot password", html=msg, recipients=[email])
    else:
        reason = "Email required."
        status_code = 403

    return {"reason": reason, "status_code": status_code}
Пример #2
0
def request_password_reset(kwds):
    """
    Request a password reset.
    """

    email = kwds.get("username")
    if email:
        user = User.query.filter_by(email=email).first()
        if not user:
            message = "User not found."
            status_code = 404
        else:
            message = "A password reset request has been sent."
            status_code = 200
            token = create_access_token(
                user.id,
                expires_delta=timedelta(hours=2),
                user_claims={"token_password_reset": True},
            )
            msg = get_mail_template("forgot_password.html", token=token)
            send_email.delay("Forgot password", html=msg, recipients=[email])
            log = Log(date_requested_password_reset=datetime.utcnow())
            user.logs.append(log)
            db_commit()
            on_forgot_password.send(user)
    else:
        message = "Email required."
        status_code = 403

    return {"message": message, "code": status_code}
Пример #3
0
def register(**kwds):
    """
    Register an account.
    """

    email = kwds.get("username")
    passw = kwds.get("password")
    first_name = kwds.get("first_name")
    last_name = kwds.get("last_name")
    username = first_name + " " + last_name
    if email:
        user = User.query.filter_by(email=email).first()
        if not user:
            if username and passw:
                passw = generate_password_hash(passw)
                user = User(
                    first_name=first_name,
                    last_name=last_name,
                    name=username,
                    email=email,
                    password=passw,
                    register_at=datetime.utcnow(),
                )
                db_add(user)
                token = create_access_token(
                    email,
                    expires_delta=False,
                    user_claims={"token_registration": True},
                )
                link_confirm = url_for(".confirm_account", token=token)
                msg = get_mail_template("register.html", link_confirm=link_confirm)
                send_email.delay("Registration", html=msg, recipients=[email])
                reason = "Successful registration."
                status_code = 200
            else:
                reason = "Username and password are required."
                status_code = 403
        else:
            reason = "Email already exists."
            status_code = 403
    else:
        reason = "Email required."
        status_code = 403

    return {"reason": reason, "status_code": status_code}