From f393db2ef1e1e0a4f87fa2e76379b7b4e01cf36a Mon Sep 17 00:00:00 2001 From: junv Date: Sat, 18 Jan 2014 21:06:30 +0800 Subject: [PATCH] add json API based client login auth --- .gitignore | 4 +++- Gemfile | 1 + Gemfile.lock | 2 ++ README.md | 2 +- app/assets/javascripts/api/v1/api.js | 2 ++ app/assets/stylesheets/api/v1/api.css | 4 ++++ app/controllers/api/v1/sessions_controller.rb | 17 +++++++++++++++++ app/controllers/book_controller.rb | 2 +- app/helpers/api/v1/sessions_helper.rb | 2 ++ config/database.yml | 4 ++-- config/initializers/devise.rb | 2 ++ config/routes.rb | 12 +++++++++++- libr_development | Bin 53248 -> 53248 bytes .../api/v1/sessions_controller_spec.rb | 5 +++++ spec/helpers/api/v1/sessions_helper_spec.rb | 15 +++++++++++++++ 15 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 app/assets/javascripts/api/v1/api.js create mode 100644 app/assets/stylesheets/api/v1/api.css create mode 100644 app/controllers/api/v1/sessions_controller.rb create mode 100644 app/helpers/api/v1/sessions_helper.rb create mode 100644 spec/controllers/api/v1/sessions_controller_spec.rb create mode 100644 spec/helpers/api/v1/sessions_helper_spec.rb diff --git a/.gitignore b/.gitignore index 36c7e50..226838f 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,6 @@ /public/assets/** -/export.md \ No newline at end of file +/export.md + +/libr_development \ No newline at end of file diff --git a/Gemfile b/Gemfile index bb4af23..f282253 100644 --- a/Gemfile +++ b/Gemfile @@ -16,6 +16,7 @@ gem 'will_paginate' gem 'nokogiri' gem 'unicorn' gem 'devise' +gem 'sqlite3' gem 'activesupport','4.0.0' diff --git a/Gemfile.lock b/Gemfile.lock index d8c96af..9ee648a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -170,6 +170,7 @@ GEM actionpack (>= 3.0) activesupport (>= 3.0) sprockets (~> 2.8) + sqlite3 (1.3.8) tddium (1.16.2) highline json @@ -229,6 +230,7 @@ DEPENDENCIES selenium-webdriver simplecov slim + sqlite3 tddium thin unicorn diff --git a/README.md b/README.md index c34968d..2389ea1 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Build Status * 2013.4.16 开始添加微信公共账号feature * 2013.4.17 通过添加go_serv 微信公共账号,输入1 即可获取书籍信息。please give a try. * 2014.1.16 升级到Rails4,并为Libr 2做准备 - * 开始Libr2, 引入Devise,重新加入用户注册等功能,原有功能基本以不能再使用了。 + * 2014.1.17 开始Libr2, 引入Devise,重新加入用户注册等功能,原有功能基本以不能再使用了。 #### JSON API使用指南 diff --git a/app/assets/javascripts/api/v1/api.js b/app/assets/javascripts/api/v1/api.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/api/v1/api.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/api/v1/api.css b/app/assets/stylesheets/api/v1/api.css new file mode 100644 index 0000000..afad32d --- /dev/null +++ b/app/assets/stylesheets/api/v1/api.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/app/controllers/api/v1/sessions_controller.rb b/app/controllers/api/v1/sessions_controller.rb new file mode 100644 index 0000000..93b804f --- /dev/null +++ b/app/controllers/api/v1/sessions_controller.rb @@ -0,0 +1,17 @@ +class Api::V1::SessionsController < Devise::SessionsController + + def create + warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure") + render :json => {:success => true} + end + + + def destroy + sign_out(resource_name) + end + + def failure + render :json => {:success => false, :errors => ["Login Failed"]} + end + +end diff --git a/app/controllers/book_controller.rb b/app/controllers/book_controller.rb index 4b792c2..d0dca02 100644 --- a/app/controllers/book_controller.rb +++ b/app/controllers/book_controller.rb @@ -36,7 +36,7 @@ class BookController < ApplicationController def add_to_lib unless @current_user - redirect_to '/login', notice: 'You need login to do the action.' + redirect_to new_user_session_path, notice: 'You need login to do the action.' return end @current_user.create_book_instance params[:isbn], params[:is_public] diff --git a/app/helpers/api/v1/sessions_helper.rb b/app/helpers/api/v1/sessions_helper.rb new file mode 100644 index 0000000..50be6d7 --- /dev/null +++ b/app/helpers/api/v1/sessions_helper.rb @@ -0,0 +1,2 @@ +module Api::V1::SessionsHelper +end diff --git a/config/database.yml b/config/database.yml index 60a13ea..40040a8 100644 --- a/config/database.yml +++ b/config/database.yml @@ -13,7 +13,7 @@ # gem 'pg' # development: - adapter: postgresql + adapter: sqlite3 encoding: unicode database: libr_development pool: 5 @@ -40,7 +40,7 @@ development: # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: - adapter: postgresql + adapter: sqlite3 encoding: unicode database: libr_test pool: 5 diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index c738df7..ec512e6 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -222,6 +222,8 @@ Devise.setup do |config| # The "*/*" below is required to match Internet Explorer requests. # config.navigational_formats = ['*/*', :html] + config.navigational_formats = ["*/*", :html, :json] + # The default HTTP method used to sign out a resource. Default is :delete. config.sign_out_via = :delete diff --git a/config/routes.rb b/config/routes.rb index dce54f1..5773ce7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -39,10 +39,20 @@ Libr::Application.routes.draw do post 'api/books/return' => 'api#return_book' post 'api/books/borrow' => 'api#borrow_book' - devise_for :users #for weixin get 'wx/query' => 'wei_xin#verify' post 'wx/query' => 'wei_xin#query' + devise_for :users + + namespace :api do + namespace :v1 do + devise_for :users + devise_scope :user do + resources :sessions, :only => [:create, :destroy, :failure] + end + end + end + end diff --git a/libr_development b/libr_development index cb27fbafb995cbc06cacb7786dc3bb53f5437d04..d2b1a791083de040a40cac25ba442043f8938fda 100644 GIT binary patch delta 1066 zcmaKq&r1|x9LC>qMpAJ1HH|`w5=5kfv+u8&_noCBhPrzU3fa`cEY=lvy9BX2SdfLO zNkoTmiI6Uq%P9Ux5d;t3`xAV3woA9V1--7jTkeSPGQ7;g^L{@(&o^Jt^96nOqK4)! z-Oy0+?x$Fr5!nOqNi>MwitPEgeKnHvpwR;OVBZzjl^g59;*6Y}DZebrXFCV$`BGu+ zK{h)PkHyAPqtv{Yemp!hZjPi=vDC!aNGyW|=S*1U!--Lc3vN-tu_J62Ecid(EXZP! zAk6v^?#H5=pg19M!c2>E;?Sy+J3;aydJDkGL!|QZM|BRi3&G+yxs@-ym^+n+SPm0J z&8Pt}#B8K~%B_N2o{^td8h~&vZc)>*C}F%&`lop9O)a2zu#dWK$$NFMxf!f}DSur$ z6@>~LFEbC6me>)g#zmw0Gch}@vZY7x72u)_TNwIB{T1vH$aJ;T~x>jN8 zQXx%GsiNe@ikyCZDicPwa2!%gN2p{5EC+Kgm)-{-c0w3bVK|86a5Qx#0st`JK7hYr z8ScaH{#oS1-gDY;I{m0s*Dq8q8Kc9{uU4+fvGGiHXnf>;#+WY;TsC?D>`|UB*x}vQ zIzc~hUG<{ZtUlVjophpVZ75YomBcWcf0|5KoyS3IGG#UkE5xLfF)I9@$@AT925bg! z7f!;Puvv}$70o4#GoV$2u4`nEo2$zz2)7!Z{a;DwRlDA3$4C2PaT;#1vU7(@BG**) qH--I&U8}tzxB={F3o6PrP1#!&eb}ukVyfB`+;v5j=j*e7uKxuI{wU@E delta 160 zcmZozz}&Ead4e>f*hCp;MzM_vOY|4BBnWUY@xNf;|Hl7fvtYs<{>d-=lTaMn-y;76z6ElWXE_O^gf-O>_+m ebqy^P5VGc$rlv-?Wj4Qy7gGSaVKd8wKlT98dnfn+ diff --git a/spec/controllers/api/v1/sessions_controller_spec.rb b/spec/controllers/api/v1/sessions_controller_spec.rb new file mode 100644 index 0000000..a65001c --- /dev/null +++ b/spec/controllers/api/v1/sessions_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Api::V1::ApiController do + +end diff --git a/spec/helpers/api/v1/sessions_helper_spec.rb b/spec/helpers/api/v1/sessions_helper_spec.rb new file mode 100644 index 0000000..1f3e69e --- /dev/null +++ b/spec/helpers/api/v1/sessions_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the Api::V1::ApiHelper. For example: +# +# describe Api::V1::ApiHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe Api::V1::SessionsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end