W
Wasp-langβ€’5mo ago
WWWillems

Usage of getEmail

Maybe I'm missing something, but why does the getEmail() helper (see https://wasp-lang.dev/docs/auth/entities#getemail) only return an email address when the user has signed up using the email auth identity? I would expect it to return the user's email address for both social & email login types. My app offers both google & email auth logins and I would like to use this helper function to get the email address for all login methods. Currently I need to check which auth method the user has signed up with to know how I can use their email address.
Auth Entities | Wasp
Wasp supports multiple different authentication methods and for each method, we need to store different information about the user. For example, if you are using the Username & password authentication method, we need to store the user's username and password. On the other hand, if you are using the Email authentication method, you will need to s...
7 Replies
kapa.ai
kapa.aiβ€’5mo ago
Hi @WWWillems. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The getEmail() helper function in Wasp is designed to return the user's email address only if the user has an email auth identity. This is because Wasp treats different authentication methods separately and stores different information about the user for each method. For example, if you are using the Email authentication method, Wasp will store the user's email, password, and their email verification status. On the other hand, for social login providers like Google, Wasp only stores the user's ID specific to the provider by default [source (https://wasp-lang.dev/docs/auth/entities)][source (https://wasp-lang.dev/docs/auth/social-auth/google#overrides)]. Currently, Wasp does not support multiple auth identities for a single user. This means, for example, that a user can't have both an email-based auth identity and a Google-based auth identity. This is something Wasp plans to add in the future with the introduction of the account merging feature [source (https://wasp-lang.dev/docs/auth/email)]. If you want to get the user's email from Google, you need to specify the email scope in the configFn function when setting up Google authentication. The data received from Google can contain the user's email among other fields, depending on the scopes you request [source (https://wasp-lang.dev/docs/auth/social-auth/google#overrides)]. For now, if you want to get the user's email regardless of the authentication method, you might need to implement your own helper function that checks the authentication method used and retrieves the email accordingly. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @WWWillems, 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?
WWWillems
WWWillemsOPβ€’5mo ago
I have already defined the email scope in my Google auth configFn and can successfully use their email address. I just don't want to have to check which auth method they have signed up with just to display/use their email address. Looks like I have to define my own helper method, too bad, as this is something I would've expected from the getEmail helper 😦 If anyone needs it, wrote my own helper πŸ€·β€β™‚οΈ
import { type AuthUser, findUserIdentity, getEmail } from "wasp/auth";

const getUserEmail = (user: AuthUser): string => {
const emailIdentity = findUserIdentity(user, "email");
const googleIdentity = findUserIdentity(user, "google");

if (emailIdentity) {
return `${getEmail(user)}`;
}

if (googleIdentity) {
return user.email;
}

return "No auth identity found";
};

export default getUserEmail;
import { type AuthUser, findUserIdentity, getEmail } from "wasp/auth";

const getUserEmail = (user: AuthUser): string => {
const emailIdentity = findUserIdentity(user, "email");
const googleIdentity = findUserIdentity(user, "google");

if (emailIdentity) {
return `${getEmail(user)}`;
}

if (googleIdentity) {
return user.email;
}

return "No auth identity found";
};

export default getUserEmail;
MEE6
MEE6β€’5mo ago
Wohooo @WWWillems, you just became a Waspeteer level 4!
miho
mihoβ€’5mo ago
This is not something we can do because we don't control the data you get from the social provider, we only know we'll get the ID but not other fields. The data you get from the social provider depends on the scope you set and then you can set any of the received data on the User object using the userSignupFields helper. For email login method, we know for a fact there will be an email and we give it to you.
martinsos
martinsosβ€’5mo ago
Right, so not only do we not know if you receive the email, we also don't know how you store it in the database, right? But on the other hand, we do store the received social provider data in the db, right? Sowe could have a function getEmail that tries to get the email for any provider, but it might not return it if it was not part of the response. Would that make sense? Or would the better solution would be to advise people to store email onto User and use that? I remember I had a similar "issue". So while maybe we can't produce exactly the method @WWWillems imagined, we can still try to solve the problem in some other way, because I think many people will want to get email regardless of the auth method used, and they won't know the best / recommended way to do it. So i would say either we offer some kind of function for it, or we write a short guide in the docs. But now it seems to me like we maybe already have an issue for it in the Github?
miho
mihoβ€’5mo ago
do store the received social provider data in the db, right
We don't store any social provider data by default πŸ‘€ If we create a helper that tries to guess where the email is in the received/stored data for Google then we'd have to implement it for each provider down the line. Validate the scopes or something like that and then know the shape of each OAuth provider data. The best thing users can do now is save the data they need and know exists in the User and use it like user.email which isn't that much work IMHO.
WWWillems
WWWillemsOPβ€’5mo ago
Oh right πŸ€¦β€β™‚οΈ forgot I added the email scope to the social login myself 😬
Want results from more Discord servers?
Add your server