basic auth portal save

This commit is contained in:
Milo Schwartz 2024-11-23 16:36:07 -05:00
parent f9e0c33368
commit 0b3ca5f999
No known key found for this signature in database
12 changed files with 511 additions and 269 deletions

View file

@ -90,7 +90,7 @@ export async function verifyResourceSession(
return allowed(res);
}
const redirectUrl = `${config.app.base_url}/${resource.orgId}/auth/resource/${resource.resourceId}?redirect=${originalRequestURL}`;
const redirectUrl = `${config.app.base_url}/${resource.orgId}/auth/resource/${resource.resourceId}?r=${originalRequestURL}`;
if (sso && sessions.session) {
const { session, user } = await validateSessionToken(

View file

@ -23,12 +23,13 @@ export type GetResourceAuthInfoResponse = {
pincode: boolean;
sso: boolean;
blockAccess: boolean;
url: string;
};
export async function getResourceAuthInfo(
req: Request,
res: Response,
next: NextFunction
next: NextFunction,
): Promise<any> {
try {
const parsedParams = getResourceAuthInfoSchema.safeParse(req.params);
@ -36,8 +37,8 @@ export async function getResourceAuthInfo(
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedParams.error).toString()
)
fromError(parsedParams.error).toString(),
),
);
}
@ -48,11 +49,11 @@ export async function getResourceAuthInfo(
.from(resources)
.leftJoin(
resourcePincode,
eq(resourcePincode.resourceId, resources.resourceId)
eq(resourcePincode.resourceId, resources.resourceId),
)
.leftJoin(
resourcePassword,
eq(resourcePassword.resourceId, resources.resourceId)
eq(resourcePassword.resourceId, resources.resourceId),
)
.where(eq(resources.resourceId, resourceId))
.limit(1);
@ -61,9 +62,11 @@ export async function getResourceAuthInfo(
const pincode = result?.resourcePincode;
const password = result?.resourcePassword;
const url = `${resource.ssl ? "https" : "http"}://${resource.fullDomain}`;
if (!resource) {
return next(
createHttpError(HttpCode.NOT_FOUND, "Resource not found")
createHttpError(HttpCode.NOT_FOUND, "Resource not found"),
);
}
@ -75,6 +78,7 @@ export async function getResourceAuthInfo(
pincode: pincode !== null,
sso: resource.sso,
blockAccess: resource.blockAccess,
url,
},
success: true,
error: false,
@ -83,7 +87,10 @@ export async function getResourceAuthInfo(
});
} catch (error) {
return next(
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"An error occurred",
),
);
}
}

View file

@ -31,7 +31,7 @@ const updateResourceBodySchema = z
export async function updateResource(
req: Request,
res: Response,
next: NextFunction
next: NextFunction,
): Promise<any> {
try {
const parsedParams = updateResourceParamsSchema.safeParse(req.params);
@ -39,8 +39,8 @@ export async function updateResource(
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedParams.error).toString()
)
fromError(parsedParams.error).toString(),
),
);
}
@ -49,8 +49,8 @@ export async function updateResource(
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedBody.error).toString()
)
fromError(parsedBody.error).toString(),
),
);
}
@ -67,8 +67,8 @@ export async function updateResource(
return next(
createHttpError(
HttpCode.NOT_FOUND,
`Resource with ID ${resourceId} not found`
)
`Resource with ID ${resourceId} not found`,
),
);
}
@ -76,16 +76,23 @@ export async function updateResource(
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Resource does not have a domain"
)
"Resource does not have a domain",
),
);
}
const fullDomain = `${updateData.subdomain}.${resource[0].orgs.domain}`;
const fullDomain = updateData.subdomain
? `${updateData.subdomain}.${resource[0].orgs.domain}`
: undefined;
const updatePayload = {
...updateData,
...(fullDomain && { fullDomain }),
};
const updatedResource = await db
.update(resources)
.set({ ...updateData, fullDomain })
.set(updatePayload)
.where(eq(resources.resourceId, resourceId))
.returning();
@ -93,8 +100,8 @@ export async function updateResource(
return next(
createHttpError(
HttpCode.NOT_FOUND,
`Resource with ID ${resourceId} not found`
)
`Resource with ID ${resourceId} not found`,
),
);
}
@ -108,7 +115,10 @@ export async function updateResource(
} catch (error) {
logger.error(error);
return next(
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"An error occurred",
),
);
}
}