0

I am trying to develop a password reset webpage to use in my email/password authentication for MongoDB Stitch, this is my code:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <!-- Stitch JavaScript SDK -->
    <!-- Base Stitch Browser SDK -->
    <script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.0.0/stitch.js"></script>
    <script>// Parse the URL query parameters
      const url = window.location.search;
      const params = new URLSearchParams(url);

      const token = params.get('token');
      const tokenId = params.get('tokenId');
      const newPassword = "test";

      // Confirm the user's email/password account
      const emailPassClient = Stitch.defaultAppClient.auth
        .getProviderClient(UserPasswordAuthProviderClient.factory);

      emailPassClient.resetPassword(token, tokenId, newPassword).then(() => {
        console.log("Successfully reset password!");
      }).catch(err => {
        console.log("Error resetting password:", err);
      });
    </script>
  </body>
</html>

I am getting an error like the following: Uncaught ReferenceError: Stitch is not defined Perhaps I am importing the stitch bundles incorrectly?

Robbie Cronin
  • 1,557
  • 2
  • 10
  • 15
  • 1
    is your code executing before the stitch script has loaded? – Bibberty Mar 07 '19 at 06:04
  • Check here: https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState – Bibberty Mar 07 '19 at 06:06
  • Check the README for the package: https://www.npmjs.com/package/mongodb-stitch-browser-sdk It clearly demonstrates use of `window.onload` and wrapping the logic in a handler function. Also demonstrates other packaging techniques. – Neil Lunn Mar 07 '19 at 06:10
  • 1
    I copied this code snippet from MongoDB Stitch docs: https://docs.mongodb.com/stitch/authentication/userpass/#auth-userpass-configuration. Looks like they had a capital 'S' when they referenced Stitch, this was the root of the error – Robbie Cronin Mar 07 '19 at 06:24

2 Answers2

1

Call stitch.Stitch.initializeDefaultAppClient(ID)

Note that you have to initialize a default app client, you can't just call stitch.Stitch.defaultappClient

Tyler Kaye
  • 11
  • 1
0
const client = stitch.Stitch.initializeDefaultAppClient('<<app-id>>');

var emailClient = client.auth.getProviderClient(window.stitch.UserPasswordAuthProviderClient.factory);

emailPassClient.resetPassword(token, tokenId, newPassword).then(() => {
        console.log("Successfully reset password!");
      }).catch(err => {
        console.log("Error resetting password:", err);
      });
  • 1
    While the code snippet could be the answer, the best way is to also explain why this is the solution as per giving reference links for it. It helps people understand what your code is doing and why it would solve the problem. – Sayuri Mizuguchi Jun 11 '20 at 15:01
  • @SayuriMizuguchi I believe stitch library doesn't expose "Stitch" but "stitch", that is why accessing "Stitch" was throwing "Reference Error". – Manas Singh Jun 15 '20 at 06:41