Commit c3bcb380 authored by Vytor Calixto's avatar Vytor Calixto 👾
Browse files

Merge pull request #4 from PETComputacaoUFPR/email-verification

Adicionado verificação de e-mail ao criar usuário
parents 45198859 6897a53f
......@@ -22,6 +22,10 @@
"name": "Farol - Repositório de Provas",
"clientId": "farol",
"clientSecret": "KeepItSecret"
},
"email": {
"user": "petcomputacaoufpr",
"password": ""
}
}
}
......@@ -41,6 +41,10 @@ var Usuario = new Schema({
bloqueado: {
type: Boolean,
default: false
},
verificado: {
type: Boolean,
default: false
}
})
......
var mongoose = require('mongoose')
var Schema = mongoose.Schema
var Usuario = require('./usuario')
var uuid = require('node-uuid')
var VerificationToken = new Schema({
userId: {
type: Schema.Types.ObjectId,
required: true,
ref: 'Usuario'
},
token: {
type: String,
required: true
},
createdAt: {
type: Date,
required: true,
default: Date.now,
expires: '4h'
}
})
VerificationToken.methods.createVerificationToken = function(done) {
var verificationToken = this
var token = uuid.v4()
verificationToken.set('token', token)
verificationToken.save(function(err) {
if (err) return done(err)
console.log('Verification Token', verificationToken)
return done(null, token)
})
}
module.exports = mongoose.model('VerificationToken', VerificationToken)
var express = require('express')
var passport = require('passport')
var router = express.Router()
var email = require('emailjs')
var libs = process.cwd() + '/libs/'
var db = require(libs + 'db/mongoose')
var Usuario = require(libs + 'model/usuario')
var VerificationToken = require(libs + 'model/verificationToken')
var role = require(libs + 'role')
var config = require(libs + 'config')
var server = email.server.connect({
user: config.get('default:email:user'),
password: process.env.EMAILPASSWORD || config.get('default:email:password'),
host: 'smtp.gmail.com',
ssl: true
})
router.get('/', passport.authenticate('bearer', { session: false }), role.isModerador(), function(req, res) {
Usuario.find()
......@@ -38,6 +48,26 @@ router.get('/me', passport.authenticate('bearer', { session: false }), function(
})
})
router.get('/verify/:token', function(req, res) {
var token = req.params.token
VerificationToken.findOne({token: token}, function(err, vToken) {
if(err) {
console.log(err)
return res.json({error: 'Email verification failed'})
}
Usuario.findOne({_id: vToken.userId}, function(err, usuario) {
usuario.verificado = true
usuario.save(function(err) {
if(err) {
console.log(err)
return res.json({error: 'Email verification failed. Failed to update user'})
}
return res.json({status: 'OK'})
})
})
})
})
router.put('/me/edit', passport.authenticate('bearer', { session: false }), function(req, res) {
Usuario.findById(req.user.userId, function(err, usuario) {
if(!usuario) {
......@@ -105,6 +135,25 @@ router.post('/', function(req, res) {
usuario.save(function (err) {
if(!err) {
var verificationToken = new VerificationToken({
userId: usuario._id
})
verificationToken.createVerificationToken(function(err, token) {
if (err) {
console.log('Não foi possível criar o token')
}
var message = {
text: req.protocol + '://' + req.get('host') + '/v1/u/verify/' + token,
from: 'PET Computação UFPR <pet@inf.ufpr.br>',
to: usuario.nome + ' <' + usuario.email + '>',
subject: 'Farol - Confirmação de Conta'
}
server.send(message, function(err, message) {
console.log(err || message)
})
})
return res.json({status: 'OK', usuario:usuario})
} else {
if(err.name === 'ValidationError') {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment