Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 }
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) => {
context: () => {
throw new AuthenticationError('valid result');
},
});
fieldWhichWillError: () => {
throw new AuthenticationError('we the best music');
},
},
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
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();
}
);
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) => {
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)
}
}
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) => {
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)
}
}