How to use the webiny-api.ApiErrorResponse function in webiny-api

To help you get started, we’ve selected a few webiny-api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github webiny / webiny-js / packages / webiny-api-security / src / endpoints / auth.js View on Github external
init(api: ApiContainer) {
        const notAuthenticated = new ApiErrorResponse(
            {},
            "You are not authenticated!",
            "WBY_NOT_AUTHENTICATED",
            401
        );

        /**
         * Identity profile
         */
        api
            .get("Auth.Me", "/me", async ({ req }) => {
                if (!req.identity) {
                    return notAuthenticated;
                }
                return new ApiResponse(await req.identity.toJSON(req.query._fields));
            })
github webiny / webiny-js / packages / webiny-api-security / src / endpoints / generator.js View on Github external
const error = `"expiresOn" function must be configured for "${strategy}" strategy!`;
                                invariant(typeof expiresOn === "function", error);

                                let expiration = expiresOn(req);
                                if (expiration instanceof Date) {
                                    expiration = Math.floor(expiration.getTime() / 1000);
                                }

                                return new ApiResponse({
                                    token: await authentication.createToken(identity, expiration),
                                    identity: await identity.toJSON(req.query._fields),
                                    expiresOn: expiration
                                });
                            } catch (e) {
                                const response = new ApiErrorResponse({}, e.message);
                                if (e instanceof AuthenticationError) {
                                    response.errorCode = "WBY_INVALID_CREDENTIALS";
                                    response.statusCode = 401;
                                } else {
                                    response.errorCode = "WBY_INTERNAL_ERROR";
                                    response.statusCode = 500;
                                }
                                return response;
                            }
                        })
                        .setPublic();