<commit_before>require 'sinatra/cookies'
require File.expand_path 'app/auth/auth.rb'
require File.expand_path 'app/api/errors.rb'

# Takes care of omniauth callbacks
class OmniauthController < Sinatra::Base
  helpers Sinatra::Cookies
  OAUTH_TIMEOUT = ENV['OAUTH_TIMEOUT'] || (30)

  # Generic oauth callback
  get '/auth/:provider/callback' do
    one = User.send "find_or_create_from_#{params[:provider]}", auth_hash, request.env['omniauth.params']
    url = Squareteam::Application::CONFIG.app_url
    if one.nil?
      path = '/#/register'
    else
      if one.valid?
        identifier = one.email
        oauth_token = auth_hash.credentials.token
        # Validity of the oauth_token is OAUTH_TIMEOUT
        Auth.cache.set "#{identifier}:OAUTH", OAUTH_TIMEOUT, oauth_token
        response.set_cookie 'st.oauth', value: oauth_token, expires: Time.now + OAUTH_TIMEOUT, path: '/'
        path = "/#/login?email=#{identifier}"
      else
        errors = one.errors
        one = User.find_by_email(one.email) if one.email
        if one.nil?
          path = "/#/login?errors=#{errors.messages}"
        else
          path = "/#/login?errors=#{['api.st_account_exist'].to_json}"
        end
      end

    end

    redirect "#{url}#{path}&provider=#{params[:provider]}"
  end

  private

  # Standard for oauth hash information
  # https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
  def auth_hash
    request.env['omniauth.auth']
  end
end
<commit_msg>feat(oauth): fix errors handling and adapt with front<commit_after>require 'sinatra/cookies'
require File.expand_path 'app/auth/auth.rb'
require File.expand_path 'app/api/errors.rb'

# Takes care of omniauth callbacks
class OmniauthController < Sinatra::Base
  helpers Sinatra::Cookies
  OAUTH_TIMEOUT = ENV['OAUTH_TIMEOUT'] || (30)

  # Generic oauth callback
  get '/auth/:provider/callback' do
    one = User.send "find_or_create_from_#{params[:provider]}", auth_hash, request.env['omniauth.params']
    url = Squareteam::Application::CONFIG.app_url
    if one.nil?
      path = '/#/register'
    else
      if one.valid?
        identifier = one.email
        oauth_token = auth_hash.credentials.token
        # Validity of the oauth_token is OAUTH_TIMEOUT
        Auth.cache.set "#{identifier}:OAUTH", OAUTH_TIMEOUT, oauth_token
        response.set_cookie 'st.oauth', value: oauth_token, expires: Time.now + OAUTH_TIMEOUT, path: '/'
        query_string = { email: identifier }.to_query
      else
        query_string = { errors: one.errors.messages }.to_query
      end
      path = "/#/login?#{query_string}"
    end

    redirect "#{url}#{path}&provider=#{params[:provider]}"
  end

  private

  # Standard for oauth hash information
  # https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
  def auth_hash
    request.env['omniauth.auth']
  end
end

{'fraction_non_alphanumeric': 0.12447108603667137, 'fraction_numerical': 0.0014104372355430183, 'mean_word_length': 3.291981845688351, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '1543862', 'n_tokens_mistral': 926, 'n_tokens_neox': 923, 'n_words': 195}