Is it possible to authenticate user against the local user database on linux/mac? I would like to create users locally on linux and then force authentication using sinatra or any other suggested ruby gem (no rails knowledge :()
I don't have any database and my app is so simple it should look like this:
require 'sinatra'
use Rack::Auth::Basic, "Restricted Area" do |username, password|
[username, password] == ['admin', 'admin']
end
get '/' do
"You're welcome"
end
My recommendation is to use a database. If you end up going that route here is how you would do it:
Add to your gemfile gem 'sqlite'
and gem 'sinatra-activerecord'
Run the command bundle exec rake db:create_migration NAME=setup_users_table
. This will create a db
directory containing migrations/<random numbers>_setup_users_table.rb
. In that file, add code inside the change
function. To create a Users table with a username and password field add the following code:
create_table :users do |i|
i.string :username
i.string :password
end
Now run bundle exec rake db:migrate
. If that has succeded then you have a working database. To access it you need to add to your app file the following code:
class User < ActiveRecord::Base
end
Now you're good to go!
To create a user:
User.create(username:<whatever>,password:<whatever>)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.