Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const userSchema = Schema({
- local: {
- email: {
- type: String,
- unique: true,
- },
- password: String,
- name: String
- },
- facebook: {
- id: String,
- token: String,
- email: String,
- name: String
- },
- ...
- const tokenForUser = (user) => {
- const timestamp = new Date().getTime();
- return jwt.encode({ sub: user._id, iat: timestamp }, SECRET);
- };
- const facebookSignIn = (model, cb) => {
- User.findOne({ 'facebook.id': model.id }, (err, docs) => {
- if (err) {
- return cb({ 'status': HTTP_INTERNAL_SERVER_ERROR,
- 'message': { error: 'Unable to search for an user due to an unknown error' } });
- } else if (docs) {
- return cb(null, { token: tokenForUser(docs) });
- }
- const user = new User({
- facebook: {
- id: model.id,
- token: model.accessToken,
- email: model.email,
- name: model.name
- }
- });
- user.save((err) => {
- if (err) {
- if (err.name === VALIDATION_ERROR) {
- return cb({ 'status': HTTP_PRECONDITION_FAILED,
- 'message': { error: Object.keys(err.errors).map(e => `${err.errors[e].message} `) } });
- }
- return cb({ 'status': HTTP_INTERNAL_SERVER_ERROR,
- 'message': { error: 'Unable to save user due to an unknown error' } });
- }
- return cb(null, { token: tokenForUser(user) });
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement