add zod checks to social auth user field getters (#185)

* add zod checks to social auth

* Update setUsername.ts
This commit is contained in:
vincanger
2024-06-21 11:33:25 +02:00
committed by GitHub
parent 486f851390
commit 3067b5e90a

View File

@@ -1,4 +1,5 @@
import { defineUserSignupFields } from 'wasp/auth/providers/types'; import { defineUserSignupFields } from 'wasp/auth/providers/types';
import { z } from 'zod';
const adminEmails = process.env.ADMIN_EMAILS?.split(',') || []; const adminEmails = process.env.ADMIN_EMAILS?.split(',') || [];
@@ -8,24 +9,59 @@ export const getEmailUserFields = defineUserSignupFields({
email: (data: any) => data.email, email: (data: any) => data.email,
}); });
export const getGitHubUserFields = defineUserSignupFields({ const githubDataSchema = z.object({
// NOTE: if we don't want to access users' emails, we can use scope ["user:read"] profile: z.object({
// instead of ["user"] and access args.profile.username instead emails: z.array(
email: (data: any) => data.profile.emails[0].email, z.object({
username: (data: any) => data.profile.login, email: z.string(),
isAdmin: (data: any) => adminEmails.includes(data.profile.emails[0].email), })
),
login: z.string(),
}),
}); });
export const getGitHubUserFields = defineUserSignupFields({
email: (data) => {
const githubData = githubDataSchema.parse(data);
return githubData.profile.emails[0].email;
},
username: (data) => {
const githubData = githubDataSchema.parse(data);
return githubData.profile.login;
},
isAdmin: (data) => {
const githubData = githubDataSchema.parse(data);
return adminEmails.includes(githubData.profile.emails[0].email);
},
});
// NOTE: if we don't want to access users' emails, we can use scope ["user:read"]
// instead of ["user"] and access args.profile.username instead
export function getGitHubAuthConfig() { export function getGitHubAuthConfig() {
return { return {
scopes: ['user'], scopes: ['user'],
}; };
} }
const googleDataSchema = z.object({
profile: z.object({
email: z.string(),
}),
});
export const getGoogleUserFields = defineUserSignupFields({ export const getGoogleUserFields = defineUserSignupFields({
email: (data: any) => data.profile.email, email: (data) => {
username: (data: any) => data.profile.name, const googleData = googleDataSchema.parse(data);
isAdmin: (data: any) => adminEmails.includes(data.profile.email), return googleData.profile.email;
},
username: (data) => {
const googleData = googleDataSchema.parse(data);
return googleData.profile.email;
},
isAdmin: (data) => {
const googleData = googleDataSchema.parse(data);
return adminEmails.includes(googleData.profile.email);
},
}); });
export function getGoogleAuthConfig() { export function getGoogleAuthConfig() {