<commit_before>const config = require('config');

module.exports = (app) => {
  const errors = app.errors;
  const models = app.get('models');
  const ConnectedAccount = models.ConnectedAccount;
  const User = models.User;


  return {
    createOrUpdate: (req, res, next, accessToken, profile, emails) => {
      var caId, user;
      const attrs = { provider: req.params.service };

      // TODO should simplify using findOrCreate but need to upgrade Sequelize to have this fix:
      // https://github.com/sequelize/sequelize/issues/4631
      User.findOne({ where: { email: { $in: emails }}})
        .then(u => u || User.create({
          name: profile.displayName,
          avatar: profile.avatar_url,
          email: emails[0]
        }))
        .tap(u => user = u)
        .tap(user => attrs.UserId = user.id)<commit_msg>add avatar for oauth user as well (#365)<commit_after>const config = require('config');

module.exports = (app) => {
  const errors = app.errors;
  const models = app.get('models');
  const ConnectedAccount = models.ConnectedAccount;
  const User = models.User;


  return {
    createOrUpdate: (req, res, next, accessToken, profile, emails) => {
      var caId, user;
      const attrs = { provider: req.params.service };
      var avatar;
      if (req.params.service === 'github'){
        avatar = `http://avatars.githubusercontent.com/${profile.username}`;
      }

      // TODO should simplify using findOrCreate but need to upgrade Sequelize to have this fix:
      // https://github.com/sequelize/sequelize/issues/4631
      User.findOne({ where: { email: { $in: emails }}})
        .then(u => u || User.create({
          name: profile.displayName,
          avatar: avatar || profile.avatar_url,
          email: emails[0]
        }))
        .tap(u => user = u)
        .tap(user => attrs.UserId = user.id)
{'fraction_non_alphanumeric': 0.1398488120950324, 'fraction_numerical': 0.007019438444924406, 'mean_word_length': 2.619140625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '236991', 'n_tokens_mistral': 580, 'n_tokens_neox': 560, 'n_words': 148}