-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Route to validate user existence in workspace. (#7456)
* Route to validate user existence in workspace. * Case insensitive check * io-ts and using resources * Nits
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |