Skip to content

Commit

Permalink
Route to validate user existence in workspace. (#7456)
Browse files Browse the repository at this point in the history
* Route to validate user existence in workspace.

* Case insensitive check

* io-ts and using resources

* Nits
  • Loading branch information
albandum authored Sep 17, 2024
1 parent a9f209f commit 25fcc5e
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions front/pages/api/v1/w/[wId]/members/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type { WithAPIErrorResponse } from "@dust-tt/types";
import { isLeft } from "fp-ts/lib/Either";
import * as t from "io-ts";
import * as reporter from "io-ts-reporters";
import type { NextApiRequest, NextApiResponse } from "next";

import { withPublicAPIAuthentication } from "@app/lib/api/wrappers";
import type { Authenticator } from "@app/lib/auth";
import { MembershipResource } from "@app/lib/resources/membership_resource";
import { UserResource } from "@app/lib/resources/user_resource";
import { apiError } from "@app/logger/withlogging";

export type ValidateMemberResponseBody = {
valid: boolean;
};

/**
* @ignoreswagger
* Validates an email corresponds to an active member in a specific workspace. For Dust managed apps only - undocumented.
*/

export const PostValidateMemberRequestBodySchema = t.type({
email: t.string,
});

async function handler(
req: NextApiRequest,
res: NextApiResponse<WithAPIErrorResponse<ValidateMemberResponseBody>>,
auth: Authenticator
): Promise<void> {
const bodyValidation = PostValidateMemberRequestBodySchema.decode(req.body);

if (isLeft(bodyValidation)) {
const pathError = reporter.formatValidationErrors(bodyValidation.left);

return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: `Invalid request body: ${pathError}`,
},
});
}

const { email } = bodyValidation.right;

switch (req.method) {
case "POST":
const user = await UserResource.fetchByEmail(email);

const workspace = auth.getNonNullableWorkspace();

if (!user) {
return res.status(200).json({
valid: false,
});
}

const workspaceMembership =
await MembershipResource.getActiveMembershipOfUserInWorkspace({
user,
workspace,
});

const valid = !!workspaceMembership;

return res.status(200).json({
valid,
});

default:
return apiError(req, res, {
status_code: 405,
api_error: {
type: "method_not_supported_error",
message: "The method passed is not supported, POST is expected.",
},
});
}
}

export default withPublicAPIAuthentication(handler);

0 comments on commit 25fcc5e

Please sign in to comment.