<commit_before>import koa_router from "koa-router";
import koa_body from "koa-body";
import request from "co-request";
import React from "react";
import { renderToString } from "react-dom/server";
import models from "db/models";
import ServerHTML from "../server-html";
import sendEmail from "../sendEmail";
import { checkCSRF, getRemoteIp } from "server/utils/misc";
import config from "config";
import SignupProgressBar from "app/components/elements/SignupProgressBar";
import MiniHeader from "app/components/modules/MiniHeader";
import secureRandom from "secure-random";
import Mixpanel from "mixpanel";
import tt from 'counterpart';

// FIXME copy paste code, refactor mixpanel out
var mixpanel = null;
if (config.has("mixpanel") && config.get("mixpanel")) {
    mixpanel = Mixpanel.init(config.get("mixpanel"));
}

var assets_file = "tmp/webpack-stats-dev.json";
if (process.env.NODE_ENV === "production") {
    assets_file = "tmp/webpack-stats-prod.json";
}

const assets = Object.assign({}, require(assets_file), { script: [] });

assets.script.push("https://www.google.com/recaptcha/api.js");
// assets.script.push("/enter_email/submit_form.js");

function* confirmEmailHandler() {
    const confirmation_code = this.params && this.params.code
        ? this.params.code
        : this.request.body.code;
    console.log(
        "-- /confirm_email -->",
        this.session.uid,
        this.session.user,
        confirmation_code
    );
    const eid = yield models.Identity.findOne({
        attributes: ["id", "user_id", "email", "updated_at", "verified"],
        where: { confirmation_code, provider: "email" },
        order: "id DESC"
    });
    if (!eid) {
        this.status = 401;
        this.body = "confirmation code not found";
        return;
    }
    if (eid.verified) {
        this.session.user = eid.user_id; // session recovery (user changed browsers)
        this.flash = { success: "Email has already been verified" };
        this.redirect("/enter_mobile");
        return;
    }
    const hours_ago = (Date.now() - eid.updated_at) / 1000.0 / 3600.0;
    if (hours_ago > 24.0 * 10) {
        eid.destroy();
        this.status = 401;
        this.body = '<!DOCTYPE html>Confirmation code expired.  Please <a href="/enter_email">re-submit</a> your email for verification.';
        return;
    }
    this.session.user = eid.user_id;
    yield eid.update({ verified: true });
    yield models.User.update({ email: eid.email, waiting_list: false }, {
        where: { id: eid.user_id }
    });

    // check if the phone is confirmed then redirect to create account - this is useful when we invite users and send them the link
    const mid = yield models.Identity.findOne({
        attributes: ["verified"],
        where: { user_id: eid.user_id, provider: "phone" },
        order: "id DESC"
    });
    if (mid && mid.verified) {
        this.redirect("/create_account");
    } else {
        this.redirect("/enter_mobile");
    }
    this.redirect('/create_account');
}

export default function useEnterAndConfirmEmailPages(app) {
    const router = koa_router();
    app.use(router.routes());
    const koaBody = koa_body();
    const rc_site_key = config.get('recaptcha.site_key');

// <<<<<<< HEAD
//     router.get("/enter_email", function*() {
//         console.log("-- /enter_email -->", this.session.uid, this.session.user);
//         this.session.user = null;
//         let default_email = "";
//         if (this.request.query && this.request.query.email)
//             default_email = this.request.query.email;
//         const body = renderToString(
//             <div className="App">
//                 <MiniHeader />
//                 <SignupProgressBar
//                     steps={["email", "phone", "steem account"]}
//                     current={1}
//                 /><commit_msg>Create account after verify email<commit_after>import koa_router from "koa-router";
import koa_body from "koa-body";
import request from "co-request";
import React from "react";
import { renderToString } from "react-dom/server";
import models from "db/models";
import ServerHTML from "../server-html";
import sendEmail from "../sendEmail";
import { checkCSRF, getRemoteIp } from "server/utils/misc";
import config from "config";
import SignupProgressBar from "app/components/elements/SignupProgressBar";
import MiniHeader from "app/components/modules/MiniHeader";
import secureRandom from "secure-random";
import Mixpanel from "mixpanel";
import tt from 'counterpart';

// FIXME copy paste code, refactor mixpanel out
var mixpanel = null;
if (config.has("mixpanel") && config.get("mixpanel")) {
    mixpanel = Mixpanel.init(config.get("mixpanel"));
}

var assets_file = "tmp/webpack-stats-dev.json";
if (process.env.NODE_ENV === "production") {
    assets_file = "tmp/webpack-stats-prod.json";
}

const assets = Object.assign({}, require(assets_file), { script: [] });

assets.script.push("https://www.google.com/recaptcha/api.js");
// assets.script.push("/enter_email/submit_form.js");

function* confirmEmailHandler() {
    const confirmation_code = this.params && this.params.code
        ? this.params.code
        : this.request.body.code;
    console.log(
        "-- /confirm_email -->",
        this.session.uid,
        this.session.user,
        confirmation_code
    );
    const eid = yield models.Identity.findOne({
        attributes: ["id", "user_id", "email", "updated_at", "verified"],
        where: { confirmation_code, provider: "email" },
        order: "id DESC"
    });
    if (!eid) {
        this.status = 401;
        this.body = "confirmation code not found";
        return;
    }
    if (eid.verified) {
        this.session.user = eid.user_id; // session recovery (user changed browsers)
        this.flash = { success: "Email has already been verified" };
        // this.redirect("/enter_mobile");
        this.redirect('/create_account');
        return;
    }
    const hours_ago = (Date.now() - eid.updated_at) / 1000.0 / 3600.0;
    if (hours_ago > 24.0 * 10) {
        eid.destroy();
        this.status = 401;
        this.body = '<!DOCTYPE html>Confirmation code expired.  Please <a href="/enter_email">re-submit</a> your email for verification.';
        return;
    }
    this.session.user = eid.user_id;
    yield eid.update({ verified: true });
    yield models.User.update({ email: eid.email, waiting_list: false }, {
        where: { id: eid.user_id }
    });

    // check if the phone is confirmed then redirect to create account - this is useful when we invite users and send them the link
    // const mid = yield models.Identity.findOne({
    //     attributes: ["verified"],
    //     where: { user_id: eid.user_id, provider: "phone" },
    //     order: "id DESC"
    // });
    // if (mid && mid.verified) {
    //     this.redirect("/create_account");
    // } else {
    //     this.redirect("/enter_mobile");
    // }
    this.redirect('/create_account');
}

export default function useEnterAndConfirmEmailPages(app) {
    const router = koa_router();
    app.use(router.routes());
    const koaBody = koa_body();
    const rc_site_key = config.get('recaptcha.site_key');

// <<<<<<< HEAD
//     router.get("/enter_email", function*() {
//         console.log("-- /enter_email -->", this.session.uid, this.session.user);
//         this.session.user = null;
//         let default_email = "";
//         if (this.request.query && this.request.query.email)
//             default_email = this.request.query.email;
//         const body = renderToString(
//             <div className="App">
//                 <MiniHeader />
//                 <SignupProgressBar
//                     steps={["email", "phone", "steem account"]}
//                     current={1}
//                 />
{'fraction_non_alphanumeric': 0.15111054050584158, 'fraction_numerical': 0.005648992168442676, 'mean_word_length': 2.6452971455311185, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3423242', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2322, 'n_words': 557}