Sha256: c259e15c737b4d9ad1e01a5895354cafd314c938b3f50f39aefc7502e5b9fb1c

Contents?: true

Size: 1.08 KB

Versions: 26

Compression:

Stored size: 1.08 KB

Contents

require_dependency "writefully/application_controller"

module Writefully
  class SessionsController < ApplicationController
    def new
      @github_app = Writefully.github_app
    end

    def create
      @auth = Writefully.github_app.get_token(params[:code])
      @github = Github.new oauth_token: @auth.token
      @github_user = @github.users.get
      @owner  = find_or_create_owner 

      refresh_token
      session[:wf_authorship_id] = @owner.id
      redirect_to root_path, notice: 'signed_in'
    end

    def destroy
      session[:wf_authorship_id] = nil
      redirect_to root_path, notice: 'signed_out'
    end

    protected

    def refresh_token
      @owner.update_attributes(data: @owner.data.merge({ auth_token: @auth.token }))
    end

    def find_or_create_owner
      find_owner || create_owner
    end

    def find_owner
      Authorship.find_by_uid(@github_user.id)
    end

    def create_owner
      if Authorship.count == 0
        Authorship.create_from_data(@github_user)
      else
        return redirect_to root_path, notice: 'owner_exists'
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
writefully-0.8.2 app/controllers/writefully/sessions_controller.rb
writefully-0.8.1 app/controllers/writefully/sessions_controller.rb
writefully-0.8.0 app/controllers/writefully/sessions_controller.rb
writefully-0.7.1 app/controllers/writefully/sessions_controller.rb
writefully-0.6.12 app/controllers/writefully/sessions_controller.rb
writefully-0.6.11 app/controllers/writefully/sessions_controller.rb
writefully-0.6.10 app/controllers/writefully/sessions_controller.rb
writefully-0.6.9 app/controllers/writefully/sessions_controller.rb
writefully-0.6.7 app/controllers/writefully/sessions_controller.rb
writefully-0.6.6 app/controllers/writefully/sessions_controller.rb
writefully-0.6.5 app/controllers/writefully/sessions_controller.rb
writefully-0.6.4 app/controllers/writefully/sessions_controller.rb
writefully-0.6.3 app/controllers/writefully/sessions_controller.rb
writefully-0.6.2 app/controllers/writefully/sessions_controller.rb
writefully-0.5.1 app/controllers/writefully/sessions_controller.rb
writefully-0.5.0 app/controllers/writefully/sessions_controller.rb
writefully-0.4.10 app/controllers/writefully/sessions_controller.rb
writefully-0.4.8 app/controllers/writefully/sessions_controller.rb
writefully-0.4.7 app/controllers/writefully/sessions_controller.rb
writefully-0.4.6 app/controllers/writefully/sessions_controller.rb