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.
-
"I generate a password key that is part of the reset url emailed to the user." How is it a part of it? Example?acdcjunior– acdcjunior2018-04-28 02:50:45 +00:00Commented Apr 28, 2018 at 2:50
-
@acdcjunior - myurl.com/passwordreset?key=somekeyuser9465677– user94656772018-04-28 03:11:18 +00:00Commented 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?Eric Guan– Eric Guan2018-04-28 03:21:18 +00:00Commented 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.user9465677– user94656772018-04-28 03:34:06 +00:00Commented Apr 28, 2018 at 3:34
Add a comment
|
1 Answer
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>
1 Comment
user9465677
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.