Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var User = require('../model/user');
- var config = require('../config/database');
- var jwt = require('jwt-simple');
- var btc = require('bitcoin-address');
- var smtp = require('../config/smtp');
- var functions = {
- authenticate: function (req, res) {
- User.findOne({
- email: req.body.email
- }, function (err, user) {
- if (err) throw err;
- if (!user) {
- res.status(403).send({
- success: false,
- msg: 'Authentication failed, User not found'
- });
- } else {
- user.comparePassword(req.body.password, function (err, isMatch) {
- if (isMatch && !err) {
- var token = jwt.encode(user, config.secret);
- res.json({success: true, token: token});
- } else {
- return res.status(403).send({
- success: false,
- msg: 'Authenticaton failed, wrong password.'
- });
- }
- })
- }
- })
- },
- addNew: function (req, res) {
- if (!req.body.email) {
- console.log(req.body.email);
- res.json({success: false, msg: 'Enter all values'});
- } else {
- var pass = make_passwd(13, 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890');
- var newUser;
- if (btc.validate(req.body.btc_address)) {
- newUser = User({
- email: req.body.email,
- password: pass,
- btc_address: req.body.btc_address
- });
- } else {
- newUser = User({
- email: req.body.email,
- password: pass,
- btc_address: ''
- });
- }
- console.log(pass);
- smtp.sendMail({
- from: "Sender Name <stefan@plasticjam.com>",
- to: "Recipient Name <"+ req.body.email+">",
- subject: "Your password",
- text: "Here is your password: " + pass
- }, function(error, response){
- if(error){
- console.log(error);
- }else{
- console.log("Message sent: " + response.message);
- }
- });
- newUser.save(function (err) {
- if (err) {
- console.log(err);
- res.json({success: false, msg: 'Failed to save'})
- } else {
- res.json({success: true, msg: 'Successfully saved'});
- }
- })
- }
- },
- getinfo: function (req, res) {
- if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
- var token = req.headers.authorization.split(' ')[1];
- var decodedtoken = jwt.decode(token, config.secret);
- return res.json({
- success: true,
- email: decodedtoken.email,
- btc_address: decodedtoken.btc_address
- });
- }
- else {
- return res.json({success: false, msg: 'No header'});
- }
- }
- };
- var make_passwd = function (n, a) {
- var index = (Math.random() * (a.length - 1)).toFixed(0);
- return n > 0 ? a[index] + make_passwd(n - 1, a) : '';
- }
- module.exports = functions;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement