Created
August 16, 2012 15:38
-
-
Save pirj/3371151 to your computer and use it in GitHub Desktop.
Sinatra + Omniauth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './site.rb' | |
run Site |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'bundler' | |
require 'logger' | |
Bundler.require | |
Dir['*.rb', 'models/*.rb'].each { |file| require File.join Dir.pwd, file } | |
version = ">= 0" | |
gem 'pry', version | |
load Gem.bin_path('pry', 'pry', version) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
class Site < Sinatra::Base | |
get '/sessions/new' do | |
slim :'sessions/new' | |
end | |
get '/auth/:provider/callback' do | |
identity = Identity.authenticate({:email => params[:auth_key]}, params[:password]) | |
return redirect 'sessions/new' unless identity | |
session[:user_id] = identity.id | |
flash[:info] = "Добро пожаловать!" | |
redirect '/company' | |
end | |
get '/sessions/logout' do | |
session[:user_id] = nil | |
redirect '/' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
class Site < Sinatra::Base | |
get '/company/register' do | |
slim :'company/register' | |
end | |
post '/company/register' do | |
company = Company.new name: params[:company] | |
identity = Identity.create email: params[:auth_key], password: params[:password], password_confirmation: params[:password], :role => 'customer' | |
Employee.create name: params[:name], company: company, identity: identity | |
session[:user_id] = identity.id | |
flash[:info] = "Добро пожаловать!" | |
redirect '/company' | |
end | |
... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'thin' | |
gem 'eventmachine', '1.0.0.rc.4' | |
gem 'sinatra' | |
gem 'sinatra-contrib', :require => 'sinatra/contrib' | |
gem 'sinatra-flash', :require => 'sinatra/flash' | |
gem 'rack-protection' | |
gem 'slim' | |
gem 'pg' | |
gem 'datamapper' | |
%w(core postgres-adapter validations timestamps migrations constraints aggregates types pager is-tree).each do |g| | |
gem 'dm-' + g | |
end | |
gem 'bcrypt-ruby', :require => "bcrypt" | |
group :development do | |
gem 'pry' | |
gem 'dm-sqlite-adapter' | |
end | |
gem 'omniauth-identity', :git => 'git://github.com/intridea/omniauth-identity.git' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'bundler' | |
require 'logger' | |
Bundler.require | |
require 'sinatra/contrib' | |
require 'sinatra/streaming' | |
require 'sinatra/reloader' | |
require 'sinatra/namespace' | |
require 'sinatra/content_for' | |
require 'sinatra/reloader' if development? | |
Dir['*.rb', 'models/*.rb', 'controllers/*.rb'].each { |file| require File.join Dir.pwd, file } | |
class Site < Sinatra::Base | |
register Sinatra::Contrib | |
register Sinatra::Namespace | |
enable :sessions | |
register Sinatra::Flash | |
helpers Sinatra::ContentFor | |
helpers Sinatra::Streaming | |
use Rack::Session::Cookie, :secret => 'fssdfsdfsfohwoiwehfowoefgqfh.zxcn.nwne;knnqonda.sdnca.nwnn' | |
use Rack::Protection | |
enable :logging | |
use Rack::CommonLogger #, Logger.new(STDOUT) | |
set :root, File.dirname(__FILE__) | |
use OmniAuth::Builder do | |
provider :identity, :fields => [:email] | |
end | |
error OmniAuth::Error do | |
403 | |
end | |
[403, 404, 405, 500].each do |code| | |
error code do | |
slim "errors/#{code}.slim" | |
end | |
end | |
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite://#{Dir.pwd}/development.db") | |
DataMapper.finalize | |
configure :development do | |
register Sinatra::Reloader | |
also_reload './*.rb' | |
also_reload './models/*.rb' | |
also_reload './controllers/*.rb' | |
end | |
not_found do | |
'Не найдено.' | |
end | |
error do | |
'Произошло нечто ужасное: ' + env['sinatra.error'].name | |
end | |
def current_user | |
@current_user ||= Identity.get(session[:user_id]) if session[:user_id] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- content_for :nav do | |
== slim :'sessions/nav' | |
p | |
a href='/company/register' | |
| Ещё не зарегистрированы? | |
form.form-horizontal action="/auth/identity/callback" | |
fieldset | |
div.control-group | |
label.control-label for='auth_key' | |
| Электронная почта | |
div.controls | |
input name='auth_key' type='text' | |
div.control-group | |
label.control-label for='password' | |
| Пароль | |
div.controls | |
input name='password' type='password' | |
div.form-actions | |
input type='submit' value='Войти' class='btn btn-primary' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- content_for :nav do | |
== slim :'sessions/nav' | |
p | |
a href='/sessions/new' | |
| Уже зарегистрированы? | |
form.form-horizontal method='post' action='/company/register' | |
fieldset | |
div.control-group | |
label.control-label for='auth_key' | |
| Электронная почта | |
div.controls | |
input name='auth_key' type='text' | |
div.control-group | |
label.control-label for='password' | |
| Пароль | |
div.controls | |
input name='password' type='password' | |
fieldset | |
div.control-group | |
label.control-label for='company' | |
| Название компании | |
div.controls | |
input name='company' type='text' | |
p.help-block | |
/| Supporting help text | |
div.control-group | |
label.control-label for='name' | |
| Фамилия Имя Отчество | |
div.controls | |
input name='name' type='text' | |
p.help-block | |
/| Supporting help text | |
div.form-actions | |
input type='submit' value='Зарегистрироваться' class='btn btn-primary' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This one sucks deeply because of omniauth/omniauth-identity#39