Sha256: 01a039ffc284b1f53d16bb36b711b194a982d48e497357d39789370516304370

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

require 'faraday'
require 'json'
require 'uri'

module Reviewed
  module Auth
    class LoginController < Sinatra::Base
      set :views, File.expand_path(File.join(File.dirname(__FILE__), "views"))
      # use Rack::Session::Cookie, secret: '0Shitt4dL3yTYe0VXfCDmQ', key: "reviewed.auth.session"

      get "/assets/*" do
        file = params[:splat].join("/")
        ext = File.extname(file)
        case ext.downcase
        when ".js"
          content_type "application/javascript"
        when ".css"
          content_type "text/css"
        end
        File.read(File.expand_path(File.join(File.dirname(__FILE__), "assets", file)))
      end

      get "/" do
        response.delete_cookie(:_reviewed_token)
        erb :login_form
      end

      post "/" do
        conn = Faraday.new
        auth_url = File.join(Reviewed::Auth.api_host_path, "authentications")
        resp = conn.post(auth_url, params)
        if resp.status == 200
          @user = OpenStruct.new(JSON.parse(resp.body))
          if params[:drop_cookie] == "true"
            response.set_cookie(:_reviewed_token, value: @user.token, path: "/")
          end
          if params[:return_url] != ""
            url = params[:return_url]
            if params[:drop_cookie] != "true"
              if url.match(/\?/)
                url += "&_reviewed_token=#{@user.token}"
              else
                url += "?_reviewed_token=#{@user.token}"
              end
            end
            redirect url
          end
          "Welcome: #{@user.name}"
        else
          resp.body
          @error = JSON.parse(resp.body)["errors"]
          @error
          erb :login_form
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reviewed-auth-0.0.2 lib/reviewed-auth/login_controller.rb
reviewed-auth-0.0.1 lib/reviewed-auth/login_controller.rb