Permalink
Browse files

Refactor LoginController to be RESTful and related changes

  • Loading branch information...
1 parent a745f21 commit 053fa1669de63e07b8b80cfb4d08de25261c6b44 Gaurav Chande committed Sep 27, 2012
View
6 README.rdoc
@@ -5,7 +5,7 @@
This gem makes it easy to get a Rails 3.2 app up and running with the
Shopify API.
-The generator creates a basic login controller for authenticating with your
+The generator creates a basic sessions controller for authenticating with your
shop and a HomeController which displays basic information about your
products, orders and articles.
@@ -26,8 +26,8 @@ If you don't have an API key yet, create a Shopify Partner account at
http://shopify.com/partners and create an app. You can also create test shops
once you're logged in as a partner.
-When you create your app in the Shopify Partner Account, set the return URL to
-<tt>http://localhost:3000/login/finalize</tt>
+When you create your app in the Shopify Partner Account, set the Application URL to
+<tt>http://localhost:3000/login</tt>
You can also create a private application that only works for your shop by
visiting https://YOUR-SHOP.myshopify.com/admin/api
View
2 install.rb
@@ -7,7 +7,7 @@
puts " * Go to http://www.shopify.com/partners and create or login to your Partner account."
puts
puts " * Jump over to the Apps tab and hit the 'Create a new app' button"
-puts " (Make sure to set the Return URL to http://localhost:3000/login/finalize during development)"
+puts " (Make sure to set the Application URL to http://localhost:3000/login during development)"
puts
puts " * Install the Shopify API gem:
View
4 lib/generators/shopify_app/README
@@ -3,8 +3,8 @@
What's next?
------------
-Make sure to set the return URL of the application your Shopify partner account
-to <tt>http://localhost:3000/login/finalize</tt>.
+Make sure to set the Application URL of the application your Shopify partner account
+to <tt>http://localhost:3000/login</tt>.
Then, start your application with:
View
6 lib/generators/shopify_app/USAGE
@@ -2,15 +2,15 @@ Description:
This is a generator for creating a basic Shopify application to quickly get
you started. You can see some examples on how to use the Shopify API.
- The generator creates a basic login controller for authenticating with your
+ The generator creates a basic sessions controller for authenticating with your
Shop and a controller called "home" which displays basic information
about your products, orders and articles.
Note: It's recommended to use this on a new Rails project, so that the
generator won't overwrite/delete some of your files.
Usage:
- Pass your API key and then your Secret, which the login controller
+ Pass your API key and then your Secret, which the sessions controller
will need to authenticate with your shop.
If you don't have an API key yet, register your application in our
partner system at http://www.shopify.com/partners
@@ -19,4 +19,4 @@ Usage:
Examples:
script/generate shopify_app edffbb1bb793e2750686e6f4647a384a abbcee050...
- This will create a login controller and a home controller and views.
+ This will create a sessions controller and a home controller and views.
View
24 lib/generators/shopify_app/shopify_app_generator.rb
@@ -40,19 +40,27 @@ def add_bootstrap_gem
def add_routes
unless options[:skip_routes]
- route "root :to => 'home#index'"
- route "match 'login/logout' => 'login#logout', :as => :logout"
- route "match 'login/finalize' => 'login#finalize', :as => :finalize"
- route "match 'login/authenticate' => 'login#authenticate', :as => :authenticate"
- route "match 'login' => 'login#index', :as => :login"
- route "match 'design' => 'home#design'"
- route "match 'welcome' => 'home#welcome'"
- route "match 'auth/shopify/callback' => 'login#finalize'"
+ route_without_newline "root :to => 'home#index'"
+ route "end"
+ route_without_newline " delete 'logout' => :destroy"
+ route_without_newline " get 'auth/shopify/callback' => :show"
+ route_without_newline " post 'login' => :create"
+ route_without_newline " get 'login' => :new"
+ route_without_newline "controller :sessions do"
+ route "match 'design' => 'home#design'"
+ route_without_newline "match 'welcome' => 'home#welcome'"
end
end
def display_readme
`bundle install`
readme '../README'
end
+
+ private
+
+ def route_without_newline(routing_code)
+ sentinel = /\.routes\.draw do(?:\s*\|map\|)?\s*$/
+ inject_into_file 'config/routes.rb', "\n #{routing_code}", { after: sentinel, verbose: false }
+ end
end
View
2 lib/generators/shopify_app/templates/app/controllers/home_controller.rb
@@ -4,7 +4,7 @@ class HomeController < ApplicationController
def welcome
current_host = "#{request.host}#{':' + request.port.to_s if request.port != 80}"
- @callback_url = "http://#{current_host}/login/finalize"
+ @callback_url = "http://#{current_host}/login"
end
def index
View
41 ...lates/app/controllers/login_controller.rb → ...es/app/controllers/sessions_controller.rb
@@ -1,52 +1,49 @@
-class LoginController < ApplicationController
- def index
- # Ask user for their #{shop}.myshopify.com address
-
- # If the #{shop}.myshopify.com address is already provided in the URL, just skip to #authenticate
- if params[:shop].present?
- redirect_to authenticate_path(:shop => params[:shop])
- end
+class SessionsController < ApplicationController
+ def new
+ authenticate if params[:shop].present?
end
- def authenticate
- if shop_name = sanitize_shop_param(params)
- redirect_to "/auth/shopify?shop=#{shop_name}"
- else
- redirect_to return_address
- end
+ def create
+ authenticate
end
- def finalize
+ def show
if response = request.env['omniauth.auth']
- sess = ShopifyAPI::Session.new(params['shop'], response['credentials']['token'])
+ sess = ShopifyAPI::Session.new(params[:shop], response[:credentials][:token])
session[:shopify] = sess
flash[:notice] = "Logged in"
redirect_to return_address
- session[:return_to] = nil
else
flash[:error] = "Could not log in to Shopify store."
- redirect_to :action => 'index'
+ redirect_to :action => 'new'
end
end
- def logout
+ def destroy
session[:shopify] = nil
flash[:notice] = "Successfully logged out."
- redirect_to :action => 'index'
+ redirect_to :action => 'new'
end
protected
+ def authenticate
+ if shop_name = sanitize_shop_param(params)
+ redirect_to "/auth/shopify?shop=#{shop_name}"
+ else
+ redirect_to return_address
+ end
+ end
+
def return_address
session[:return_to] || root_url
end
-
+
def sanitize_shop_param(params)
return unless params[:shop].present?
name = params[:shop].to_s.strip
name += '.myshopify.com' if !name.include?("myshopify.com") && !name.include?(".")
name.sub('https://', '').sub('http://', '')
end
-
end
View
2 .../templates/app/views/login/index.html.erb → ...templates/app/views/sessions/new.html.erb
@@ -4,7 +4,7 @@
</div>
<div style="width:340px; margin:0 auto;">
- <%= form_tag '/login/authenticate', :class => 'form-search' do %>
+ <%= form_tag login_path, :class => 'form-search' do %>
<%= text_field_tag 'shop', nil, :placeholder => 'Shop URL', :class => 'btn-large' %>
<%= submit_tag 'Install', :class => 'btn btn-primary btn-large' %>
<% end %>
View
2 shopify_app.gemspec
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
s.email = ["edward@shopify.com", "willem@shopify.com", "david.underwood@shopify.com"]
s.homepage = "http://www.shopify.com/developers"
s.summary = %q{This gem is used to get quickly started with the Shopify API}
- s.description = %q{Creates a basic login controller for authenticating with your Shop and also a product controller which lets your edit your products easily.}
+ s.description = %q{Creates a basic sessions controller for authenticating with your Shop and also a product controller which lets your edit your products easily.}
s.rubyforge_project = "shopify-api"

0 comments on commit 053fa16

Please sign in to comment.