|
8 | 8 | require 'ginatra/repo'
|
9 | 9 | require 'ginatra/repo_list'
|
10 | 10 | require 'ginatra/repo_stats'
|
| 11 | +require 'roda' |
| 12 | +require 'sequel/core' |
| 13 | +require 'bcrypt' |
11 | 14 |
|
12 | 15 | module Ginatra
|
13 | 16 | # The main application class.
|
@@ -36,6 +39,50 @@ class App < Sinatra::Base
|
36 | 39 | Dir["#{settings.root}/ginatra/*.rb"].each { |file| also_reload file }
|
37 | 40 | end
|
38 | 41 |
|
| 42 | + # Add a cookie-based session handler, to store the login id of the user |
| 43 | + use Rack::Session::Cookie, :secret=>File.file?('ginatra.secret') ? File.read('ginatra.secret') : (ENV['GINATRA_SECRET'] || SecureRandom.hex(20)) |
| 44 | + |
| 45 | + class RodauthApp < Roda |
| 46 | + # Include these modules, as Ginatra's layout calls methods in them |
| 47 | + include Ginatra::Helpers |
| 48 | + include Sinatra::Partials |
| 49 | + |
| 50 | + # Setup the database unless it already exists |
| 51 | + db = Sequel.sqlite('users.sqlite3') |
| 52 | + unless db.table_exists?(:accounts) |
| 53 | + db.create_table(:accounts) do |
| 54 | + primary_key :id |
| 55 | + String :email, :unique=>true, :null=>false |
| 56 | + String :password_hash, :null=>false |
| 57 | + end |
| 58 | + |
| 59 | + # Add a demo account for testing, since we aren't allowing users to create their own |
| 60 | + # accounts. |
| 61 | + db[:accounts].insert(:email=>'demo', :password_hash=>BCrypt::Password.create('demo')) |
| 62 | + end |
| 63 | + |
| 64 | + plugin :middleware |
| 65 | + plugin :rodauth do |
| 66 | + enable :login |
| 67 | + |
| 68 | + # Since we are using SQLite as the database and not PostgreSQL, just store the |
| 69 | + # password hash in a column in the main table |
| 70 | + account_password_hash_column :password_hash |
| 71 | + end |
| 72 | + |
| 73 | + # Alias render to erb, since the layout calls the erb method to render |
| 74 | + alias erb render |
| 75 | + |
| 76 | + route do |r| |
| 77 | + r.rodauth |
| 78 | + |
| 79 | + # Force all users to login before accessing Ginatra |
| 80 | + rodauth.require_authentication |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + use RodauthApp |
| 85 | + |
39 | 86 | def cache(obj)
|
40 | 87 | etag obj if settings.production?
|
41 | 88 | end
|
|
0 commit comments