Giter Site home page Giter Site logo

uilgenstein / sinatra-simple-auth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vast/sinatra-simple-auth

4.0 1.0 1.0 91 KB

Super simple auth extension for Sinatra

Home Page: http://github.com/vast/sinatra-simple-auth

License: MIT License

Ruby 100.00%

sinatra-simple-auth's Introduction

##Sinatra SimpleAuth Extension

Extends Sinatra with extension methods and routes for dealing with a simple authorization method. Both Sinatra application styles are supported: "Classic" and "Classy" (modular) style.

##Installation

Since this is a fork of the original gem sources it is not released as a gem. You can build the gem yourself:

gem build sinatra-simple-auth.gemspec
gem install sinatra-simple-auth-0.1.1.gem

or you can install it from a local path or even from github through Bundler by adding one of these two lines to your Gemfile:

gem 'sinatra-simple-auth', :path => /path/to/sources
or
gem 'sinatra-simple-auth'. :git => 'git://github.com/uilgenstein/sinatra-simple-auth.git'

##Usage for "Classic" style applications

require 'rubygems'
require 'sinatra'
require 'sinatra/simple_auth'

enable :sessions
set    :home, '/secure/' #where user should be redirected after successful authorization

def authorize(login, password)
  # return value will be saved in session[:user_id]
  login if login == 'bob' && password == 'secret
end

get '/login/?' do
  erb :login #page with logon form
end

get '/secure/?' do
  login_required #protected route, requires auth
  erb :secure
end

get '/' do
  if authorized? #helper method
    "Hello, your user id is: #{session[:user_id]}"
  else
    "Not authorized"
  end
end

##Usage for "Classy" (modular) style applications

In your config.ru you can mount your proteceted app under a url prefix (e.g. /admin):

require 'rubygems'
require 'sinatra/base'
require 'admin_application
require 'public_application

map '/admin' do
  run AdminApplication
end

map '/' do
  run PublicApplication
end

Your protected application could then look something like this:

require 'sinatra/simple_auth'

class AdminApplication < Sinatra::Base
  register Sinatra::SimpleAuth

  enable :sessions
  set    :home, '/' # relative to url prefix from config.ru. 
                    # this is where user should be redirected after successful authorization

  def authorize(login, password)
    # return value will be saved in session[:user_id]
    login if login == 'bob' && password == 'secret
  end

  before do
    login_required unless request.path_info =~ /^\/login\/?$/
  end

  get '/login/?' do
    erb :login #page with logon form
  end

  get '/' do
    if authorized? #helper method
      "Hello, your user id is: #{session[:user_id]}"
    else
      "Not authorized"
    end
  end
end

sinatra-simple-auth's People

Contributors

uilgenstein avatar vast avatar

Stargazers

 avatar  avatar Simon Swain avatar Kerry Benton avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.