Password Reset for usernameAndPassword

How do we create a password reset page when using the usernameAndPassword option? It should allow a user to change their password
5 Replies
kapa.ai
kapa.ai2w ago
Hi @simsima. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I apologize, but I don't have enough information to provide a complete answer about creating a password reset page for the usernameAndPassword authentication option in Wasp. The username and password authentication method in Wasp is primarily intended for learning and testing purposes, not for production use. As noted in a GitHub issue, this method lacks built-in account recovery features GitHub Issue (https://github.com/wasp-lang/wasp/issues/1572). For a more robust authentication system with password reset functionality, Wasp recommends using email-based authentication instead. With email authentication, you can implement a password reset flow that includes: 1. A forgot password form Forgot Password Form (https://wasp-lang.dev/docs/auth/ui#forgot-password-form) 2. A reset password form Reset Password Form (https://wasp-lang.dev/docs/auth/ui#reset-password-form) 3. A password reset flow involving email Password Reset Flow (https://wasp-lang.dev/docs/auth/email#password-reset-flow) If you're migrating from an older version of Wasp that used username and password authentication, there are migration steps available to help users transition their passwords to a new system Migration Guide (https://wasp-lang.dev/docs/migrate-from-0-11-to-0-12#username--password). However, this is a migration process rather than an ongoing password reset feature. For the most secure and user-friendly approach, consider switching to email-based authentication if possible, as it provides built-in support for password reset functionality. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @simsima, 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?
sim_sima_
sim_sima_OP2w ago
Right, I guess I just want to use that in production and need that to be possible Is there a way to add additional fields to the login page?
NEROX
NEROX2w ago
@kapa.ai how can I customize my login page to add or delete aditional fields?
kapa.ai
kapa.ai2w ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To customize your login page by adding or deleting additional fields in Wasp, you can follow these steps: 1. For adding extra fields during signup: You need to define the extra fields in your main.wasp file and then create a custom signup form. Here's how: a. Define extra fields in main.wasp:
app myApp {
// ...
auth: {
userEntity: User,
methods: {
usernameAndPassword: {
userSignupFields: import { userSignupFields } from "@src/auth/signup",
},
},
onAuthFailedRedirectTo: "/login",
},
}

app myApp {
// ...
auth: {
userEntity: User,
methods: {
usernameAndPassword: {
userSignupFields: import { userSignupFields } from "@src/auth/signup",
},
},
onAuthFailedRedirectTo: "/login",
},
}

b. Create a userSignupFields object in src/auth/signup.js (or .ts):
import { defineUserSignupFields } from 'wasp/server/auth'

export const userSignupFields = defineUserSignupFields({
address: async (data) => {
const address = data.address
if (typeof address !== 'string') {
throw new Error('Address is required')
}
if (address.length < 5) {
throw new Error('Address must be at least 5 characters long')
}
return address
},
})

import { defineUserSignupFields } from 'wasp/server/auth'

export const userSignupFields = defineUserSignupFields({
address: async (data) => {
const address = data.address
if (typeof address !== 'string') {
throw new Error('Address is required')
}
if (address.length < 5) {
throw new Error('Address must be at least 5 characters long')
}
return address
},
})

This example adds an address field to the signup process Defining Extra Fields (https://wasp-lang.dev/docs/auth/overview#1-defining-extra-fields). 2. For customizing the login/signup form UI: If you're using Wasp's Auth UI, you can customize the SignupForm component by passing the additionalFields prop to it. This can be either a list of extra fields or a render function Customizing the Signup Component (https://wasp-lang.dev/docs/auth/overview#2-customizing-the-signup-component). 3. For a fully custom login page: If you need more control, you can create your own custom login page using the login() and signup() actions provided by Wasp. Here's an example of a custom signup form:
import { signup, login } from 'wasp/client/auth'
import { useState } from 'react'
import { useHistory } from 'react-router-dom'

export function Signup() {
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState(null)
const history = useHistory()

async function handleSubmit(event) {
event.preventDefault()
try {
await signup({
username,
password,
})
await login(username, password)
history.push("/")
} catch (error) {
setError(error)
}
}

return (
<form onSubmit={handleSubmit}>
{/* Add your custom fields here */}
</form>
);
}

import { signup, login } from 'wasp/client/auth'
import { useState } from 'react'
import { useHistory } from 'react-router-dom'

export function Signup() {
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [error, setError] = useState(null)
const history = useHistory()

async function handleSubmit(event) {
event.preventDefault()
try {
await signup({
username,
password,
})
await login(username, password)
history.push("/")
} catch (error) {
setError(error)
}
}

return (
<form onSubmit={handleSubmit}>
{/* Add your custom fields here */}
</form>
);
}

This example shows how to create a custom signup form Using signup() and login() actions (https://wasp-lang.dev/docs/auth/username-and-pass#signup). Remember, if you're using the usernameAndPassword auth method, it's primarily intended for learning and testing purposes. For production use, consider using email-based authentication, which provides more robust features including built-in password reset functionality. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NEROX, 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?
Vinny (@Wasp)
Vinny (@Wasp)7d ago
Yep! to add additional fields to the signup process, read the guide in our docs: https://wasp-lang.dev/docs/auth/overview#customizing-the-signup-process
Overview | Wasp
Auth is an essential piece of any serious application. That's why Wasp provides authentication and authorization support out of the box.
Want results from more Discord servers?
Add your server