Can I create a user with only an email?

TLDR: I want to create an user only knowing the email in advance in order to allow users to pay without having to log in, just provide their email. I'm building an app that requires an extensive survey, in order to reduce friction the last step on my survey is asking the user for an email. After that I would like to show a paywall and allow the user to pay / subscribe without ever having to officially create an account. After payment I will redirect to account creation etc. is that possible?
8 Replies
kapa.ai
kapa.ai3w ago
Hi @brahyammm. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, it is possible to create a user with only an email in Wasp. You can create a custom sign-up action that only requires an email. Here's how you can do it: First, you need to define a custom sign-up action in your main.wasp file:
action customSignup {
fn: import { signup } from "@src/auth/signup.js",
}
action customSignup {
fn: import { signup } from "@src/auth/signup.js",
}
Source: Wasp Docs (https://wasp-lang.dev/docs/auth/email#creating-a-custom-sign-up-action) Then, in your src/auth/signup.js file, you can define the signup function that only requires an email:
import {
ensureValidEmail,
createProviderId,
sanitizeAndSerializeProviderData,
createUser,
createEmailVerificationLink,
sendEmailVerificationEmail,
} from 'wasp/server/auth'

export const signup = async (args, _context) => {
ensureValidEmail(args)

try {
const providerId = createProviderId('email', args.email)
const newUserProviderData = await sanitizeAndSerializeProviderData({
isEmailVerified: false,
emailVerificationSentAt: null,
passwordResetSentAt: null,
})
await createUser(
providerId,
providerData,
// Any additional data you want to store on the User entity
{},
)

// Verification link links to a client route e.g. /email-verification
const verificationLink = await createEmailVerificationLink(args.email, '/email-verification');
try {
await sendEmailVerificationEmail(
args.email,
{
from: {
name: "My App Postman",
email: "**[email protected]** (<https://wasp-lang.dev/cdn-cgi/l/email-protection>)",
},
to: args.email,
subject: "Verify your email",
text: `Click the link below to verify your email: ${verificationLink}`,
html: `
<p>Click the link below to verify your email</p>
<a href="${verificationLink}">Verify email</a>
`,
}
);
} catch (e: unknown) {
console.error("Failed to send email verification email:", e);
throw new HttpError(500, "Failed to send email verification email.");
}
}
}
import {
ensureValidEmail,
createProviderId,
sanitizeAndSerializeProviderData,
createUser,
createEmailVerificationLink,
sendEmailVerificationEmail,
} from 'wasp/server/auth'

export const signup = async (args, _context) => {
ensureValidEmail(args)

try {
const providerId = createProviderId('email', args.email)
const newUserProviderData = await sanitizeAndSerializeProviderData({
isEmailVerified: false,
emailVerificationSentAt: null,
passwordResetSentAt: null,
})
await createUser(
providerId,
providerData,
// Any additional data you want to store on the User entity
{},
)

// Verification link links to a client route e.g. /email-verification
const verificationLink = await createEmailVerificationLink(args.email, '/email-verification');
try {
await sendEmailVerificationEmail(
args.email,
{
from: {
name: "My App Postman",
email: "**[email protected]** (<https://wasp-lang.dev/cdn-cgi/l/email-protection>)",
},
to: args.email,
subject: "Verify your email",
text: `Click the link below to verify your email: ${verificationLink}`,
html: `
<p>Click the link below to verify your email</p>
<a href="${verificationLink}">Verify email</a>
`,
}
);
} catch (e: unknown) {
console.error("Failed to send email verification email:", e);
throw new HttpError(500, "Failed to send email verification email.");
}
}
}
Source: Wasp Docs (https://wasp-lang.dev/docs/auth/email#creating-a-custom-sign-up-action) This will create a new user with only an email and send them a verification email. After the user verifies their email, they can proceed to create a password and complete their account creation. Please note that this approach requires the user to verify their email before they can complete their account creation. If you want to allow users to pay/subscribe without verifying their email, you might need to modify this approach. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @brahyammm, 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?
miho
miho3w ago
The bot made some stuff up here beware! 😄
brahyammm
brahyammm3w ago
Sounds possible with the above approach by skipping the email verification right?
miho
miho3w ago
What Kapa talked about here is a bit too optimistic, I don't it would work with Wasp in that form. What you can do of course is - have two entities / tables - User for auth and Customer that has on it an email and hasPaid fields. Do the process without auth first and rely on the Customer entity. Later on when they create an account, it will create an User entity for them and you can then use the onAfterSignup auth hook to connect the User and Customer entities 🙂
brahyammm
brahyammm3w ago
oh nice!, Im gonna give that a try
miho
miho3w ago
In the suggestion above, you are doing your flow without auth, but later on getting the benefits of the Wasp's auth system 😄
brahyammm
brahyammm3w ago
thank you
miho
miho3w ago
Of course, you can go more custom like Kapa suggested above, feel free to experiment! Good luck 😄
Want results from more Discord servers?
Add your server