Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6114573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:59:25+00:00 2026-05-23T14:59:25+00:00

Same app, different problem. I’m working on an app using the Dan Benjamin Meet

  • 0

Same app, different problem. I’m working on an app using the Dan Benjamin “Meet Sinatra” screencast as a reference. I’m trying to include a custom authentication module, which is housed in a lib folder (lib/authentication.rb). I am requiring that line at the top of my code, but when I try to load the page, it claims there is no such file to load.

Any thoughts?

Here’s the top of my main Sinatra file:

require 'sinatra'
require 'rubygems'
require 'datamapper'
require 'dm-core'
require 'lib/authorization'

DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/entries.db")

class Entry
include DataMapper::Resource

property :id,           Serial
property :first_name,   String
property :last_name,    String
property :email,        String
property :created_at,   DateTime    

end

# create, upgrade, or migrate tables automatically
DataMapper.auto_upgrade!

helpers do
include Sinatra::Authorization
end

And the actual Module:

module Sinatra
  module Authorization

  def auth
    @auth ||= Rack::Auth::Basic::Request.new(request.env)
  end

  def unauthorized!(realm="Short URL Generator")
    headers 'WWW-Authenticate' => %(Basic realm="#{realm}")
    throw :halt, [ 401, 'Authorization Required' ]
  end

  def bad_request!
    throw :halt, [ 400, 'Bad Request' ]
  end

  def authorized?
    request.env['REMOTE_USER']
  end

  def authorize(username, password)
    if (username=='topfunky' && password=='peepcode') then
      true
  else
    false
  end
end

def require_admin
  return if authorized?
  unauthorized! unless auth.provided?
  bad_request! unless auth.basic?
  unauthorized! unless authorize(*auth.credentials)
  request.env['REMOTE_USER'] = auth.username
end

  def admin?
    authorized?
  end

  end
end

Then, on any of the handlers I want to protect, I put “require_admin.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:59:26+00:00Added an answer on May 23, 2026 at 2:59 pm

    Assuming you’re using Ruby 1.9, the default $LOAD_PATH no longer includes the current directory. So while statements like require 'sinatra' work just fine (because those gems are in $LOAD_PATH), Ruby doesn’t know that your lib/authorization file is located relative to your main Sinatra file.

    You can add the Sinatra file’s directory to the load path, and then your require statements should work fine:

    $LOAD_PATH.unshift(File.dirname(__FILE__))
    require 'sinatra'
    require 'rubygems' # Not actually needed on Ruby 1.9
    require 'datamapper'
    require 'dm-core'
    require 'lib/authorization'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario : You need to expose the same app on different screens - let's
I'm trying to do some image edit syncing between two of the same app
I am working on a project to call a webservice from different domain using
Im in the sticky problem of in need to localize the app for different
I have a weird problem, I have 2 testing devices running the same app
I'm using the Ad-Hoc distribution to deploy my app on different devices. I already
I'm working with 2 different DB's in my app, a big(oracle) one for the
In my iPhone app, I am using a single tableview to display different sets
Hi i'm working on an android app and this is my problem I have
I am trying to make an app with two different tab bar controllers, one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.