<commit_before>_ = require 'lodash'
Rx = require 'rx-lite'
log = require 'loga'

config = require '../config'

module.exports = class Auth
  constructor: ({@netox, cookieSubject}) ->
    initialAuthPromise = null

    @accessTokenStreams = new Rx.ReplaySubject(1)
    @accessTokenStreams.onNext Rx.Observable.defer =>
      unless initialAuthPromise?
        cookieAccessToken = cookieSubject.getValue()[config.AUTH_COOKIE]

        initialAuthPromise = (if cookieAccessToken?
          @netox.stream config.API_URL + '/demo/users/me',
            headers:
              Authorization: "Token #{cookieAccessToken}"
          .take(1).toPromise()
          .catch =>
            @netox.fetch config.API_URL + '/demo/users/me',
              method: 'POST'
              isIdempotent: true
        else
          @netox.fetch config.API_URL + '/demo/users/me',
            method: 'POST'
            isIdempotent: true
        ).then ({accessToken}) -> accessToken
      return initialAuthPromise

    @accessTokens = @accessTokenStreams.switch()
    .doOnNext (accessToken) ->
      cookies = {}
      cookies[config.AUTH_COOKIE] = accessToken
      cookieSubject.onNext \
        _.defaults cookies, cookieSubject.getValue()

  stream: (url, opts) =>
    @accessTokens
    .flatMapLatest (accessToken) =>
      @netox.stream url, _.merge {
        headers:
          Authorization: "Token #{accessToken}"
      }, opts

  fetch: (url, opts) =>
    @accessTokens.take(1).toPromise()
    .then (accessToken) =>
      @netox.fetch url, _.merge {
        headers:
          Authorization: "Token #{accessToken}"
      }, opts

  loginAnon: =>
    @fetch config.API_URL + '/demo/users/me',
      method: 'POST'
      isIdempotent: true
    .then (user) =>
      @accessTokenStreams.onNext Rx.Observable.just user.accessToken
      return user<commit_msg>auth: cleaned up to make clearer how to extend<commit_after>_ = require 'lodash'
Rx = require 'rx-lite'
log = require 'loga'

config = require '../config'

module.exports = class Auth
  constructor: ({@netox, cookieSubject}) ->
    initialAuthPromise = null

    @accessTokenStreams = new Rx.ReplaySubject(1)
    @accessTokenStreams.onNext Rx.Observable.defer =>
      if initialAuthPromise?
        return initialAuthPromise

      cookieAccessToken = cookieSubject.getValue()[config.AUTH_COOKIE]

      return initialAuthPromise = (if cookieAccessToken?
        @netox.stream config.API_URL + '/demo/users/me',
          headers:
            Authorization: "Token #{cookieAccessToken}"
        .take(1).toPromise()
        .catch =>
          @loginAnon()
      else
        @loginAnon()
      ).then ({accessToken}) ->
        accessToken

    @accessTokens = @accessTokenStreams.switch()
    .doOnNext (accessToken) ->
      cookies = {}
      cookies[config.AUTH_COOKIE] = accessToken
      cookieSubject.onNext \
        _.defaults cookies, cookieSubject.getValue()

  setAccessToken: (accessToken) =>
    @accessTokenStreams.onNext Rx.Observable.just accessToken

  stream: (url, opts) =>
    @accessTokens
    .flatMapLatest (accessToken) =>
      @netox.stream url, _.merge {
        headers:
          Authorization: "Token #{accessToken}"
      }, opts

  fetch: (url, opts) =>
    @accessTokens.take(1).toPromise()
    .then (accessToken) =>
      @netox.fetch url, _.merge {
        headers:
          Authorization: "Token #{accessToken}"
      }, opts

  loginAnon: =>
    @netox.fetch config.API_URL + '/demo/users/me',
      method: 'POST'
      isIdempotent: true
{'fraction_non_alphanumeric': 0.12096317280453257, 'fraction_numerical': 0.0016997167138810198, 'mean_word_length': 2.506454816285998, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '1965967', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1040, 'n_words': 224}