超級初學者在這裏。當facebook認證時顯示用戶的獨特信息,與Sinatra /紅寶石
這裏就是我想要做的事:
建立一個基本的待辦事項清單app,其中在與Facebook用戶X原木,增加了一些項目,會看到他們,註銷。用戶Y/Z/M/etc應該能夠登錄查看他們的OWN列表,添加他們自己的物品等。 AKA:一個標準的網絡應用程序,您可以登錄到您的帳戶並查看您自己的信息。
我到目前爲止:
能夠建立一個列表,使用Facebook登錄並讓它知道你的名字。 但是,無論我登錄或我的朋友是否使用她的帳戶登錄,列表都保持不變。
我需要做的,不知道怎麼什麼:我
需要每個用戶能夠創建和看到自己的列表,並能夠回來給它,仍然可以看到它/加到它等
我甚至不知道這是如何調用,這是一個數據庫的用戶,每個都有自己的一套數據?列表是否需要設置,以便它們可以作爲一大塊數據存儲? 這是否與此有關:Sessions in Sinatra using Facebook authentication如果是這樣,什麼?
如果任何人都可以給我一些真的很基本的方向在何處何去何從,任何教程或者我應該google搜索,那會是真棒。
這裏是我的代碼主片(警告:這是真的很亂):
require 'sinatra'
require 'data_mapper'
require 'time'
require 'rubygems'
require 'json'
require 'omniauth'
require 'omniauth-facebook'
#TODO require 'omniauth-att'
SCOPE = 'email,read_stream'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/queue.db")
class SinatraApp < Sinatra::Base
configure do
set :sessions, true
set :inline_templates, true
set :protection, :except => :frame_options
end
class Note
include DataMapper::Resource
property :id, Serial
property :content, Text, :required => true
property :complete, Boolean, :required => true, :default => false
property :created_at, DateTime
property :updated_at, DateTime
end
class User
include DataMapper::Resource
property :id, Serial
property :uid, String
property :name, String
property :created_at, DateTime
end
###### no clue what this does ##############
DataMapper.finalize
DataMapper.auto_upgrade!
enable :session
use OmniAuth::Builder do
provider :facebook, '464630283595639','5e4c7ad43bf111c10287c981d51127a3',:scope => SCOPE, :display => "popup"
#provider :att, 'client_id', 'client_secret', :callback_url => (ENV['BASE_DOMAIN']
end
###### root ##############
get '/' do
if current_user
@notes = Note.all :order => :id.desc
@title = 'Movie Queue'
erb :home
else
' <a href="/sign_in">sign in with Facebook</a>'
end
end
###### authentication ##############
["/sign_in/?", "/signup/?"].each do |path|
get path do
redirect '/auth/facebook'
end
end
get '/auth/:name/callback' do
auth = request.env["omniauth.auth"]
user = User.first_or_create({ :uid => auth["uid"]}, {
:uid => auth["uid"],
:name => auth["first_name"],
:created_at => Time.now })
session[:user_id] = user.id
redirect '/'
end
helpers do
def current_user
@current_user ||= User.get(session[:user_id]) if session[:user_id]
end
end
##list making part###
post '/' do
n = Note.new
n.content = params[:content]
n.save
redirect '/'
end
get '/:id/delete' do
n = Note.get params[:id]
if n.destroy
redirect '/', :notice => 'Note deleted successfully.'
else
redirect '/', :error => 'Error deleting note.'
end
end
get '/:id/complete' do
n = Note.get params[:id]
n.complete = n.complete ? 0 : 1 # flip it
n.save
redirect '/'
end
########## logout and error handlers #############
get '/logout' do
session[:user_id] = nil
redirect '/'
end
get '/auth/failure' do
erb "<h1>Authentication Failed:</h1><h3>message:<h3> <pre>#{params}</pre>"
end
get '/auth/:provider/deauthorized' do
erb "#{params[:provider]} has deauthorized this app."
end
get '/protected' do
throw(:halt, [401, "Not authorized\n"]) unless session[:authenticated]
erb "<pre>#{request.env['omniauth.auth'].to_json}</pre><hr>
<a href='/logout'>Logout</a>"
end
end
########## don't know what this is #############
SinatraApp.run! if __FILE__ == $0
順便說一句,我認爲你到目前爲止取得的成績對於自稱是超級初學者的人來說是非常令人印象深刻的(我知道這聽起來光顧,對不起!但是對你有好感) – iain
我認爲她騙了一點點並採用了一些鬼鬼祟祟的代碼複製粘貼在這裏(評論「不知道這是什麼」),但沒有其他更好的:)順便說一句。 Helena'DataMapper.finalize/auto_upgrade'你可以在[docs](http://datamapper.org/getting-started.html)中找到這些解釋。只需在該頁面上搜索這些條款即可。他們在該頁面上解釋。 – Casper
我一定會看看。一直在做一些衝,但應退後一步,看看一些東西:)謝謝你們的幫助。 –