* fix(models.user): Colocate all user methods Moved user methods/extensions into one file. Tracked down `next method called more than once` error and setting headers after their sent. Let regular error handler handle api errors as well. * feat(server.auth): Disable github account creation We are no longer allowing account creation through github * refactor(Auth): Move user identity link into models dir * feat(Disable link account login): This removes the ability to use a linked account t * feat(errorhandlers): Add opbeat, filter out handled error
		
			
				
	
	
		
			75 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
import passport from 'passport';
 | 
						|
import { PassportConfigurator } from 'loopback-component-passport';
 | 
						|
import passportProviders from './passport-providers';
 | 
						|
 | 
						|
const passportOptions = {
 | 
						|
  emailOptional: true,
 | 
						|
  profileToUser: null
 | 
						|
};
 | 
						|
 | 
						|
const fields = {
 | 
						|
  progressTimestamps: false,
 | 
						|
  completedChallenges: false,
 | 
						|
  challengeMap: false
 | 
						|
};
 | 
						|
 | 
						|
PassportConfigurator.prototype.init = function passportInit(noSession) {
 | 
						|
  this.app.middleware('session:after', passport.initialize());
 | 
						|
 | 
						|
  if (noSession) {
 | 
						|
    return;
 | 
						|
  }
 | 
						|
 | 
						|
  this.app.middleware('session:after', passport.session());
 | 
						|
 | 
						|
  // Serialization and deserialization is only required if passport session is
 | 
						|
  // enabled
 | 
						|
 | 
						|
  passport.serializeUser((user, done) => {
 | 
						|
    done(null, user.id);
 | 
						|
  });
 | 
						|
 | 
						|
  passport.deserializeUser((id, done) => {
 | 
						|
 | 
						|
    this.userModel.findById(id, { fields }, (err, user) => {
 | 
						|
      if (err || !user) {
 | 
						|
        return done(err, user);
 | 
						|
      }
 | 
						|
      return this.app.dataSources.db.connector
 | 
						|
        .collection('user')
 | 
						|
        .aggregate([
 | 
						|
          { $match: { _id: user.id } },
 | 
						|
          { $project: { points: { $size: '$progressTimestamps' } } }
 | 
						|
        ], function(err, [{ points = 1 } = {}]) {
 | 
						|
          if (err) { return done(err); }
 | 
						|
          user.points = points;
 | 
						|
          return done(null, user);
 | 
						|
        });
 | 
						|
    });
 | 
						|
  });
 | 
						|
};
 | 
						|
 | 
						|
export default function setupPassport(app) {
 | 
						|
  const configurator = new PassportConfigurator(app);
 | 
						|
 | 
						|
  configurator.setupModels({
 | 
						|
    userModel: app.models.user,
 | 
						|
    userIdentityModel: app.models.userIdentity,
 | 
						|
    userCredentialModel: app.models.userCredential
 | 
						|
  });
 | 
						|
 | 
						|
  configurator.init();
 | 
						|
 | 
						|
  Object.keys(passportProviders).map(function(strategy) {
 | 
						|
    var config = passportProviders[strategy];
 | 
						|
    config.session = config.session !== false;
 | 
						|
    configurator.configureProvider(
 | 
						|
      strategy,
 | 
						|
      {
 | 
						|
        ...config,
 | 
						|
        ...passportOptions
 | 
						|
      }
 | 
						|
    );
 | 
						|
  });
 | 
						|
}
 |