Skip to content

Instantly share code, notes, and snippets.

@anshulrgoyal
Last active May 21, 2019 18:13
Show Gist options
  • Save anshulrgoyal/e7dad78e2e1dc53f4ba53045537942b1 to your computer and use it in GitHub Desktop.
Save anshulrgoyal/e7dad78e2e1dc53f4ba53045537942b1 to your computer and use it in GitHub Desktop.
This example route for validation.
async function loginUser(userObject, callback) {
// some validation for the request body or data supplied to function
const { error, value } = Joi.validate(userObject, userObjectSchema);
if (error) {
callback(error);
} else {
try {
// check for validate of the access torken the code for it is given in pervious example
const isTokenValid = await fbHelper.validateToken(
userObject.access_token,
userObject.ffid
);
if (isTokenValid) {
// find the user in db
const userDetials = await User.findOne({ ffid: userObject.ffid });
// if yes then just issue a token for it
if (userDetials) {
try {
// code of this function is given in next example
const token = await jwtHelper.sign({
userId: userDetials._id
});
callback(null, token);
// update new access token
User.update(
{ ffid: passwordAndMail.ffid },
{ access_token: passwordAndMail.access_token }
);
} catch (errorInSign) {
callback(errorInSign);
}
} else {
// if not create new using your user model
const newUser = new User(userObject);
await newUser.save();
// issue him a new token
const token = await jwtHelper.sign({ userId: newUser._id });
callback(null, token);
}
} else {
callback("Invalid Facebook Token");
}
} catch (errorInSave) {
callback(errorInSave);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment