当用户自动登录时,如何自动导航到电子邮件中链接指定的页面?
以下是代码的详细说明及其工作原理:
邮件发送功能
async sendingEmail(email, ndfCollabName, message, numero, intitulé, Url) { const transporter = nodemailer.createTransport({ host: 'smtp.office365.com', port: 587, secure: false, auth: { user: process.env.USER_EMAIL, pass: process.env.USER_PASS, }, }); const mailOptions = { from: 'fromEmail@gamil.com', to: email, subject: '', html: ` <!DOCTYPE html> <html lang="fr"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>test</title> <style> body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 600px; margin: 0 auto; padding: 20px; } h1 { color: #007bff; } h2 { color: #555; } .links { margin-top: 20px; } .links a { display: block; margin-bottom: 10px; color: #007bff; } </style> </head> <body> <h1>Cher/Chère ${ndfCollabName.toUpperCase()},</h1> <h2>${message}, N° ${numero}, ${intitulé}.</h2> <div class="links"> <a href="http://localhost:4000/?redirect=${encodeURIComponent(Url)}">Lien local</a> </div> <h2>Vérifiez ici?</h2> </body> </html> `, }; transporter.sendMail(mailOptions, function (error, info) { if (error) { console.log(error); } else { console.log('Email sent:' + info.response); } }); }
说明:
设置传输器:
配置 Nodemailer 使用 Office365 SMTP 发送电子邮件。
邮件选项:
设置电子邮件的主题和 HTML 正文,包括带有重定向查询参数的链接。
编码网址:
使用encodeURIComponent 对 Url 进行安全编码,以便包含在电子邮件链接中。
发送邮件:
使用transporter.sendMail发送电子邮件。
@Post('login') async login( @Body('id') id: string, @Body('password') password: string, @Body('company') company: Company, @Body('redirect') redirect: string, @Res({ passthrough: true }) response: Response, ) { const user = await this.collaborateursService.find({ where: { nomtechnicien: id }, relations: [ 'companies', 'roles.role', 'roles.company', 'groups', 'groupe', ], }); if (!user) { throw new BadRequestException('Invalid credentials'); } if (!(await bcrypt.compare(password, user.password))) { throw new BadRequestException('Invalid credentials'); } if (!user.lastconnectedcompany) { await this.collaborateursService.lastConnectedCompany(user.id, user.companies[0]); } const jwt = await this.jwtService.signAsync({ id: user.id, name: user.nomtechnicien, lastconnectedcompany: user.lastconnectedcompany || user.companies[0].name, rolesByCompanies: user.rolesByCompanies, groups: user.groups!, company: user.companies, companyId: user.companies.filter((company) => user.lastconnectedcompany ? company.name == user.lastconnectedcompany : company.name == user.companies[0].name, )[0].identifiantBc, }); response.cookie('jwt', jwt, { httpOnly: true }); delete user.password; return { message: 'success', user, redirect: redirect ? decodeURIComponent(redirect) : null, }; }
查找用户:
通过 ID 检索用户并检查凭据。
生成 JWT:
创建 JWT 令牌并将其设置为 cookie。
解码重定向 URL:
从请求正文中解码重定向参数。
返回响应:
返回成功消息和解码后的重定向 URL。
前端提交
const submit = async () => { setIsLoading(true); try { const res = await empLogin({ id: id, password: pass, redirect: redirectUrl }); console.log(res); if (res.message === "success") { dispatch(login()); // Navigate to the redirect URL provided in the response navigate(res.redirect); } } catch (error) { console.log(error); setError(true); } finally { setIsLoading(false); } };
提交登录:
发送带有 ID、密码和重定向 URL 的登录请求。
处理响应:
如果登录成功,则导航到响应的重定向字段中提供的 URL。
错误处理:
捕获并记录过程中的任何错误。
此设置可确保用户登录时自动重定向到电子邮件链接中指定的 URL。
以上是当用户自动登录时,如何自动导航到电子邮件中链接指定的页面?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

从C/C 转向JavaScript需要适应动态类型、垃圾回收和异步编程等特点。1)C/C 是静态类型语言,需手动管理内存,而JavaScript是动态类型,垃圾回收自动处理。2)C/C 需编译成机器码,JavaScript则为解释型语言。3)JavaScript引入闭包、原型链和Promise等概念,增强了灵活性和异步编程能力。

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

Python和JavaScript在开发环境上的选择都很重要。1)Python的开发环境包括PyCharm、JupyterNotebook和Anaconda,适合数据科学和快速原型开发。2)JavaScript的开发环境包括Node.js、VSCode和Webpack,适用于前端和后端开发。根据项目需求选择合适的工具可以提高开发效率和项目成功率。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。
