Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
speckle now can send emails 🎉 via nodemailer dep, and SMTP provider o…
Browse files Browse the repository at this point in the history
…f choice; .env-base updated with extra config settings for this
  • Loading branch information
didimitrie committed Jul 17, 2019
1 parent e1e34a4 commit 08ed881
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 29 deletions.
13 changes: 13 additions & 0 deletions .env-base
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ EXPOSE_EMAILS=false

# The first user to register gets an admin role.
FIRST_USER_ADMIN=true

# SMTP server
# This section is used to send emails out. You can use the provider of your choice,
# as long as it exposes an SMTP server that you can use.
SMTP_HOST=""
SMTP_PORT=587
SMPT_USERNAME=""
SMPT_PASSWORD=""

# Will populate the `from:` field in any emails this server sends. Please note, some
# providers will require to verify your domain first.
EMAIL_SENDER=""

7 changes: 5 additions & 2 deletions app/api/accounts/UserCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const cryptoRandomString = require( 'crypto-random-string' )
const User = require( '../../../models/User' )
const ActionToken = require( '../../../models/ActionToken' )

const SendVerificationEmail = require( '../../../app/email/index' ).SendEmailVerification

module.exports = function ( req, res ) {
winston.debug( 'register new user route' )
if ( !req.body.email ) { res.status( 400 ); return res.send( { success: false, message: 'Do not fuck with us. Give us your email.' } ) }
Expand Down Expand Up @@ -44,9 +46,10 @@ module.exports = function ( req, res ) {
} )
.then( user => {
savedUser = user
return validationToken.save()
})
return validationToken.save( )
} )
.then( result => {
let verfication = SendVerificationEmail( { name: savedUser.name, email: savedUser.email, token: validationToken.token } )
let token = 'JWT ' + jwt.sign( { _id: myUser._id, name: myUser.name }, sessionSecret, { expiresIn: '24h' } )
return res.send( { success: true, message: 'User saved. Redirect to login.', resource: { apitoken: savedUser.apitoken, token: token, email: savedUser.email }, validationToken: res.token } )
} )
Expand Down
4 changes: 1 addition & 3 deletions app/api/accounts/UserVerify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const User = require( '../../../models/User' )

module.exports = function ( req, res ) {
winston.debug( 'TODO: verify email route' )
if ( !req.body.email ) { res.status( 400 ); return res.send( { success: false, message: 'Do not fuck with us. Give us your email.' } ) }
if ( !req.body.password ) { res.status( 400 ); return res.send( { success: false, message: 'Passwords are a necessary evil, fam.' } ) }

return res.status(404).send('not implemented yet')
return res.status( 404 ).send( `not implemented yet (token: ${req.params.token})` )
}
5 changes: 4 additions & 1 deletion app/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ module.exports = function ( app, express, urlRoot, plugins ) {
r.post( '/accounts/search', mandatoryAuthorisation, require( './accounts/UserSearch' ) )

// verify
r.post( '/accounts/verify', require( './accounts/UserVerify' ) )
r.get( '/accounts/verify/:token', optionalAuthorisation, require( './accounts/UserVerify' ) )

// reset password
r.post( '/accounts/reset/:token', require( './accounts/UserVerify' ) )

//
// CLIENTS
Expand Down
93 changes: 93 additions & 0 deletions app/email/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict'
const nodemailer = require( 'nodemailer' )
const winston = require( '../../config/logger' )

let initOk = false

let transporter = nodemailer.createTransport( {
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
auth: {
user: process.env.SMPT_USERNAME,
pass: process.env.SMPT_PASSWORD,
}
} )

transporter.verify( ( err, success ) => {
if ( err )
winston.debug( err )
else {
initOk = true
winston.debug( 'Email service is intialised.' )
}
} )


exports.SendEmailVerification = ( { name, email, token } ) => {

let message = {
from: process.env.EMAIL_SENDER,
to: email,
subject: `Speckle Password reset for your account on ${process.env.SERVER_NAME}`,
text: `
Hello ${name},
Please verify your speckle account on ${process.env.SERVER_NAME} (${process.env.CANONICAL_URL}) by clicking on the following link:
${process.env.CANONICAL_URL}/api/accounts/verify/${token}
If any problems, do get in touch via the forum (https://discourse.speckle.works).
Best regards,
Speckle
---
Speckle is the open source data platform for AEC.
> https://speckle.works
> https://twitter.com/speckle_works
`,
html: `
Hello ${name},
<br>
<br>
We're happy you're on board! There's just a bit of house-keeping to take care of. Complete your registration by verifying your speckle account on ${process.env.SERVER_NAME} (${process.env.CANONICAL_URL}) by clicking on <a href="${process.env.CANONICAL_URL}/api/accounts/verify/${token}">this link.</a> If that doesn't work, copy paste it in your browser of choice:
<br>
<br>
<pre>${process.env.CANONICAL_URL}/api/accounts/verify/${token}</pre>
<br>
<br>
If you have any problems or questions, do get in touch via <a href="https://discourse.speckle.works">the forum</a>.
<br>
<br>
Best regards,<br>
Speckle
<br>
---<br>
<img src="https://speckle.systems/speckle-min.png">
<br>
Speckle is the open source data platform for architecture, engineering, and construction.
<br>
> https://speckle.works <br>
> https://twitter.com/speckle_works <br>
`
}

transporter.sendMail( message, ( err ) => {
if(err) {
winston.debug( 'OUPS ERRROROR')
console.log( err )
winston.debug( err )
} else {
winston.debug( 'email sent?')
winston.debug( message )
}
} )
}

exports.SendPasswordReset = ( token ) => {

}

exports.default = {}
1 change: 1 addition & 0 deletions config/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const logger = createLogger( {
level: 'debug',
format: format.combine(
format.timestamp( { format: 'YYYY-MM-DD HH:mm:ss' } ),
format.errors( { stack: true } ),
format.json( )
),
transports: [
Expand Down
87 changes: 65 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"jsonwebtoken": "^8.4.0",
"lodash": "^4.17.11",
"mongoose": "^5.3.4",
"nodemailer": "^6.3.0",
"passport": "^0.4.0",
"passport-anonymous": "^1.0.1",
"passport-jwt": "^4.0.0",
Expand All @@ -34,7 +35,7 @@
"redis": "^2.8.0",
"shortid": "^2.2.13",
"uuid": "^3.3.2",
"winston": "^3.0.0",
"winston": "^3.2.1",
"winston-daily-rotate-file": "^3.5.1",
"ws": "^6.1.2"
},
Expand Down
4 changes: 4 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ if ( cluster.isMaster ) {
require( './app/api/index' )( app, express, '/api', plugins )
require( './app/api/index' )( app, express, '/api/v1', plugins )


// init email transport
require( './app/email/index' )

/// /////////////////////////////////////////////////////////////////////
/// LAUNCH /////.
/// /////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 08ed881

Please sign in to comment.