Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: Redeem invitation logic

Blue Mouse417f45fbc375
diff --git a/backend/apps/cloud/src/auth/auth.controller.ts b/backend/apps/cloud/src/auth/auth.controller.ts
index 982953cd8..15021e3c9 100644
--- a/backend/apps/cloud/src/auth/auth.controller.ts
+++ b/backend/apps/cloud/src/auth/auth.controller.ts
@@ -132,10 +132,10 @@ export class AuthController {
body.password,
)
- const hadPendingInvitations =
+ const redeemedCount =
await this.authService.redeemPendingInvitations(newUser)
- if (hadPendingInvitations) {
+ if (redeemedCount > 0) {
await this.userService.updateUser(newUser.id, {
registeredViaInvitation: true,
hasCompletedOnboarding: true,
@@ -151,11 +151,12 @@ export class AuthController {
const jwtTokens = await this.authService.generateJwtTokens(newUser.id, true)
- const updatedUser = hadPendingInvitations
- ? await this.userService.findUserById(newUser.id)
- : newUser
+ const updatedUser =
+ redeemedCount > 0
+ ? await this.userService.findUserById(newUser.id)
+ : newUser
- if (hadPendingInvitations) {
+ if (redeemedCount > 0) {
const [sharedProjects, organisationMemberships] = await Promise.all([
this.authService.getSharedProjectsForUser(newUser.id),
this.userService.getOrganisationsForUser(newUser.id),
@@ -490,7 +491,13 @@ export class AuthController {
const project = await this.projectService.findOne({
where: { id: invitation.projectId },
})
- targetName = project?.name || ''
+
+ if (!project) {
+ await this.pendingInvitationService.delete(invitation.id)
+ throw new NotFoundException('Invitation not found or has expired')
+ }
+
+ targetName = project.name
} else if (
invitation.type === PendingInvitationType.ORGANISATION_MEMBER &&
invitation.organisationId
@@ -498,7 +505,13 @@ export class AuthController {
const organisation = await this.organisationService.findOne({
where: { id: invitation.organisationId },
})
- targetName = organisation?.name || ''
+
+ if (!organisation) {
+ await this.pendingInvitationService.delete(invitation.id)
+ throw new NotFoundException('Invitation not found or has expired')
+ }
+
+ targetName = organisation.name
}
return {
@@ -576,6 +589,36 @@ export class AuthController {
throw new BadRequestException('Email does not match the invitation')
}
+ if (
+ invitation.type === PendingInvitationType.PROJECT_SHARE &&
+ invitation.projectId
+ ) {
+ const project = await this.projectService.findOne({
+ where: { id: invitation.projectId },
+ })
+
+ if (!project) {
+ await this.pendingInvitationService.delete(invitation.id)
+ throw new BadRequestException(
+ 'The project this invitation was for no longer exists',
+ )
+ }
+ } else if (
+ invitation.type === PendingInvitationType.ORGANISATION_MEMBER &&
+ invitation.organisationId
+ ) {
+ const organisation = await this.organisationService.findOne({
+ where: { id: invitation.organisationId },
+ })
+
+ if (!organisation) {
+ await this.pendingInvitationService.delete(invitation.id)
+ throw new BadRequestException(
+ 'The organisation this invitation was for no longer exists',
+ )
+ }
+ }
+
const existingUser = await this.userService.findUser(body.email)
if (existingUser) {
diff --git a/backend/apps/cloud/src/auth/auth.service.ts b/backend/apps/cloud/src/auth/auth.service.ts
index 043b2af3f..3307125e6 100644
--- a/backend/apps/cloud/src/auth/auth.service.ts
+++ b/backend/apps/cloud/src/auth/auth.service.ts
@@ -189,15 +189,17 @@ export class AuthService {
return user
}
- public async redeemPendingInvitations(user: User): Promise<boolean> {
+ public async redeemPendingInvitations(user: User): Promise<number> {
const pendingInvitations = await this.pendingInvitationService.findByEmail(
user.email,
)
if (!pendingInvitations.length) {
- return false
+ return 0
}
+ let redeemed = 0
+
for (const pending of pendingInvitations) {
if (
pending.type === PendingInvitationType.PROJECT_SHARE &&
@@ -222,6 +224,7 @@ export class AuthService {
await this.projectService.createShare(share)
await deleteProjectRedis(project.id)
+ redeemed++
}
}
} else if (
@@ -248,6 +251,7 @@ export class AuthService {
await this.organisationService.deleteOrganisationProjectsFromRedis(
organisation.id,
)
+ redeemed++
}
}
}
@@ -255,7 +259,7 @@ export class AuthService {
await this.pendingInvitationService.deleteAllByEmail(user.email)
- return true
+ return redeemed
}
public async generateJwtAccessToken(
@@ -667,9 +671,9 @@ export class AuthService {
const user = await this.userService.create(query)
- const hadPendingInvitations = await this.redeemPendingInvitations(user)
+ const redeemedCount = await this.redeemPendingInvitations(user)
- if (hadPendingInvitations) {
+ if (redeemedCount > 0) {
await this.userService.updateUser(user.id, {
registeredViaInvitation: true,
hasCompletedOnboarding: true,
@@ -678,9 +682,8 @@ export class AuthService {
const jwtTokens = await this.generateJwtTokens(user.id, true)
- const finalUser = hadPendingInvitations
- ? await this.userService.findUserById(user.id)
- : user
+ const finalUser =
+ redeemedCount > 0 ? await this.userService.findUserById(user.id) : user
return {
...jwtTokens,
@@ -1080,9 +1083,9 @@ export class AuthService {
const user = await this.userService.create(query)
- const hadPendingInvitations = await this.redeemPendingInvitations(user)
+ const redeemedCount = await this.redeemPendingInvitations(user)
- if (hadPendingInvitations) {
+ if (redeemedCount > 0) {
await this.userService.updateUser(user.id, {
registeredViaInvitation: true,
hasCompletedOnboarding: true,
@@ -1091,9 +1094,8 @@ export class AuthService {
const jwtTokens = await this.generateJwtTokens(user.id, true)
- const finalUser = hadPendingInvitations
- ? await this.userService.findUserById(user.id)
- : user
+ const finalUser =
+ redeemedCount > 0 ? await this.userService.findUserById(user.id) : user
return {
...jwtTokens,
diff --git a/web/app/pages/Auth/Signup/InvitationSignup.tsx b/web/app/pages/Auth/Signup/InvitationSignup.tsx
index fa63b03d7..6222f3e9b 100644
--- a/web/app/pages/Auth/Signup/InvitationSignup.tsx
+++ b/web/app/pages/Auth/Signup/InvitationSignup.tsx
@@ -10,7 +10,7 @@ import {
} from 'react-router'
import { toast } from 'sonner'
-import { HAVE_I_BEEN_PWNED_URL } from '~/lib/constants'
+import { HAVE_I_BEEN_PWNED_URL, isSelfhosted } from '~/lib/constants'
import type {
InvitationSignupLoaderData,
InvitationSignupActionData,
@@ -155,42 +155,44 @@ const InvitationSignup = () => {
value={checkIfLeaked ? 'true' : 'false'}
/>
- <Checkbox
- checked={tos}
- onChange={(checked) => {
- setTos(checked)
- clearFieldError('tos')
- }}
- disabled={isFormSubmitting}
- label={
- <Text as='span' size='sm'>
- <Trans
- t={t}
- i18nKey='auth.signup.tos'
- components={{
- tos: (
- <Link
- to={routes.terms}
- className='font-medium text-gray-900 underline decoration-dashed hover:decoration-solid dark:text-gray-300'
- aria-label={t('footer.tos')}
- />
- ),
- pp: (
- <Link
- to={routes.privacy}
- className='font-medium text-gray-900 underline decoration-dashed hover:decoration-solid dark:text-gray-300'
- aria-label={t('footer.pp')}
- />
- ),
- }}
- />
- </Text>
- }
- classes={{
- hint: '!text-red-600 dark:!text-red-500',
- }}
- hint={getFieldError('tos')}
- />
+ {isSelfhosted ? null : (
+ <Checkbox
+ checked={tos}
+ onChange={(checked) => {
+ setTos(checked)
+ clearFieldError('tos')
+ }}
+ disabled={isFormSubmitting}
+ label={
+ <Text as='span' size='sm'>
+ <Trans
+ t={t}
+ i18nKey='auth.signup.tos'
+ components={{
+ tos: (
+ <Link
+ to={routes.terms}
+ className='font-medium text-gray-900 underline decoration-dashed hover:decoration-solid dark:text-gray-300'
+ aria-label={t('footer.tos')}
+ />
+ ),
+ pp: (
+ <Link
+ to={routes.privacy}
+ className='font-medium text-gray-900 underline decoration-dashed hover:decoration-solid dark:text-gray-300'
+ aria-label={t('footer.pp')}
+ />
+ ),
+ }}
+ />
+ </Text>
+ }
+ classes={{
+ hint: '!text-red-600 dark:!text-red-500',
+ }}
+ hint={getFieldError('tos')}
+ />
+ )}
<div className='flex items-center'>
<Checkbox
diff --git a/web/app/routes/signup_.invitation.$id.tsx b/web/app/routes/signup_.invitation.$id.tsx
index b5e00cc71..94ed141d5 100644
--- a/web/app/routes/signup_.invitation.$id.tsx
+++ b/web/app/routes/signup_.invitation.$id.tsx
@@ -13,7 +13,7 @@ import {
registerViaInvitation,
claimInvitation,
} from '~/api/api.server'
-import { getOgImageUrl } from '~/lib/constants'
+import { getOgImageUrl, isSelfhosted } from '~/lib/constants'
import InvitationSignup from '~/pages/Auth/Signup/InvitationSignup'
import { getDescription, getPreviewImage, getTitle } from '~/utils/seo'
import { createHeadersWithCookies } from '~/utils/session.server'
@@ -128,7 +128,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
fieldErrors.password = 'Password must be at most 50 characters'
}
- if (!tos) {
+ if (!tos && !isSelfhosted) {
fieldErrors.tos = 'You must accept the Terms of Service'
}