Search code examples
javascriptreactjselectronreset-password

Open password reset link from an email in electron


Hi i have a Electron App built with React. My password reset token link is sent by E-mail and that link then opens the browser to reset the users password. If possible i would like that email link to open in the electron app. Unfortuantly I havn't found anything helpful as of yet and i dont know where to even start. Any infomation would be greatly appreciated.

this is my function sending a token link to the users email if it helps

app.post('/forgotPassword', async function(req, res){
    const name = req.body.name;
    const mail = req.body.email;
    crypto.randomBytes(32, (err, buffer)=>{
      if(err){
        console.log(err)
      }
      console.log(buffer)
      const token = buffer.toString("hex")
      console.log(token) 

    User.findOne({userName: name, email: mail})
      .then(user =>{
        if(!user){
          return res.json({
            status: 404,
            message: "No user found with Entered User name and email"
          })
        }
        user.resetToken = token
        user.expireToken = Date.now() + 1800000
        user.save().then((result)=>{
          transporter.sendMail({
            from: process.env.EMAIL,
            to: mail,
            subject: "Password Reset",
            html: `
              <p>Your requested password reset</p>
              <h5>Click on this <a href="http://localhost:3000/${token}">link<a/> to reset password</h5>
            `
          })
          res.json({
            status: 200,
            message: "Password Reset email Sent please check your inbox"
          })
        })
      })
    })
  })

In summary: I want the users password reset link (sent by email) to open in electron not the browser

Thanks in advance :)


Solution

  • since you want a link from the users mail client (browser, outlook, ...) to open your electron app, you have register your app as a "opener for that type of link" on the operating system level.

    you might need to define a custom protocol for that like myapp://password-reset-link or something similar (because you don't want all http links to be opened with your electron app).

    I have not done this myself yet, but I think this link looks promising https://glebbahmutov.com/blog/electron-app-with-custom-protocol/