0

I am writing a password reset function in Vue.js. As part of the process I generate a password key that is part of the reset url emailed to the user. It is very well possible that user may request a reset on one device but then go to another to do the reset. Therefore, storing it in the local storage is not an option. How to I pass the password key along with the new password. Please advise.

4
  • "I generate a password key that is part of the reset url emailed to the user." How is it a part of it? Example? Commented Apr 28, 2018 at 2:50
  • @acdcjunior - myurl.com/passwordreset?key=somekey Commented Apr 28, 2018 at 3:11
  • Shouldn't the user be able to access their email on another device, and then complete the password reset flow? Commented Apr 28, 2018 at 3:21
  • @Eric - yes. The problem is not with the email access. The problem is to pass "somekey" string as a hidden variable along with new password so that the reset can be completed. Commented Apr 28, 2018 at 3:34

1 Answer 1

2

The way you can do this with Vue is using vue-router and accessing the $route.query object.

For instance, when the URL has ?key=somekey, the $route.query object is `{key: "somekey"}.

Here's a demo CodeSandbox showing how you could use it.

Relevant code:

// main.js
import Vue from "vue";
import VueRouter from "vue-router";
import PasswordReset from "./PasswordReset";

Vue.use(VueRouter);

var router = new VueRouter({
  mode: 'history',
  routes: [{
    path: '/',
    component: {
      template: `<div>
   You are at '/'.<br>
     Click to simulate an external link user:
     <a href="https://lrrwkyv7k7.codesandbox.io/passwordreset?key=somekey">
     https://lrrwkyv7k7.codesandbox.io/passwordreset?key=somekey
     </a>
      </div>`
    }
  }, {
    path: '/passwordreset',
    component: PasswordReset
  }]
});

new Vue({
  el: "#app",
  router: router,
  template: '<router-view></router-view>'
});
// PasswordReset.vue
<template>
  <div id="app">
    <p>Hello, user.</p>
    <form action="" @submit="submit">
    Type some new password: <input type="text" name="newpass" v-model="newPass">
    <input type="hidden" name="key" :value="hiddenKey">
    <button>Send</button>
    </form>
    <hr>
    <div class="debug">
        Object that will be sent:
        <pre>{{ $data }}</pre>

        <a href="/">click here to get back to home</a>
    </div>
  </div>
</template>

<script>
export default {
  name: "PasswordReset",
  data() {
    return {
      newPass: "",
      hiddenKey: ""
    };
  },
  mounted() {
    this.hiddenKey = this.$route.query.key;
  },
  methods: {
    submit(event) {
      alert('We would have sent:\n'+JSON.stringify(this.$data, null, 2));
      event.preventDefault(); // don't submit now, just testing...
    }
  }
};
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

This worked. I was using the $route.query.key but missing the mounted() part. I am new to Vue.js, thus a little bit learning curve. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.