setPassword with POST from JS app frontend template instead of craft link #9549
-
DescriptionI have a VueJS frontend and using craft cms and graphql, I would like to set the user's password with a frontend form my JS app using axios, something like this...
Is it possible to do this? @brandonkelly What would the action expect in terms of data? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Hey @brandonkelly how can I allow a user set a password via a form using an axios POST request in a vue.js app? |
Beta Was this translation helpful? Give feedback.
-
Yes this is possible. You’d do it with the try {
// Get the current CSRF token (if you don't already have it)
const csrfToken = (await axios.get(`${this.apiUrl}actions/users/session-info`)).data.csrfTokenValue;
const response = await axios.post(`${this.apiUrl}actions/users/save`, {
userId: 100,
currentPassword: 'current-password',
newPassword: 'new-password',
}, {
headers: {
'x-csrf-token': csrfToken,
},
});
const {success, id, csrfTokenValue} = response.data;
} catch (e) {
return;
} For it to work, the user must have an active session. If they don’t already have one, you can create one via the |
Beta Was this translation helpful? Give feedback.
-
For handling Then in JS on the page, fetch the Display a password form, and on submit, send a POST request to Craft’s try {
const response = await axios.post(`${this.apiUrl}actions/users/set-password`, {
code: code,
id: id,
newPassword: 'new-password',
});
if (typeof response.data.error !== 'undefined') {
throw response.data.error;
}
} catch (e) {
return;
} If successful, |
Beta Was this translation helpful? Give feedback.
For handling
set-password
requests coming from verification emails, first you’ll need to change your setPasswordPath config setting to the absolute URL of whatever page you want to show the password form on.Then in JS on the page, fetch the
id
andcode
query string params. (id
will be the user’s UID, not their numerical ID.)Display a password form, and on submit, send a POST request to Craft’s
users/set-password
action: