通过电子邮件节点js和Angular 5进行帐户验证

时间:2018-06-26 16:56:20

标签: node.js angular

我正在建立一个SPA,用户在其中注册自己并接收电子邮件。在验证按钮上,单击我从路径中获取令牌,然后尝试将其发送到NodeJs

activation.component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { RestApiService } from '../../rest-api.service';
import { DataService } from '../../data.service';

@Component({
  selector: 'app-activation',
  templateUrl: './activation.component.html',
  styleUrls: ['./activation.component.css']
})
export class ActivationComponent implements OnInit {

  temporarytoken: string;
  public sub: any;

  constructor(public route: ActivatedRoute, private router: Router, private 
  rest: RestApiService, private data: DataService) { }  

  ngOnInit() {
    this.sub =this.route.params.subscribe(params => {
  this.temporarytoken= params['temporarytoken'];
  this.updateAccount();
});

if(this.temporarytoken == null){
  this.router.navigate(['/home']);
}
 }

 async updateAccount(){
   try{
    const data = await this.rest.post(
   'http://localhost:3030/api/accounts/activate', {
     temporarytoken: this.temporarytoken
   },
 );
} catch {
 this.data.error(['message']);
}
}

 }

路由节点JS

router.put('/activate', (req, res, next) => {
   User.findOne({ temporarytoken: req.body.temporarytoken }, (err, user) => {
       if (err) throw err;
      var token = req.body.temporarytoken;

        jwt.verify(token, secret, (err, decoded) => {
        if (err) {
            res.json({ success: false, message: 'Activation Link has Expired' 
    });
        }
        else if (!user) {
            res.json({ success: false, message: 'Activation Link has Expired' 
    });
        }
        else {
            user.temporarytoken = false;
            user.active = true;
            user.save(function (err) {
                if (err) {
                    console.log(err);
                }
                else {
                    nodemailer.createTestAccount((err, account) => {
                        let transporter = nodemailer.createTransport({
                            service: "gmail",
                            debug: true,
                            auth: {
                                user: "*******@gmail.com",
                                pass: "*********"
                            }
                        });
                        let mailOptions = {
                            from: '"Company" <********@gmail.com>""',
                            to: user.email,
                            subject: 'Welcome!!!',
                            text: 'hello account activated',
                            html: 'hello account activated'
                        };
                        transporter.sendMail(mailOptions, (error, info) => {
                            if (error) {
                                return console.log(error);
                            }
                            console.log('Message sent: %s', info.messageId);
                            console.log('emial entered', user.email);
                            console.log('Preview URL: %s', 
                    nodemailer.getTestMessageUrl(info));
                        });
                    });
                }
            });
            res.json({ success: true, message: 'Account Activated!' })
            }
          });
       });
    });

当我尝试传递邮递员的令牌时,会抛出此错误

  

events.js:183         投掷者//未处理的“错误”事件         ^ ReferenceError:机密未定义       在User.findOne(C:\ Users \ manoj \ Desktop \ blue \ server \ routes \ account.js:133:27)       在C:\ Users \ manoj \ Desktop \ blue \ server \ node_modules \ mongoose \ lib \ model.js:3950:16       立即。 (C:\ Users \ manoj \ Desktop \ blue \ server \ node_modules \ mongoose \ lib \ query.js:1516:14)       在即时._onImmediate(C:\ Users \ manoj \ Desktop \ blue \ server \ node_modules \ mquery \ lib \ utils.js:119:16)       在runCallback(timers.js:789:20)       在tryOnImmediate(timers.js:751:5)       在processImmediate [as _immediateCallback](timers.js:722:5)[nodemon]应用程序崩溃-等待文件更改后再开始...

如果秘密被删除,我会收到此错误

  

events.js:183         投掷者//未处理的“错误”事件         ^ JsonWebTokenError:必须提供jwt       在Object.module.exports [作为验证](C:\ Users \ manoj \ Desktop \ blue \ server \ node_modules \ jsonwebtoken \ verify.js:39:17)       在User.findOne(C:\ Users \ manoj \ Desktop \ blue \ server \ routes \ account.js:133:13)       在C:\ Users \ manoj \ Desktop \ blue \ server \ node_modules \ mongoose \ lib \ model.js:3950:16       立即。 (C:\ Users \ manoj \ Desktop \ blue \ server \ node_modules \ mongoose \ lib \ query.js:1516:14)       在即时._onImmediate(C:\ Users \ manoj \ Desktop \ blue \ server \ node_modules \ mquery \ lib \ utils.js:119:16)       在runCallback(timers.js:789:20)       在tryOnImmediate(timers.js:751:5)       在processImmediate [as _immediateCallback](timers.js:722:5)[nodemon]应用程序崩溃-等待文件更改后再开始...

如何将令牌发送到NodeJs

0 个答案:

没有答案
相关问题