W
Wasp5w ago
Joaquim

How can users update their password?

What's the best way to implement users updating their password? I think what I want is an operation that received a password and updates the current user's password to that one. It's fine if it doesn't exist
1 Reply
kapa.ai
kapa.ai5w ago
Hi @Joaquim. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To update a user's password in Wasp, you can implement an action that allows users to change their password. Here's how you can do it: 1. Define an action in your main.wasp file:
action updatePassword {
fn: import { updatePassword } from "@src/auth",
}
action updatePassword {
fn: import { updatePassword } from "@src/auth",
}
2. Implement the updatePassword function in your src/auth.js or src/auth.ts file:
import {
createProviderId,
findAuthIdentity,
updateAuthIdentityProviderData,
deserializeAndSanitizeProviderData,
} from 'wasp/server/auth';

export const updatePassword = async (args, context) => {
const providerId = createProviderId('email', args.email)
const authIdentity = await findAuthIdentity(providerId)
if (!authIdentity) {
throw new HttpError(400, "Unknown user")
}

const providerData = deserializeAndSanitizeProviderData(authIdentity.providerData)

// Updates the password and hashes it automatically.
await updateAuthIdentityProviderData(providerId, providerData, {
hashedPassword: args.password,
})
}
import {
createProviderId,
findAuthIdentity,
updateAuthIdentityProviderData,
deserializeAndSanitizeProviderData,
} from 'wasp/server/auth';

export const updatePassword = async (args, context) => {
const providerId = createProviderId('email', args.email)
const authIdentity = await findAuthIdentity(providerId)
if (!authIdentity) {
throw new HttpError(400, "Unknown user")
}

const providerData = deserializeAndSanitizeProviderData(authIdentity.providerData)

// Updates the password and hashes it automatically.
await updateAuthIdentityProviderData(providerId, providerData, {
hashedPassword: args.password,
})
}
This implementation uses Wasp's helper functions to handle password hashing automatically Password Hashing (https://wasp-lang.dev/docs/auth/overview#password-hashing). When implementing this functionality, keep in mind: 1. Always hash passwords before storing them in the database. Wasp's helper functions handle this for you. 2. Implement proper error handling and validation to ensure password requirements are met. 3. Consider implementing rate limiting to prevent abuse. If you want to provide a complete password reset flow, including sending reset emails, Wasp also offers built-in support for this Password Reset Flow (https://wasp-lang.dev/docs/auth/email#password-reset-flow). Remember to handle the frontend part of this process, where you'll need to create a form for users to enter their new password and call the updatePassword action. Hmm, I do not have enough information to provide a specific code example for the frontend implementation, as it wasn't included in the knowledge sources. However, you can create a React component with a form that calls the updatePassword action when submitted. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Joaquim, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Want results from more Discord servers?
Add your server