首页 web前端 js教程 当用户自动登录时,如何自动导航到电子邮件中链接指定的页面?

当用户自动登录时,如何自动导航到电子邮件中链接指定的页面?

Sep 10, 2024 pm 08:30 PM

How can I automatically navigate to a page specified in a link within an email when the user logs in automatically?

以下是代码的详细说明及其工作原理:

邮件发送功能

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

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发送电子邮件。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

@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。
前端提交

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在JavaScript中替换字符串字符 在JavaScript中替换字符串字符 Mar 11, 2025 am 12:07 AM

在JavaScript中替换字符串字符

自定义Google搜索API设置教程 自定义Google搜索API设置教程 Mar 04, 2025 am 01:06 AM

自定义Google搜索API设置教程

示例颜色json文件 示例颜色json文件 Mar 03, 2025 am 12:35 AM

示例颜色json文件

10个jQuery语法荧光笔 10个jQuery语法荧光笔 Mar 02, 2025 am 12:32 AM

10个jQuery语法荧光笔

8令人惊叹的jQuery页面布局插件 8令人惊叹的jQuery页面布局插件 Mar 06, 2025 am 12:48 AM

8令人惊叹的jQuery页面布局插件

构建您自己的Ajax Web应用程序 构建您自己的Ajax Web应用程序 Mar 09, 2025 am 12:11 AM

构建您自己的Ajax Web应用程序

什么是这个'在JavaScript? 什么是这个'在JavaScript? Mar 04, 2025 am 01:15 AM

什么是这个'在JavaScript?

10 JavaScript和JQuery MVC教程 10 JavaScript和JQuery MVC教程 Mar 02, 2025 am 01:16 AM

10 JavaScript和JQuery MVC教程

See all articles