當使用者自動登入時,如何自動導覽至電子郵件中連結指定的頁面?
以下是程式碼的詳細說明及其運作方式:
郵件發送功能
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)

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

探索前端中類似VSCode的面板拖拽調整功能的實現在前端開發中,如何實現類似於VSCode...
