Skip to content

Commit

Permalink
feat(user): add post accept invitation config (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
dipendraupreti committed Aug 2, 2023
1 parent 44bd832 commit 2e8bb39
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/user/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import "@dzangolab/fastify-mercurius";

import type { SupertokensConfig } from "./supertokens";
import type { IsEmailOptions, StrongPasswordOptions, User } from "./types";
import type { Invitation } from "./types/invitation";
import type { FastifyRequest } from "fastify";
import type { ServerResponse } from "node:http";

declare module "fastify" {
Expand All @@ -26,6 +28,11 @@ declare module "@dzangolab/fastify-config" {
* @default 30
*/
expireAfterInDays?: number;
postAccept?: (
request: FastifyRequest,
invitation: Invitation,
user: User
) => Promise<void>;
};
email?: IsEmailOptions;
password?: StrongPasswordOptions;
Expand Down Expand Up @@ -60,3 +67,8 @@ export type {
UserUpdateInput,
User,
} from "./types";
export type {
Invitation,
InvitationCreateInput,
InvitationUpdateInput,
} from "./types/invitation";
12 changes: 12 additions & 0 deletions packages/user/src/model/invitations/handlers/acceptInvitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import validateEmail from "../../../validator/email";
import validatePassword from "../../../validator/password";
import Service from "../service";

import type { User } from "../../../types";
import type {
Invitation,
InvitationCreateInput,
Expand Down Expand Up @@ -98,6 +99,17 @@ const acceptInvitation = async (
acceptedAt: formatDate(new Date(Date.now())),
});

// run post accept hook
try {
await config.user.invitation?.postAccept?.(
request,
invitation,
signUpResponse.user as unknown as User
);
} catch (error) {
log.error(error);
}

// create new session so the user be logged in on signup
await createNewSession(request, reply, signUpResponse.user.id);

Expand Down

0 comments on commit 2e8bb39

Please sign in to comment.