<commit_before>Ea         = require \everyauth
M-Sessions = require \./model/sessions
M-Users    = require \./model/users

const API-PATH = '/api/auth'

module.exports =
  init: ->
    Ea
      ..debug = true
      ..everymodule.findUserById (id, cb) -> cb! # define stub to avoid Everyauth error
      ..facebook
        .appId '1655165271375218'
        .appSecret '<KEY>'
        .entryPath "#API-PATH/facebook"
        .callbackPath "#API-PATH/facebook/callback"
        .fields 'id,name,link'
        .findOrCreateUser (session, , , user) ->
          findOrCreateUser session, \facebook, user.id, user.name
        .handleAuthCallbackError handleAuthCallbackError
        .myHostname \http://kango.com:4000
        .redirectPath '/'
      ..github
        .appId '80e325ecea5fc7f624a8'
        .appSecret '2ea910bc3649bd39ab0740b457076866008d490e'
        .entryPath "#API-PATH/github"
        .callbackPath "#API-PATH/github/callback"
        .findOrCreateUser (session, , , user) ->
          findOrCreateUser session, \github, user.id, user.name
        .handleAuthCallbackError handleAuthCallbackError
        .myHostname \http://kango.com:4000
        .redirectPath '/#/session'
        .scope ''

## helpers

function findOrCreateUser session, auth-type, id, name
  # oauth has successfully authenticated the user
  p = Ea.everymodule.Promise!

  function signin user
    M-Sessions.signin session, user
    p.fulfill user

  M-Users.findOne login_id:id, (err, user) ->
    return p.fail err if err
    return signin user if user # user found in db!
    # user doesn't exist in db so create it before signing in
    o = { login_id:id, auth_type:auth-type, name:name }
    err, user <- (new M-Users o).save
    return p.fail err if err
    signin user

  p

function handleAuthCallbackError req, res
  log \auth-cb-error, req, res
  res.send 500, \failed # TODO: improve
<commit_msg>amend to use env config<commit_after>Ea         = require \everyauth
M-Sessions = require \./model/sessions
M-Users    = require \./model/users

module.exports =
  init: ->
    env = process.env
    setup \facebook, env.OAUTH_FACEBOOK_ID, env.OAUTH_FACEBOOK_SECRET
    setup \github  , env.OAUTH_GITHUB_ID  , env.OAUTH_GITHUB_SECRET
    Ea
      ..debug = true
      ..everymodule.findUserById (id, cb) -> cb! # define stub to avoid Everyauth error
      # module specific config
      ..facebook
        .fields 'id,name,link'
      ..github
        .scope ''

    function setup ea-id, oa-id, oa-secret
      Ea[ea-id] # module common config
        .appId oa-id
        .appSecret oa-secret
        .entryPath "/api/auth/#ea-id"
        .callbackPath "/api/auth/#ea-id/callback"
        .findOrCreateUser (session, , , oa-user) ->
          # at this point, oauth has successfully authenticated the user
          p = Ea.everymodule.Promise!

          function signin user
            M-Sessions.signin session, user
            p.fulfill user

          M-Users.findOne login_id:oa-user.id, (err, user) ->
            return p.fail err if err
            return signin user if user # user found in db!
            # user doesn't exist in db so create it before signing in
            o = { login_id:oa-user.id, auth_type:auth-type, name:oa-user.name }
            err, user <- (new M-Users o).save
            return p.fail err if err
            signin user
          p
        .handleAuthCallbackError (req, res) -> res.send 500, 'openauth authentication failed'
        .myHostname "#{env.OAUTH_HOSTNAME}:#{env.PORT || 80}"
        .redirectPath '/#/session'

{'fraction_non_alphanumeric': 0.10671159786576805, 'fraction_numerical': 0.020219039595619208, 'mean_word_length': 2.209009009009009, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 14, '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': '3901899', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1117, 'n_words': 301}