<commit_before>require! {
  passport
  GithubStrategy: 'passport-github'.Strategy
}

require! {
  './sequelize'
  User: '../server/models/user'
}

module.exports = passport

# Serialize the user id to push into the session
passport.serializeUser !(user, done) ->
  done void, user.id

# Deserialize the user object based on a pre-serialized token
 # which is the user id
passport.deserializeUser !(id, done) ->
  User
    .find id
    .error done
    .then done.bind @, void


# Use github strategy
const config = do
  clientID:     '3e06d2279155f8910bae'
  clientSecret: '<KEY>' #process.env.GITHUB_CLIENT_SECRET
  callbackURL:  'http://localhost:5000/users/auth/github/callback'

passport.use new GithubStrategy config, !(accessToken, refreshToken, profile, done) ->
  const userFetched = done.bind @, void

  sequelize.query 'SELECT * FROM "Users" WHERE "Users"."github" @> \'id=>' + profile.id + '\'', User
  .error done
  .then (users) ->
    return userFetched users.0 if users.length > 0

    User.create do
      username:     profile.username
      displayName:  profile.displayName
      email:        profile.emails.0.value
      github: do
        id:           profile.id
        accessToken:  accessToken
        refreshToken: refreshToken
  .then userFetched<commit_msg>fix(passport): get github config from env<commit_after>require! {
  passport
  GithubStrategy: 'passport-github'.Strategy
}

require! {
  './sequelize'
  User: '../server/models/user'
}

module.exports = passport

# Serialize the user id to push into the session
passport.serializeUser !(user, done) ->
  done void, user.id

# Deserialize the user object based on a pre-serialized token
# which is the user id
passport.deserializeUser !(id, done) ->
  User
    .find id
    .error done
    .then done.bind @, void

# Use github strategy
const config = do
  clientID:     '3e06d2279155f8910bae'
  clientSecret: '<KEY>'
  callbackURL:  'http://localhost:5000/users/auth/github/callback'

if 'production' is process.env.NODE_ENV
  config.clientID     = process.env.GITHUB_CALLBACK_URL
  config.clientSecret = process.env.GITHUB_CLIENT_ID
  config.callbackURL  = process.env.GITHUB_CLIENT_SECRET

passport.use new GithubStrategy config, !(accessToken, refreshToken, profile, done) ->
  const userFetched = done.bind @, void

  sequelize.query 'SELECT * FROM "Users" WHERE "Users"."github" @> \'id=>' + profile.id + '\'', User
  .error done
  .then (users) ->
    return userFetched users.0 if users.length > 0

    User.create do
      username:     profile.username
      displayName:  profile.displayName
      email:        profile.emails.0.value
      github: do
        id:           profile.id
        accessToken:  accessToken
        refreshToken: refreshToken
  .then userFetched
{'fraction_non_alphanumeric': 0.10762007945106537, 'fraction_numerical': 0.015167930660888408, 'mean_word_length': 3.128166915052161, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, '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': '5141153', 'n_tokens_mistral': 911, 'n_tokens_neox': 885, 'n_words': 237}