Hello, how would I get user data from the thirdparty provider? Im using ThirdPartyNode.Discord
p
Hello, how would I get user data from the thirdparty provider? Im using ThirdPartyNode.Discord
I have a react component in NextJS which should contain the discord username and discord user icon. Ive looked a bit into the user metadata but have no idea how to make it add it once the user logged in
r
Hey! You can get the access token from discord (see https://supertokens.com/docs/thirdparty/common-customizations/getting-provider-access-token), and then you can query discord to get the required info, which you can save in the user metadata
p
Thank you! Also, can SuperTokens work if I disable the email scope on the thirdparty auth?
r
Yes it is possible, but a little hacky. You want to override the discord provider to manually set the email to "something". You can do it this way:
Copy code
ts
ThirdParty.init({
    signInAndUpFeature: {
        providers: [{
            ...DiscordProvider,
            get: (redirectURI, authCodeFromRequest, userContext) => {
                let result = DiscordProvider.get(redirectURI, authCodeFromRequest, userContext);
                return {
                    ...result,
                    getProfileInfo: (accessTokenAPIResponse: {
                        access_token,
                        expires_in,
                        token_type
                    }) => {
                        let accessToken = accessTokenAPIResponse.access_token;
                        let authHeader = `Bearer ${accessToken}`;
                        let response = await axios({
                            method: "get",
                            url: "https://discord.com/api/users/@me",
                            headers: {
                                Authorization: authHeader,
                            },
                        });
                        let userInfo = response.data;
                        let username = "..." // fetch username from discord
                        return {
                            id: userInfo.id,
                            email:
                                userInfo.email === undefined
                                    ? undefined
                                    : {
                                        id: username + "@example.com", // doesn't really matter
                                        isVerified: true,
                                    },
                        };
                    },
                }
            },
        }]
    }
})
Essentially we fake an email which is
${username}@example.com
and this should be fine. Just jeep in mind that if you even want to send an email to this user, you won't actually have their email.
p
Thank you! It works perfectly
r
ok great
p
So I have a userdata endpoint to get the metadata i attached
Copy code
ts
export default async function userData(
    req: NextApiRequest & Request,
    res: NextApiResponse & Response
) {

    await superTokensNextWrapper(
        async (next) => {
            await middleware()(req, res, next)
        },
        req,
        res
    )

    const session = (req as SessionRequest).session;
    console.log(session)
    const userId = session!.getUserId();

    const { metadata } = await UserMetadata.getUserMetadata(userId);

    res.json(metadata)

}
session
is undefined always
r
you need to use verifySession and not middleware - please check the docs once again 🙂
p
👍 Thank you
2 Views