Sha256: 074c81c5cebe84bf4a274f37c46eef876c7e49e53248c3974f6d6991d250a6f3

Contents?: true

Size: 1.87 KB

Versions: 8

Compression:

Stored size: 1.87 KB

Contents

require 'entrance'
require 'erb'

=begin

Simple login/signup support for sinatra. This extension
expects a login.erb and a signup.erb (unless disabled) to
be present in a views/public directory.

Once a user logs in, he or she will be redirected to /.

require 'sinatra/base'
require 'entrance/sinatra'

class Hello < Sinatra::Base
  register Entrance::Sinatra

  before do
    login_required :except => ['/login', '/signup'] # or just /login if you don't want signups
  end
end

=end

module Entrance

  module Sinatra

    def self.registered(app)

      app.send(:include, ::Entrance::Controller)

      app.get '/login' do
        if logged_in?
          redirect(to('/'))
        else
          erb :'public/login'
        end
      end

      app.post '/login' do
        remember = ['on', 'true', '1'].include?(params[:remember])
        if params[:username].blank? or params[:password].blank?
          redirect_with('/login', :error, "Both fields are required.")
        elsif user = authenticate_and_login(params[:username], params[:password], remember)
          flash[:success] = 'Welcome back!'
          redirect_to_stored_or(to('/'))
        else
          redirect_with('/login', :error, "Couldn't log you in. Please try again.")
        end
      end

      app.get '/logout' do
        logout!
        redirect_with('/login', :notice, 'Logged out! See you soon.')
      end

      app.get '/signup' do
        return redirect(to('/')) if logged_in?
        @user = ::Entrance.model.new
        erb :'public/signup'
      end

      app.post '/signup' do
        @user = ::Entrance.model.new(params[:user])
        if @user.valid? && @user.save
          redirect_with('/login', :success, "Account created! Please sign in to continue.")
        else
          flash[:error] = "Couldn't sign you up. Please try again."
          erb :'public/signup'
        end
      end

    end

  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
entrance-0.6.3 lib/entrance/addons/sinatra.rb
entrance-0.6.2 lib/entrance/addons/sinatra.rb
entrance-0.6.1 lib/entrance/addons/sinatra.rb
entrance-0.6.0 lib/entrance/addons/sinatra.rb
entrance-0.5.3 lib/entrance/addons/sinatra.rb
entrance-0.5.2 lib/entrance/addons/sinatra.rb
entrance-0.5.1 lib/entrance/addons/sinatra.rb
entrance-0.5.0 lib/entrance/addons/sinatra.rb