verifyEmail.js 3.42 KB
import React                              from 'react';
import _                                  from 'lodash';
import { Users }                          from '/imports/collections/users/index';




const finishWithMessage = (res, message, redirect = "/") => {

  const output = `
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style type="text/css">

.congo-wrap .header {
    background: white;
    height: 50px;
}

.brand-style
{
  font-family: 'Ubuntu', sans-serif !important;
  color:#00b395;
}

.congo-wrap .header .container {
    width: 100%;
}
.congo-wrap{
    background:#00b395;
    height:100%;
}
.widgetbox-congrats {
  margin: 20px auto;
  width: 400px;
  height: auto;
  padding: 20px;
  -webkit-border-radius: 8px/7px;
  -moz-border-radius: 8px/7px;
  border-radius: 8px/7px;
  background-color: white;
  -webkit-box-shadow: 1px 2px 5px rgba(0,0,0,.31);
  -moz-box-shadow: 1px 2px 5px rgba(0,0,0,.31);
  box-shadow: 1px 2px 5px rgba(0,0,0,.31);
  border: solid 1px #cbc9c9;
  font-family: 'Ubuntu', sans-serif;
}

body {
    margin: 0;
    padding: 0;
}


.text-blue {
    color: #00b395;
}

.widgetbox-congrats .congo-msg h4 {
    margin: 8px 0;
    font-size: 24px;
    text-align:center;
}

.widgetbox-congrats .congo-msg .icon {
   color: #00b395;
    font-size: 90px;
    display: inline-block;
    width: 100%;
    text-align: center;
}

p
{
  display: inline-block;
  width: 100%;
  text-align: center;
}

.widgetbox-congrats .widgetbox-footer .btn {
    background: #00b395;
    color: #fff;
    text-transform: uppercase;
    display: inline-block;
    width: 100%;
    text-align: center;
    font-family: 'Ubuntu', sans-serif;s
}
</style>

</head>

<body>
<div class="congo-wrap">
<div class="header">
    <div class="container">
          <span class="brand-style">YOUNGDESK</span>
    </div>
</div>
    <div class="container">
        <div class="widgetbox-congrats">
            <div class="congo-msg">
                <div class="text-blue">
                    <i class="icon icon-simple icon-check"></i>
                    <h4>Congrats!</h4>
                </div>
                <p> ${message}</p>
            </div>
            <div class="widgetbox-footer">
                <a href="${redirect}" class="btn btn-md btn-prmary">Continue</a>
            </div>

        </div>
    </div>
</div>

</html>
  `;

  res.writeHead(200, {
    'Content-Length': output.length,
    'Content-Type': 'text/html',
  });
  res.end(output);
};

Picker.route('/back/verifyEmail/:token', function(params, req, res, next) {

  const user = Users.findOne({'services.email.verificationTokens.token': params.token});
  if(!user) return finishWithMessage(res, 'Invalid or outdated token.');

  const token = _.find(user.services.email.verificationTokens, x => x.token === params.token);
  if(new Date().getTime() - token.when.getTime() > 2 * 24 * 60 * 60 * 1000) return finishWithMessage(res, 'Invalid or outdated token.');

  finishWithMessage(res, 'Email has been verified.',"/");
  const idx = _.findIndex(user.emails, x => x.address === token.address);

  Users.update({_id: user._id}, {$set: {
    [`emails.${idx}.verified`]: true,
  }});

});