How to use the apollo-server-core.ForbiddenError 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 chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
async forgotPassword(
		@Args('email') email: string,
		@Context('req') req: any
	): Promise {
		const user = await getMongoRepository(User).findOne({
			where: {
				'local.email': email,
				isVerified: true,
			},
		})

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

		const resetPassToken = await generateResetPassToken(user)

		const existedEmail = await this.emailResolver.createEmail({
			userId: user._id,
			type: Type.FORGOT_PASSWORD,
		})

		// console.log(existedEmail)

		await sendMail(
			'forgotPassword',
			user,
			req,
			resetPassToken,
github chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
async createSubscription(
		@Args('source') source: string,
		@Args('ccLast4') ccLast4: string,
		@Context('currentUser') currentUser: User
	): Promise {
		// console.log(source)
		if (currentUser.stripeId) {
			throw new ForbiddenError('stripeId already existed.')
		}
		const email = currentUser.local
			? currentUser.local.email
			: currentUser.google
			? currentUser.google.email
			: currentUser.facebook.email

		const customer = await stripe.customers.create({
			email,
			source,
			plan: STRIPE_PLAN!,
		})

		// console.log(customer)

		const user = await getMongoRepository(User).save(
github chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
async changeCreditCard(
		@Args('source') source: string,
		@Args('ccLast4') ccLast4: string,
		@Context('currentUser') currentUser: User
	): Promise {
		// console.log(source)
		if (!currentUser.stripeId || currentUser.type !== UserType.PREMIUM) {
			throw new ForbiddenError('User not found.')
		}

		await stripe.customers.update(currentUser.stripeId, {
			source,
		})

		const updateUser = await getMongoRepository(User).save(
			new User({
				...currentUser,
				ccLast4,
			})
		)

		return updateUser
	}
github chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
@Context('pubsub') pubsub: any,
		@Context('req') req: any
	): Promise {
		try {
			const { email, password } = input

			let existedUser

			existedUser = await getMongoRepository(User).findOne({
				where: {
					'local.email': email
				}
			})

			if (existedUser) {
				throw new ForbiddenError('User already exists.')
			}

			// Is there a Google account with the same email?
			existedUser = await getMongoRepository(User).findOne({
				where: {
					$or: [{ 'google.email': email }, { 'facebook.email': email }]
				}
			})

			if (existedUser) {
				// Let's merge them?

				const updateUser = await getMongoRepository(User).save(
					new User({
						...input,
						local: {
github chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
@Context('pubsub') pubsub: any,
		@Context('req') req: any
	): Promise {
		try {
			const { email, password } = input

			let existedUser

			existedUser = await getMongoRepository(User).findOne({
				where: {
					'local.email': email,
				},
			})

			if (existedUser) {
				throw new ForbiddenError('User already exists.')
			}

			// Is there a Google account with the same email?
			existedUser = await getMongoRepository(User).findOne({
				where: {
					$or: [{ 'google.email': email }, { 'facebook.email': email }],
				},
			})

			if (existedUser) {
				// Let's merge them?

				const updateUser = await getMongoRepository(User).save(
					new User({
						...input,
						local: {
github chnirt / nestjs-graphql-best-practice / src / resolvers / store.resolver.ts View on Github external
async createStore(@Args('input') input: CreateStoreInput): Promise {
		const { name } = input

		const store = await getMongoRepository(Store).findOne({ name })

		if (store) {
			throw new ForbiddenError('Store already existed.')
		}

		return await getMongoRepository(Store).save(new Store({ ...input }))
	}
}
github chnirt / nestjs-graphql-best-practice / src / resolvers / room.resolver.ts View on Github external
async room(@Args('_id') _id: string): Promise {
		const room = await getMongoRepository(Room).findOne({
			_id
		})

		if (!room) {
			throw new ForbiddenError('Room not found.')
		}

		return getMongoRepository(Room).findOne({
			_id
		})
	}
github chnirt / nestjs-graphql-best-practice / src / resolvers / user.resolver.ts View on Github external
async updateUser(
		@Args('_id') _id: string,
		@Args('input') input: UpdateUserInput
	): Promise {
		try {
			const { password } = input

			const user = await getMongoRepository(User).findOne({ _id })

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

			const updateUser = await await getMongoRepository(User).save(
				new User({
					...user,
					...input,
					local: {
						email: user.local.email,
						password: await hashPassword(password),
					},
				})
			)

			return updateUser ? true : false
		} catch (error) {
			throw new ApolloError(error)
github chnirt / nestjs-graphql-best-practice / src / resolvers / room.resolver.ts View on Github external
async joinRoom(
		@Args('_id') _id: string,
		@Context('currentUser') currentUser: User
	): Promise {
		const room = await getMongoRepository(Room).findOne({
			_id,
		})

		if (!room) {
			throw new ForbiddenError('Room not found.')
		}

		const rs = room.users.filter(item => item._id === currentUser._id)

		if (rs.length > 0) {
			throw new ForbiddenError('You joined the room.')
		}

		room.users = [...room.users, currentUser]

		return getMongoRepository(Room).save(room) ? true : false
	}
github chnirt / nestjs-graphql-best-practice / src / resolvers / room.resolver.ts View on Github external
async joinRoom(
		@Args('_id') _id: string,
		@Context('currentUser') currentUser: User
	): Promise {
		const room = await getMongoRepository(Room).findOne({
			_id
		})

		if (!room) {
			throw new ForbiddenError('Room not found.')
		}

		const rs = room.users.filter(item => item._id === currentUser._id)

		if (rs.length > 0) {
			throw new ForbiddenError('You joined the room.')
		}

		room.users = [...room.users, currentUser]

		return getMongoRepository(Room).save(room) ? true : false
	}