如何在 Node js 中从电子邮件重定向到前端页面?

问题描述 投票:0回答:1

我正在用nodejs和vuejs编写确认电子邮件的脚本。因此,当用户注册时,验证电子邮件已发送到用户的电子邮件地址。现在,当用户单击电子邮件中的“验证您的电子邮件”按钮时,它必须重定向到前端页面视图。

问题:重定向不起作用

在验证按钮中,我按以下方式放置了链接:

const confirmTokenUrl = `${req.protocol}://${req.get("host")}/api/confirm-email/confirm-token/${confirmToken}`

sgMail.setApiKey(process.env.SENDGRID_KEY)

const mail = {
    to: req.user.email,
    from: {
      email: process.env.SG_SENDER,
      name: "xyz",
    },
    subject: "Email Confirmation",
    html: `
    <h1>Hello ${req.user.username}!</h1>
    <a style="
        text-decoration: none;
        padding: 15px 30px;
        background-color: #13f195;
        border-radius: 3px;
        font-size: 20px;
        font-weight: bold;
        color: #000;
        "
      href="${confirmTokenUrl}"
      target="_blank"
    >
     Confirm your email
    </a>
    `
}

await sgMail.send(mail)

res.status(200).json({
    success: true
})

现在为了进行验证工作,我编写了另一个端点。我预计当用户单击该电子邮件中的验证按钮时会触发此端点。在该端点的控制器中,我为响应对象提供了重定向符号,如下所示:

res.redirect("http://localhost:3000/login"); //frontend page

现在的问题是它没有重定向到前端页面。现在你能解释一下我缺少什么吗?

javascript node.js redirect sendgrid
1个回答
0
投票

尝试使用“no-cache”标头,否则浏览器将缓存 url 并重定向到前端,而不发出 get 请求来确认电子邮件

 res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max- stale=0, post-check=0, pre-check=0');
 res.writeHead(301,{Location:process.env.CLIENT_URL + /login} )
 res.end()
© www.soinside.com 2019 - 2024. All rights reserved.