Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// @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) {
// @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;
// @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.