Skip to content

Commit

Permalink
Return customClaims in both beforeUserCreated and beforeUserSignedIn
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljohanneskraft committed Nov 7, 2024
1 parent 151f723 commit 1f5fa5b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 39 deletions.
19 changes: 18 additions & 1 deletion functions/models/src/types/userRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ export const userRegistrationConverter = new Lazy(
}),
)

export interface UserClaims {
type: UserType
organization?: string
}

export class UserRegistration {
// Properties
// Stored Properties

readonly type: UserType
readonly organization?: string
Expand All @@ -80,6 +85,18 @@ export class UserRegistration {
readonly language?: string
readonly timeZone?: string

// Computed Properties

get claims(): UserClaims {
const result: UserClaims = {
type: this.type,
}
if (this.organization !== undefined) {
result.organization = this.organization
}
return result
}

// Constructor

constructor(input: {
Expand Down
16 changes: 11 additions & 5 deletions functions/src/functions/blocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const beforeUserCreatedFunction = beforeUserCreated(
const credential = event.credential

// Escape hatch for users using invitation code to enroll
if (!credential) return
if (!credential) return {}

if (event.data.email === undefined)
throw new https.HttpsError(
Expand Down Expand Up @@ -64,6 +64,8 @@ export const beforeUserCreatedFunction = beforeUserCreated(
isSingleSignOn: true,
})
await factory.trigger().userEnrolled(userDoc)

return { customClaims: invitation.content.user.claims as object }
},
)

Expand All @@ -72,12 +74,16 @@ export const beforeUserSignedInFunction = beforeUserSignedIn(
async (event) => {
try {
const userService = getServiceFactory().user()
const claims = await userService.getClaims(event.data.uid)
logger.info(`beforeUserSignedIn finished successfully.`)
return { sessionClaims: claims }
const user = await userService.getUser(event.data.uid)
if (user !== undefined) {
logger.info(`beforeUserSignedIn finished successfully.`)
return { customClaims: user.content.claims as object }
}
logger.info(`beforeUserSignedIn finished without user.`)
return { customClaims: {} }
} catch (error) {
logger.error(`beforeUserSignedIn finished with error: ${String(error)}`)
return { sessionClaims: {} }
return { customClaims: {} }
}
},
)
12 changes: 3 additions & 9 deletions functions/src/services/user/databaseUserService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ describe('DatabaseUserService', () => {
expect(userData).to.exist
expect(userData?.invitationCode).to.equal(invitationCode)
expect(userData?.dateOfEnrollment).to.exist

const claims = await userService.getClaims(userId)
expect(claims).to.deep.equal({
expect(userData?.claims).to.deep.equal({
type: UserType.admin,
})
})
Expand Down Expand Up @@ -113,9 +111,7 @@ describe('DatabaseUserService', () => {
expect(userData).to.exist
expect(userData?.invitationCode).to.equal(invitationCode)
expect(userData?.dateOfEnrollment).to.exist

const claims = await userService.getClaims(userId)
expect(claims).to.deep.equal({
expect(userData?.claims).to.deep.equal({
type: UserType.clinician,
organization: 'mockOrganization',
})
Expand Down Expand Up @@ -162,9 +158,7 @@ describe('DatabaseUserService', () => {
expect(userData).to.exist
expect(userData?.invitationCode).to.equal(invitationCode)
expect(userData?.dateOfEnrollment).to.exist

const claims = await userService.getClaims(userId)
expect(claims).to.deep.equal({
expect(userData?.claims).to.deep.equal({
type: UserType.patient,
organization: 'mockOrganization',
})
Expand Down
18 changes: 0 additions & 18 deletions functions/src/services/user/databaseUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ import {
type DatabaseService,
} from '../database/databaseService.js'

export interface UserClaims {
type: UserType
organization?: string
}

export class DatabaseUserService implements UserService {
// Properties

Expand Down Expand Up @@ -63,19 +58,6 @@ export class DatabaseUserService implements UserService {
})
}

async getClaims(userId: string): Promise<object> {
const user = await this.getUser(userId)
if (user !== undefined) {
const claims: UserClaims = {
type: user.content.type,
}
if (user.content.organization !== undefined)
claims.organization = user.content.organization
return claims
}
return {}
}

// Invitations

async createInvitation(content: Invitation): Promise<{ id: string }> {
Expand Down
6 changes: 1 addition & 5 deletions functions/src/services/user/userService.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ export class MockUserService implements UserService {
async updateAuth(userId: string, user: UserAuth): Promise<void> {
return
}

async getClaims(userId: string): Promise<object> {
return {}
}


Check failure on line 46 in functions/src/services/user/userService.mock.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `··`
// Methods - Invitations

async createInvitation(content: Invitation): Promise<{ id: string }> {
Expand Down
1 change: 0 additions & 1 deletion functions/src/services/user/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export interface UserService {

getAuth(userId: string): Promise<UserAuth>
updateAuth(userId: string, auth: UserAuth): Promise<void>
getClaims(userId: string): Promise<object>

// Invitations

Expand Down

0 comments on commit 1f5fa5b

Please sign in to comment.