Skip to content

Update profile creation to set Profile all at once at the beginning of the process #1810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions components/auth/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,24 @@ export function useCreateUserWithEmailAndPassword(isOrg: boolean) {
email,
password
)
await finishSignup({ requestedRole: isOrg ? "organization" : "user" })

const categories = orgCategory ? [orgCategory] : ""

if (isOrg) {
await Promise.all([
setProfile(credentials.user.uid, {
fullName,
orgCategories: categories,
notificationFrequency: "Monthly",
email: credentials.user.email
}),
sendEmailVerification(credentials.user)
])
await finishSignup({
requestedRole: "organization",
fullName,
orgCategories: orgCategory ? [orgCategory] : "",
notificationFrequency: "Monthly",
email: credentials.user.email
})
} else {
await Promise.all([
setProfile(credentials.user.uid, {
fullName,
notificationFrequency: "Monthly",
email: credentials.user.email,
public: true
}),
sendEmailVerification(credentials.user)
])
await finishSignup({
requestedRole: "user",
fullName,
notificationFrequency: "Monthly",
email: credentials.user.email,
public: true
})
}
await sendEmailVerification(credentials.user)

return credentials
}
Expand Down
9 changes: 5 additions & 4 deletions components/auth/types.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { functions } from "components/firebase"
import { httpsCallable } from "firebase/functions"
import { Role } from "../../functions/src/auth/types"
import { Profile } from "components/db"

export * from "../../functions/src/auth/types"

export const finishSignup = httpsCallable<{ requestedRole: Role }, void>(
functions,
"finishSignup"
)
export const finishSignup = httpsCallable<
{ requestedRole: Role } | Partial<Profile>,
void
>(functions, "finishSignup")
7 changes: 5 additions & 2 deletions functions/src/auth/setRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ export const setRole = async ({
uid,
role,
auth,
db
db,
newProfile
}: {
email?: string
uid?: string
role: Role
auth: Auth
db: Database
newProfile?: Partial<Profile>
}) => {
let user: UserRecord
if (email) user = await auth.getUserByEmail(email)
Expand All @@ -30,7 +32,8 @@ export const setRole = async ({
const currentProfile = Profile.Or(Undefined).check(profileData)
const profileUpdate: Partial<Profile> = {
role,
public: isPublic(currentProfile, role)
public: isPublic(currentProfile, role),
...newProfile
}
await profile.set(profileUpdate, { merge: true })

Expand Down
35 changes: 22 additions & 13 deletions functions/src/profile/finishSignup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { db, auth } from "../firebase"
import { z } from "zod"
import { checkRequestZod, checkAuth } from "../common"
import { setRole } from "../auth"
import { Role } from "../auth/types"

const CreateProfileRequest = z.object({
requestedRole: z.enum(["user", "organization", "pendingUpgrade"])
Expand All @@ -14,21 +13,31 @@ export const finishSignup = functions.https.onCall(async (data, context) => {

const { requestedRole } = checkRequestZod(CreateProfileRequest, data)

let role: Role = "user"
const {
fullName,
orgCategories,
notificationFrequency,
email,
public: isPublic
} = data

// Only an admin can approve organizations, after they've signed up initially
// There's a nextjs api route: PATCH /users/<uid> {"role": <role>}
if (requestedRole === "organization") {
role = "pendingUpgrade"
await setRole({
role: "pendingUpgrade",
auth,
db,
uid,
newProfile: { fullName, email, orgCategories }
})
} else {
await setRole({
role: "user",
auth,
db,
uid,
newProfile: { fullName, notificationFrequency, email, public: isPublic }
})
}

await setRole({ role, auth, db, uid })

// upgrade requests table pulls from the profiles collection
await db.doc(`profiles/${uid}`).set(
{
role
},
{ merge: true }
)
})
10 changes: 9 additions & 1 deletion functions/src/profile/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ export const Profile = Record({
about: Optional(String),
social: Optional(Dictionary(String)),
organization: Optional(Boolean),
orgCategories: Optional(Array(String.Or(Null)))
orgCategories: Optional(Array(String.Or(Null))),
email: Optional(String.Or(Null)),
notificationFrequency: Optional(String),
nextDigestAt: Optional(String),
profileImage: Optional(String),
billsFollowing: Optional(Array(String)),
contactInfo: Optional(Dictionary(String)),
location: Optional(String)
})

export type Profile = Static<typeof Profile>