How to use the apollo-server-core.AuthenticationError function in apollo-server-core

To help you get started, we’ve selected a few apollo-server-core 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 danielivert / loginplify / server / resolvers / authResolver.ts View on Github external
requestVerify: async (_: any, { email }: { email: string }, ctx: any) => {
    // Check if there is a user with that email
    const user = await ctx.models.User.findOne({ email })

    if (!user) {
      throw new AuthenticationError(`No such user found for email: ${email}`)
    }

    if (user.verified) {
      throw new AuthenticationError('This user has been verified already')
    }

    // Set a reset token and expiry on that user
    const resetToken = await createRandomToken()

    // Update user adding the reset token and expiry
    const requestingUser = await ctx.models.User.updateOne(
      {
        _id: user._id
      },
      {
        ...user._doc,
        verifyToken: resetToken.randomToken,
        verifyTokenExpiry: resetToken.randomTokenExpiry
      },
      { upsert: true }
github danielivert / loginplify / server / resolvers / authResolver.ts View on Github external
login: async (
    _: any,
    { email, password }: { email: string; password: string },
    ctx: any
  ) => {
    // 1. Check if there is a user with that email
    const possibleUser = await ctx.models.User.findOne({ email })

    if (!possibleUser) {
      throw new AuthenticationError(`No such user found for email: ${email}`)
    }

    // 2. Check if their password is correct
    const valid = await bcrypt.compare(password, possibleUser.password)
    if (!valid) {
      throw new AuthenticationError('Invalid password')
    }

    // Get the user with permissions
    const user = await getUserFromId(ctx, possibleUser._id)
    const userTokenData = getUserTokenData(user)

    // Create JWT token
    const token = jwt.sign(userTokenData, APP_SECRET)

    // 4. Return the user
    return {
      token,
      user
    }
  },
  requestReset: async (_: any, { email }: { email: string }, ctx: any) => {
github apollographql / apollo-server / packages / apollo-server-integration-testsuite / src / ApolloServer.ts View on Github external
fieldWhichWillError: () => {
                throw new AuthenticationError('we the best music');
              },
            },
github chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
async resetPassword(
		@Args('resetPasswordToken') resetPasswordToken: string,
		@Args('password') password: string
	): Promise {
		const user = await getMongoRepository(User).findOne({
			resetPasswordToken,
		})

		if (!user) {
			throw new ForbiddenError('User not found.')
		}

		if (user.resetPasswordExpires < Date.now()) {
			throw new AuthenticationError(
				'Reset password token is invalid, please try again.'
			)
		}

		const updateUser = await getMongoRepository(User).save(
			new User({
				...user,
				local: {
					password: await hashPassword(password),
				},
				resetPasswordToken: null,
				resetPasswordExpires: null,
			})
		)

		return updateUser ? true : false
github birkir / prime / packages / prime-core / src / modules / internal / utils / Authorized.ts View on Github external
async ({ args, context, info }: { args: any; context: Context; info: any }, next) => {
      const { session = {} } = info || {};
      if (!context.user && !session.user) {
        throw new AuthenticationError('Must be authenticated');
      }

      if (ruleFn) {
        ruleFn(
          {
            can: (action: string, subject: any, field?: string) => {
              context.ability.throwUnlessCan(action, subject, field);
            },
          },
          args
        );
      }

      return next();
    }
  );
github danielivert / loginplify / server / resolvers / userResolver.ts View on Github external
getUsers: async (_: any, _args: any, ctx: any) => {
    const user = await userQueries.me(_, _args, ctx)

    const isAdmin = verifyAdmin(user.permissions)

    if (!isAdmin) {
      throw new AuthenticationError('You dont have permissions for that')
    }

    const users = await ctx.models.User.find({}).exec()
    return users
  },
  me: async (_: any, _args: any, ctx: any) => {
github danielivert / loginplify / server / utils / userUtils.ts View on Github external
export async function isAlreadyRegistered(ctx: any, email: string) {
  try {
    const user = await ctx.models.User.findOne({ email })
    if (user) {
      throw new AuthenticationError('User already registered')
    }

    return true
  } catch (e) {
    throw new Error(e)
  }
}
github chnirt / nestjs-graphql-best-practice / src / config / graphql / index.ts View on Github external
currentUser = await verifyToken(token)

						await getMongoRepository(User).updateOne(
							{ _id: currentUser._id },
							{
								$set: { isOnline: true },
							},
							{
								upsert: true,
							}
						)

						return { currentUser }
					}

					throw new AuthenticationError(
						'Authentication token is invalid, please try again.'
					)
				},
				onDisconnect: async (webSocket, context) => {
github danielivert / loginplify / server / utils / authUtils.ts View on Github external
export async function isAlreadyRegistered(ctx: any, email: string) {
  try {
    const user = await ctx.models.User.findOne({ email })
    if (user) {
      throw new AuthenticationError('User already registered')
    }

    return true
  } catch (e) {
    throw new Error(e)
  }
}