Friday 17 March 2017

Api stops working after authenticating

My api works perfectly until you authenticate. After authenticating all the services stop responding, and worst of all it does not return any errors. The only clue I have is that it has something to do with the sessions, because when I delete all sessions from the database, the services go back to working instantly. I have been trying for 4 days and have not made any progress. I am using PassportJS with the Instagram Token Strategy to authenticate my users.

index.js

import config from './config/config';
import app from './config/express';

const debug = require('debug')('express-mongoose-es6-rest-api:index');

if (!module.parent) {
  // listen on port config.port
  app.listen(config.port, () => {
    debug(`server started on port ${config.port} (${config.env})`);
  });
}

export default app;

passport.js

import _ from 'lodash';
import passport from 'passport';
import httpStatus from 'http-status';
import InstagramTokenStrategy from 'passport-instagram-token';

import config from './config';
import APIError from '../server/helpers/APIError';
import User from '../server/models/user.model';

passport.serializeUser((user, next) => next(null, user._id));
passport.deserializeUser((id, next) => User.get(id, (err, user) => next(err, user)));

passport.use(new InstagramTokenStrategy({
  clientID: config.instagram.id,
  clientSecret: config.instagram.secret,
  passReqToCallback: true
}, function(req, accessToken, refreshToken, profile, next) {
  User.findOne({ 'instagram.id': profile.id }, (err, user) => {
    if (err) {
      return next(err);
    }

    if (req.user) {
      if (user) {
        err = new APIError('There is already an instagram account that belongs to you!', httpStatus.INTERNAL_SERVER_ERROR);
        return next(err);
      } else {
        const token = _.find(user.tokens, (token) => token.kind === 'instagram');
        token.accessToken = accessToken;
        user.instagram.username = profile.username;
        user.profile.name = profile.displayName;
        user.profile.website = profile._json.data.website;
        user.profile.picture = profile._json.data.profile_picture;
        return user.save((err) => next(err, user));
      }
    } else {
      if (user) {
        const token = _.find(user.tokens, (token) => token.kind === 'instagram');
        token.accessToken = accessToken;
        user.instagram.username = profile.username;
        user.profile.name = profile.displayName;
        user.profile.website = profile._json.data.website;
        user.profile.picture = profile._json.data.profile_picture;
        return user.save((err) => next(err, user));
      } else {
        user = new User();
        user.tokens.push({ kind: 'instagram', accessToken });
        user.instagram.id = profile.id;
        user.instagram.username = profile.username;
        user.profile.name = profile.displayName;
        user.profile.website = profile._json.data.website;
        user.profile.picture = profile._json.data.profile_picture;
        return user.save((err) => next(err, user));
      }
    }
  });
}));

export default passport;

authCtrl.js

import httpStatus from 'http-status';

import APIError from '../helpers/APIError';
import config from '../../config/config';
import passport from '../../config/passport';

function login(req, res, next) {
  passport.authenticate('instagram-token', (err, user) => {
    if (err) {
      return next(err);
    }

    if (!user) {
      err = new ApiError('No such user exists!', httpStatus.NOT_FOUND);
      return next(err);
    }

    req.login((user), (err) => {
      if (err) {
        return next(err);
      }

      return res.json(user);
    })
  })(req, res, next);
}

function logout(req, res, next) {
  req.logout();
  res.sendStatus(httpStatus.NO_CONTENT);
}

export default { login, logout };



via Ibn Claudius

No comments:

Post a Comment