Beispiel #1
0
def activate():
    if current_user.is_authenticated:
        return (
            render_template("auth/activate.html",
                            error="You are already logged in"),
            400,
        )

    code = request.args.get("code")

    activation_code: ActivationCode = ActivationCode.get_by(code=code)

    if not activation_code:
        # Trigger rate limiter
        g.deduct_limit = True
        return (
            render_template("auth/activate.html",
                            error="Activation code cannot be found"),
            400,
        )

    if activation_code.is_expired():
        return (
            render_template(
                "auth/activate.html",
                error="Activation code was expired",
                show_resend_activation=True,
            ),
            400,
        )

    user = activation_code.user
    user.activated = True
    login_user(user)

    # activation code is to be used only once
    ActivationCode.delete(activation_code.id)
    Session.commit()

    flash("Your account has been activated", "success")

    email_utils.send_welcome_email(user)

    # The activation link contains the original page, for ex authorize page
    if "next" in request.args:
        next_url = sanitize_next_url(request.args.get("next"))
        LOG.d("redirect user to %s", next_url)
        return redirect(next_url)
    else:
        LOG.d("redirect user to dashboard")
        return redirect(url_for("dashboard.index"))
Beispiel #2
0
def send_activation_email(user, next_url):
    # the activation code is valid for 1h
    activation = ActivationCode.create(user_id=user.id, code=random_string(30))
    db.session.commit()

    # Send user activation email
    activation_link = f"{URL}/auth/activate?code={activation.code}"
    if next_url:
        LOG.d("redirect user to %s after activation", next_url)
        activation_link = activation_link + "&next=" + encode_url(next_url)

    email_utils.send_activation_email(user.email, user.name, activation_link)