How to use the webiny-api/graphql.ErrorResponse 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 / plugins / graphql / userResolvers / loginUser.js View on Github external
// @flow
import bcrypt from "bcryptjs";
import util from "util";
import { Response, ErrorResponse } from "webiny-api/graphql";
import { JwtToken } from "../../authentication/jwtToken";
import type { Entity } from "webiny-entity";
type EntityFetcher = (context: Object) => Class;

const verifyPassword = util.promisify(bcrypt.compare);

const invalidCredentials = new ErrorResponse({
    code: "INVALID_CREDENTIALS",
    message: "Invalid credentials."
});

export default (entityFetcher: EntityFetcher) => async (
    root: any,
    args: Object,
    context: Object
) => {
    const User = entityFetcher(context);

    const user: User = (await User.findOne({
        query: { email: args.username }
    }): any);

    if (!user) {
github webiny / webiny-js / packages / webiny-api-security / src / plugins / graphql / userResolvers / loginUsingToken.js View on Github external
// @flow
import { Response, ErrorResponse } from "webiny-api/graphql";
import { JwtToken } from "../../authentication/jwtToken";
import type { Entity } from "webiny-entity";
type EntityFetcher = (context: Object) => Class;

const invalidCredentials = new ErrorResponse({
    code: "INVALID_CREDENTIALS",
    message: "Invalid credentials."
});

export default (entityFetcher: EntityFetcher) => async (
    root: any,
    args: Object,
    context: Object
) => {
    const jwt = new JwtToken({ secret: context.config.security.token.secret });

    // Decode the login token
    let email;
    try {
        const { data } = await jwt.decode(args.token);
        email = data.email;
github webiny / webiny-js / packages / webiny-api-security / src / plugins / graphql / userResolvers / loginUser.js View on Github external
// @flow
import bcrypt from "bcryptjs";
import util from "util";
import { Response, ErrorResponse } from "webiny-api/graphql";
import { JwtToken } from "../../authentication/jwtToken";

const verifyPassword = util.promisify(bcrypt.compare);

const invalidCredentials = new ErrorResponse({
    code: "INVALID_CREDENTIALS",
    message: "Invalid credentials."
});

export default () => async (root: any, args: Object, context: Object) => {
    const { SecurityUser } = context.getModels();

    const user: SecurityUser = (await SecurityUser.findOne({
        query: { email: args.username }
    }): any);

    if (!user) {
        return invalidCredentials;
    }

    // $FlowFixMe - user has a "password" attribute.