minnie 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ bin
6
+ vendor/bundle
7
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in minnie.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ #Minnie
2
+
3
+ ###Install
4
+
5
+ Add minnie and bcrypt-ruby to your Gemfile
6
+
7
+ gem 'minnie'
8
+ gem 'bcrypt-ruby'
9
+
10
+ Include minnie's authentication in application_controller.rb and set it to require
11
+ authentication for every controller
12
+
13
+ class ApplicationController
14
+ include Minnie::Authentication
15
+
16
+ before_filter :authenticate_user!
17
+
18
+ ...
19
+
20
+ Create a sessions_controller.rb
21
+
22
+ class SessionsController < ApplicationController
23
+ skip_before_filter :authenticate_user!, :except => [:destroy]
24
+
25
+ def new
26
+ @user = User.new
27
+ end
28
+
29
+ def create
30
+ if @user = authenticate(params[:user][:email], params[:user][:password])
31
+ sign_in_and_redirect(@user)
32
+ else
33
+ flash.now[:error] = I18n.t(:invalid_login, :scope => 'app.sessions')
34
+ render "new"
35
+ end
36
+ end
37
+
38
+ def destroy
39
+ sign_out_and_redirect
40
+ end
41
+ end
42
+
43
+ And a login form at app/views/sessions/new.html.erb (this one uses simple_form)
44
+
45
+ <h2>Sign in</h2>
46
+
47
+ <%= simple_form_for @user, :url => sessions_path do |f| %>
48
+ <%= f.error_notification %>
49
+
50
+ <div class="inputs">
51
+ <%= f.input :email %>
52
+ <%= f.input :password %>
53
+ </div>
54
+ <div class="actions">
55
+ <%= f.button :submit, 'Sign In' %>
56
+ </div>
57
+ <% end %>
58
+
59
+ Generate a user.rb that is set up for has_secure_password
60
+
61
+ bundle exec rails g model user email:string password_digest:string
62
+
63
+ And then update app/models/user.rb to include has_secure_password
64
+
65
+ class User < ActiveRecord::Base
66
+ has_secure_password
67
+ end
68
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ class SessionsController < ApplicationController
2
+ skip_before_filter :authenticate_user!, :except => [:destroy]
3
+
4
+ def new
5
+ @user = User.new
6
+ end
7
+
8
+ def create
9
+ if @user = authenticate(params[:user][:email], params[:user][:password])
10
+ sign_in_and_redirect(@user)
11
+ else
12
+ @user = User.new
13
+ flash.now[:error] = I18n.t(:invalid_login, :scope => 'app.sessions')
14
+ render "new"
15
+ end
16
+ end
17
+
18
+ def destroy
19
+ sign_out_and_redirect
20
+ end
21
+ end
data/lib/minnie.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "minnie/version"
2
+
3
+ module Minnie
4
+ autoload :Authentication, 'minnie/authentication'
5
+ end
@@ -0,0 +1,70 @@
1
+ module Minnie
2
+ module Authentication
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :current_user
7
+
8
+ hide_action :authenticate_user!, :sign_in_and_redirect,
9
+ :sign_out_and_redirect, :current_user,
10
+ :authenticate
11
+ end
12
+
13
+ def authenticate_user!
14
+ redirect unless session[:user_id] && User.first(:conditions => {:id => session[:user_id]})
15
+ end
16
+
17
+ def sign_in_and_redirect(user, options = {})
18
+ session[:user_id] = user.id
19
+ options.reverse_merge!({:notice => I18n.t(:signed_in, :scope => 'app.sessions')})
20
+ redirect_to after_sign_in_path, options
21
+ end
22
+
23
+ def sign_out_and_redirect(options = {})
24
+ reset_session
25
+ options.reverse_merge!({:notice => I18n.t(:signed_out, :scope => 'app.sessions')})
26
+ redirect_to after_sign_out_path, options
27
+ end
28
+
29
+ def current_user
30
+ @current_user ||= User.first(:conditions => {:id => session[:user_id]}) if session[:user_id]
31
+ end
32
+
33
+ def authenticate(email, password)
34
+ user = User.find_by_email(email)
35
+ return user && user.authenticate(password)
36
+ end
37
+
38
+ private
39
+
40
+ def redirect
41
+ store_location!
42
+ store_params!
43
+ redirect_to new_session_path
44
+ end
45
+
46
+ def store_location!
47
+ session[:return_to] = request.fullpath if request.get?
48
+ end
49
+
50
+ def stored_location
51
+ session.delete("return_to")
52
+ end
53
+
54
+ def store_params!
55
+ session[:params] = params
56
+ end
57
+
58
+ def stored_params
59
+ session[:params] || {}
60
+ end
61
+
62
+ def after_sign_in_path
63
+ stored_location || root_path
64
+ end
65
+
66
+ def after_sign_out_path
67
+ root_path
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ module Minnie
2
+ VERSION = "0.0.2"
3
+ end
data/minnie.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "minnie/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "minnie"
7
+ s.version = Minnie::VERSION
8
+ s.authors = ["Michael McClenaghan"]
9
+ s.email = ["mike@sideline.ca"]
10
+ s.homepage = ""
11
+ s.summary = %q{Simplest authentication possible}
12
+ s.description = %q{The simplest that authentication can get while still being useful}
13
+
14
+ s.rubyforge_project = "minnie"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency('rails', '>= 3.0')
22
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minnie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael McClenaghan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-21 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70109536609320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70109536609320
25
+ description: The simplest that authentication can get while still being useful
26
+ email:
27
+ - mike@sideline.ca
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.md
35
+ - Rakefile
36
+ - lib/generators/templates/sessions_controller.rb
37
+ - lib/minnie.rb
38
+ - lib/minnie/authentication.rb
39
+ - lib/minnie/version.rb
40
+ - minnie.gemspec
41
+ homepage: ''
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ segments:
54
+ - 0
55
+ hash: 3873794554763273120
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ segments:
63
+ - 0
64
+ hash: 3873794554763273120
65
+ requirements: []
66
+ rubyforge_project: minnie
67
+ rubygems_version: 1.8.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Simplest authentication possible
71
+ test_files: []