How to use the koa-passport.serializeUser function in koa-passport

To help you get started, we’ve selected a few koa-passport examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hongymagic / k2 / src / passport.js View on Github external
// @flow

import passport from 'koa-passport';
import { Strategy } from 'passport-local';
import User from './models/User';

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findOne({ id }).then(user => done(null, user || null), done);
});

passport.use(
  'local',
  new Strategy(
    {
      usernameField: 'email',
      passwordField: 'password',
      session: false,
    },
    async (email, password, done) => {
github elementary / houston / src / houston / passport / index.js View on Github external
export function setup (server) {
  // TODO: Serialize user data in the database?
  passport.serializeUser((user, done) => {
    done(null, user._id)
  })

  passport.deserializeUser((id, done) => {
    User.findById(id)
    .then((user) => done(null, user))
    .catch((error) => done(error))
  })

  passport.use(github.strategy)

  server.use(passport.initialize())
  server.use(passport.session())

  log.debug('Passport setup complete')
}
github zalando / zappr / server / passport / passport.js View on Github external
export function init(Strategy = GithubStrategy) {
  const GITHUB_CLIENT_ID = nconf.get('GITHUB_CLIENT_ID')
  const GITHUB_CLIENT_SECRET = nconf.get('GITHUB_CLIENT_SECRET')
  const GITHUB_UI_URL = nconf.get('GITHUB_UI_URL')
  const GITHUB_API_URL = nconf.get('GITHUB_API_URL')
  const HOST_ADDR = nconf.get('HOST_ADDR')

  /**
   * Serialize user data into the session.
   */
  passport.serializeUser((data, done) => {
    log(`serializeUser id: ${data.id}`)
    done(null, data)
  })

  /**
   * Deserialize user profile out of the session.
   */
  passport.deserializeUser((data, done) => {
    log(`deserializeUser id: ${data.id}`)
    User.findById(data.id)
        .then(user => user
          ? user.toJSON()
          : null)
        .then(user => user
          ? done(null, {...user, ...data})
          : done(new Error(`no user for id ${data.id}`)))
github TryStarboard / Starboard / source / server / util / auth.js View on Github external
passport.use('local-signup', new LocalStrategy(opts, wrap(function *(email, password, done) {
  try {
    const [user] = yield db.select('email').from('users').where({ email }).limit(1);
    if (user) {
      throw new EmailExistError();
    }
    const hash = yield hashAsync(password, 10);
    const [id] = yield db('users').insert({ email, hash }, 'id');
    done(null, { id });
  } catch (err) {
    done(err);
  }
})));

passport.serializeUser(function (user, done) {
  done(null, user.id);
});

passport.deserializeUser(wrap(function *(id, done) {
  try {
    const [user] = yield db('users').select('id').where({ id }).limit(1);
    if (!user) {
      throw new Error('user does not exist');
    }
    done(null, user);
  } catch (err) {
    done(err);
  }
}));

export const authInit = passport.initialize();
github velop-io / server / src / classes / PassportHandler.js View on Github external
initStrategies() {
    if (this.strategies.length > 0) {
      this.parent.parent.logger.info(
        "[VelopServer][Passport] Initialize " +
          this.strategies.length +
          " passport strategies"
      );

      this.parent.app.use(passport.initialize());
      this.parent.app.use(passport.session());

      passport.serializeUser((user, done) => done(null, user));
      passport.deserializeUser((user, done) => done(null, user));

      this.strategies.map(strategy => passport.use(strategy));
    }
  }
github moovel / teamchatviz / server / auth.js View on Github external
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
  USA
*/

import passport from 'koa-passport';
import PassportSlack from 'passport-slack';
import config from './config';
import db from './db';
import { save, getById, updateAccessToken } from './repositories/user';

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  getById(id)
    .then(user => done(null, user))
    .catch(done);
});

const handleUser = (accessToken, refreshToken, profile, done) => {
  profile.accessToken = accessToken;
  profile.refreshToken = refreshToken;
  return getById(profile.id)
    .then(user => {
      if (!user) {
        const user = {
github lorenzopicoli / node-api-starter / config / passport.js View on Github external
import { User } from 'models/users'
import { Strategy as LocalStrategy } from 'passport-local'
import FacebookTokenStrategy from 'passport-facebook-token'
import passport from 'koa-passport'
import config from './index'

passport.serializeUser((user, done) => {
  done(null, user.id)
})

passport.deserializeUser(async (id, done) => {
  try {
    const user = await User.where({ id }).fetch()
    done(null, user)
  } catch (err) {
    done(err)
  }
})

passport.use('local', new LocalStrategy({
  usernameField: 'email',
  passwordField: 'password'
}, async (email, password, done) => {
github SystangoTechnologies / koach-sql / config / passport.js View on Github external
import passport from 'koa-passport'
import db from '../src/models/index'
import { Strategy } from 'passport-local'

passport.serializeUser((user, done) => {
	done(null, user.id)
})

passport.deserializeUser(async (id, done) => {
	try {
		const user = await db.user.findOne({
			where: {
				id: id
			},
			attributes: { exclude: ['password'] }
		})
		done(null, user)
	} catch (err) {
		done(err)
	}
})
github rusty1s / koa2-rest-api / server / auth / index.js View on Github external
'use strict';

import passport from 'koa-passport';
import compose from 'koa-compose';
import importDir from 'import-dir';
import User from '../models/user';
import { prefix } from '../api/config';
import * as provider from './provider';

const strategies = importDir('./strategies');

Object.keys(strategies).forEach(name => {
  passport.use(name, strategies[name]);
});

passport.serializeUser((user, done) => done(null, user._id));

passport.deserializeUser((id, done) => {
  (async () => {
    try {
      const user = await User.findById(id);
      done(null, user);
    } catch (error) {
      done(error);
    }
  })();
});

export default function auth() {
  return compose([
    passport.initialize(),
    passport.session(),
github keattang / eks-auth-proxy / src / passport.js View on Github external
const configurePassport = async () => {
    passport.use('oidc', await oidc.getPassportStrategy());
    passport.serializeUser(handleSerializeUser);
    passport.deserializeUser(handleDeserializeUser);
};