0

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
11
  • Hi Marko! Welcome to Stack Overflow! I don't think that you've provided enough information in the question. For quidelines on how to ask a better question look at this page stackoverflow.com/help/how-to-ask. We also like to see some attempt to answer your question youself, showing some research. Please just do some googling to specify your question further. We also do not include a signature like "Regards" so please edit that out. Commented May 31, 2016 at 22:45
  • Specifically please include what type of database and if your server is run locally or not. Commented May 31, 2016 at 22:47
  • Thank you @thesecretmaster for your guidelines. Commented Jun 1, 2016 at 8:42
  • Do you mean that you want to check if a user has an account on the computer running the ruby code? Commented Jun 1, 2016 at 9:41
  • Yes. I want to perform check/auth against local users in linux or mac (i'm not having any type of database). Commented Jun 1, 2016 at 9:45

1 Answer 1

0

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>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.