Skip to content

Commit 8f86d00

Browse files
authored
Update profile creation to set Profile all at once at the beginning of the process (#1810)
* Move profile setting to happen all at once in beginning * Remove redundant role * Skip pendingUpgrade step
1 parent 7a0f6d9 commit 8f86d00

5 files changed

Lines changed: 58 additions & 48 deletions

File tree

components/auth/hooks.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,24 @@ export function useCreateUserWithEmailAndPassword(isOrg: boolean) {
6767
email,
6868
password
6969
)
70-
await finishSignup({ requestedRole: isOrg ? "organization" : "user" })
71-
72-
const categories = orgCategory ? [orgCategory] : ""
73-
7470
if (isOrg) {
75-
await Promise.all([
76-
setProfile(credentials.user.uid, {
77-
fullName,
78-
orgCategories: categories,
79-
notificationFrequency: "Weekly",
80-
email: credentials.user.email
81-
}),
82-
sendEmailVerification(credentials.user)
83-
])
71+
await finishSignup({
72+
requestedRole: "organization",
73+
fullName,
74+
orgCategories: orgCategory ? [orgCategory] : "",
75+
notificationFrequency: "Weekly",
76+
email: credentials.user.email
77+
})
8478
} else {
85-
await Promise.all([
86-
setProfile(credentials.user.uid, {
87-
fullName,
88-
notificationFrequency: "Weekly",
89-
email: credentials.user.email,
90-
public: true
91-
}),
92-
sendEmailVerification(credentials.user)
93-
])
79+
await finishSignup({
80+
requestedRole: "user",
81+
fullName,
82+
notificationFrequency: "Weekly",
83+
email: credentials.user.email,
84+
public: true
85+
})
9486
}
87+
await sendEmailVerification(credentials.user)
9588

9689
return credentials
9790
}

components/auth/types.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { functions } from "components/firebase"
22
import { httpsCallable } from "firebase/functions"
33
import { Role } from "../../functions/src/auth/types"
4+
import { Profile } from "components/db"
45

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

7-
export const finishSignup = httpsCallable<{ requestedRole: Role }, void>(
8-
functions,
9-
"finishSignup"
10-
)
8+
export const finishSignup = httpsCallable<
9+
{ requestedRole: Role } | Partial<Profile>,
10+
void
11+
>(functions, "finishSignup")

functions/src/auth/setRole.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ export const setRole = async ({
99
uid,
1010
role,
1111
auth,
12-
db
12+
db,
13+
newProfile
1314
}: {
1415
email?: string
1516
uid?: string
1617
role: Role
1718
auth: Auth
1819
db: Database
20+
newProfile?: Partial<Profile>
1921
}) => {
2022
let user: UserRecord
2123
if (email) user = await auth.getUserByEmail(email)
@@ -30,7 +32,8 @@ export const setRole = async ({
3032
const currentProfile = Profile.Or(Undefined).check(profileData)
3133
const profileUpdate: Partial<Profile> = {
3234
role,
33-
public: isPublic(currentProfile, role)
35+
public: isPublic(currentProfile, role),
36+
...newProfile
3437
}
3538
await profile.set(profileUpdate, { merge: true })
3639

functions/src/profile/finishSignup.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { db, auth } from "../firebase"
33
import { z } from "zod"
44
import { checkRequestZod, checkAuth } from "../common"
55
import { setRole } from "../auth"
6-
import { Role } from "../auth/types"
76

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

1514
const { requestedRole } = checkRequestZod(CreateProfileRequest, data)
1615

17-
let role: Role = requestedRole
16+
const {
17+
fullName,
18+
orgCategories,
19+
notificationFrequency,
20+
email,
21+
public: isPublic
22+
} = data
1823

1924
// Only an admin can approve organizations, after they've signed up initially
2025
// There's a nextjs api route: PATCH /users/<uid> {"role": <role>}
21-
22-
// Removing the "pendingUpgrade" flow (temporarily) because the
23-
// organization approval process is currently too cumbersome.
24-
25-
// if (requestedRole === "organization") {
26-
// role = "pendingUpgrade"
27-
// }
28-
29-
await setRole({ role, auth, db, uid })
30-
31-
// upgrade requests table pulls from the profiles collection
32-
await db.doc(`profiles/${uid}`).set(
33-
{
34-
role
35-
},
36-
{ merge: true }
37-
)
26+
if (requestedRole === "organization") {
27+
await setRole({
28+
role: "organization",
29+
auth,
30+
db,
31+
uid,
32+
newProfile: { fullName, email, orgCategories }
33+
})
34+
} else {
35+
await setRole({
36+
role: "user",
37+
auth,
38+
db,
39+
uid,
40+
newProfile: { fullName, notificationFrequency, email, public: isPublic }
41+
})
42+
}
3843
})

functions/src/profile/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ export const Profile = Record({
2626
about: Optional(String),
2727
social: Optional(Dictionary(String)),
2828
organization: Optional(Boolean),
29-
orgCategories: Optional(Array(String.Or(Null)))
29+
orgCategories: Optional(Array(String.Or(Null))),
30+
email: Optional(String.Or(Null)),
31+
notificationFrequency: Optional(String),
32+
nextDigestAt: Optional(String),
33+
profileImage: Optional(String),
34+
billsFollowing: Optional(Array(String)),
35+
contactInfo: Optional(Dictionary(String)),
36+
location: Optional(String)
3037
})
38+
3139
export type Profile = Static<typeof Profile>

0 commit comments

Comments
 (0)