mirror of
https://github.com/wahyd4/Libr.git
synced 2026-08-09 04:46:07 +10:00
finish douban oauth2
This commit is contained in:
@@ -5,4 +5,10 @@ tw-libr
|
||||
|
||||
Build Status
|
||||
====
|
||||
[](https://travis-ci.org/wahyd4/Libr)
|
||||
[](https://travis-ci.org/wahyd4/Libr)
|
||||
|
||||
#### Export System variable
|
||||
|
||||
export CLIENT_ID='0858f96ac849895e190aec4058dc9c1a'
|
||||
export CLIENT_SECRET='0c363bc78dbacb3f'
|
||||
export REDIRECT_URI='http://localhost:3000/douban_callback'
|
||||
@@ -1,3 +1,24 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
protect_from_forgery
|
||||
before_filter :current_user
|
||||
|
||||
def current_user
|
||||
@current_user = nil
|
||||
|
||||
if session[:name]
|
||||
@current_user = User.find_by_name session[:name]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def sign_in(name,avatar)
|
||||
if session[:name] == name
|
||||
return
|
||||
end
|
||||
unless User.find_by_name name
|
||||
User.create_user(name,avatar)
|
||||
end
|
||||
session[:name] = name
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
require "douban_auth"
|
||||
|
||||
class HomeController < ApplicationController
|
||||
include Douban_Auth
|
||||
before_filter :current_user
|
||||
|
||||
def index
|
||||
@books = Book.order 'id DESC'
|
||||
@@ -8,7 +10,10 @@ class HomeController < ApplicationController
|
||||
|
||||
def douban_callback
|
||||
|
||||
access_token params[:code]
|
||||
token = access_token params[:code]
|
||||
user_info = fetch_user_info token
|
||||
sign_in(user_info['name'],user_info['avatar'])
|
||||
redirect_to '/'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -7,11 +7,14 @@ class UserController < ApplicationController
|
||||
render :layout => false
|
||||
end
|
||||
|
||||
def login_douban
|
||||
|
||||
def auth_douban
|
||||
url = auth_url.to_s
|
||||
|
||||
puts "xxxxxxxxxxxx"+url
|
||||
redirect_to url
|
||||
end
|
||||
|
||||
def logout
|
||||
session[:name] = nil
|
||||
redirect_to '/'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
module HomeHelper
|
||||
|
||||
|
||||
end
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
module UserHelper
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
+8
-1
@@ -1,5 +1,12 @@
|
||||
class User < ActiveRecord::Base
|
||||
attr_accessible :email, :name, :books
|
||||
attr_accessible :email, :name, :books, :avatar
|
||||
|
||||
validates_uniqueness_of :name
|
||||
|
||||
has_many :user_to_books
|
||||
has_many :books, through: :user_to_books
|
||||
|
||||
def self.create_user(name,avatar)
|
||||
User.create name: name, avatar: avatar
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,8 +20,16 @@ html
|
||||
li
|
||||
a href="" 书架
|
||||
ul.nav.pull-right
|
||||
li
|
||||
a href="/login" 登录
|
||||
-if @current_user != nil
|
||||
li
|
||||
a href="#"
|
||||
img.img-circle style="max-width:82%;margin:-5px" src="#{@current_user.avatar}" title="#{@current_user.name}"
|
||||
li
|
||||
a href="/logout" 注销
|
||||
- else
|
||||
li
|
||||
a href="/login" 登录
|
||||
|
||||
|
||||
div.container.main
|
||||
= yield
|
||||
|
||||
+2
-1
@@ -4,7 +4,8 @@ Libr::Application.routes.draw do
|
||||
match 'books/:id' => 'book#view'
|
||||
get '/search/book/:arg' =>'book#search'
|
||||
get '/login' => 'user#login'
|
||||
get '/login/douban' => 'user#login_douban'
|
||||
get '/logout' => 'user#logout'
|
||||
get '/login/douban' => 'user#auth_douban'
|
||||
get '/douban_callback' =>'home#douban_callback'
|
||||
|
||||
# The priority is based upon order of creation:
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddAvatarToUser < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :users, :avatar, :string
|
||||
end
|
||||
end
|
||||
+2
-1
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20130107052141) do
|
||||
ActiveRecord::Schema.define(:version => 20130109051810) do
|
||||
|
||||
create_table "books", :force => true do |t|
|
||||
t.string "name"
|
||||
@@ -34,6 +34,7 @@ ActiveRecord::Schema.define(:version => 20130107052141) do
|
||||
t.string "name"
|
||||
t.datetime "created_at", :null => false
|
||||
t.datetime "updated_at", :null => false
|
||||
t.string "avatar"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
+19
-6
@@ -5,12 +5,12 @@ module Douban_Auth
|
||||
|
||||
|
||||
def auth_url
|
||||
client_id = '0858f96ac849895e190aec4058dc9c1a'
|
||||
redirect_uri = 'http://localhost:3000/douban_callback'
|
||||
response_type = 'code'
|
||||
scope = 'douban_basic_common'
|
||||
scope = 'douban_basic_common,book_basic_r'
|
||||
|
||||
url = 'https://www.douban.com/service/auth2/auth?client_id=' << client_id << '&redirect_uri=' << redirect_uri << '&response_type=' << response_type << '&scope='+ scope
|
||||
url = 'https://www.douban.com/service/auth2/auth?client_id=' + ::ENV['CLIENT_ID'] +
|
||||
'&redirect_uri=' + ::ENV['REDIRECT_URI'] +
|
||||
'&response_type=' + response_type + '&scope=' + scope
|
||||
|
||||
end
|
||||
|
||||
@@ -19,13 +19,26 @@ module Douban_Auth
|
||||
http = Net::HTTP.new('www.douban.com', 443)
|
||||
http.use_ssl = true
|
||||
path ='/service/auth2/token'
|
||||
data ='client_id=0858f96ac849895e190aec4058dc9c1a&client_secret=0c363bc78dbacb3f&redirect_uri=http://localhost:3000/douban_callback&grant_type=authorization_code&code='+code
|
||||
data ='client_id='+ ::ENV['CLIENT_ID']+'&client_secret='+ ::ENV['CLIENT_SECRET']+'&redirect_uri=http://localhost:3000/douban_callback'+'&grant_type=authorization_code&code='+code
|
||||
headers = {
|
||||
'Authorization' => my_code
|
||||
}
|
||||
|
||||
response = http.post(path, data, headers)
|
||||
response = JSON.parse response.body
|
||||
puts '111111111111111111'+ response.to_s
|
||||
|
||||
response['access_token']
|
||||
end
|
||||
|
||||
def fetch_user_info(token)
|
||||
http = Net::HTTP.new('api.douban.com', 443)
|
||||
http.use_ssl = true
|
||||
path ='/v2/user/~me'
|
||||
header = {
|
||||
'Authorization' => 'Bearer '+token
|
||||
}
|
||||
response = http.get(path,header)
|
||||
response = JSON.parse response.body
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user