|
| 1 | +# Authie |
| 2 | + |
| 3 | +This is a Rails library which provides applications with a database-backed user |
| 4 | +sessions. This ensures that user sessions can be invalidated from the server and |
| 5 | +users activity can be easily tracked. |
| 6 | + |
| 7 | +The "traditional" way of simply setting a user ID in your session is insecure |
| 8 | +and unwise. If you simply do something like the example below, it means that anyone |
| 9 | +with access to the session cookie can login as the user whenever and wherever they wish. |
| 10 | + |
| 11 | +```ruby |
| 12 | +if user = User.authenticate(params[:username], params[:password]) |
| 13 | + # Don't do this... |
| 14 | + session[:user_id] = user.id |
| 15 | + redirect_to root_path, :notice => "Logged in successfully!" |
| 16 | +end |
| 17 | +``` |
| 18 | + |
| 19 | +The design goals behind Authie are: |
| 20 | + |
| 21 | +* Any session can be invalidated instantly from the server without needing to make |
| 22 | + changes to remote cookies. |
| 23 | +* We can see who is logged in to our application at any point in time. |
| 24 | +* Sessions should automatically expire after a certain period of inactivity. |
| 25 | +* Sessions can be either permanent or temporary. |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +As usual, just pop this in your Gemfile: |
| 30 | + |
| 31 | +```ruby |
| 32 | +gem 'authie', '~> 1.0.0' |
| 33 | +``` |
| 34 | + |
| 35 | +You will then need to run your `db:migrate` task to add the Authie sessions table |
| 36 | +to your local database. |
| 37 | + |
| 38 | +``` |
| 39 | +rake db:migrate |
| 40 | +``` |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +Authie is just a session manager and doesn't provide any functionality for your authentication or User models. Your `User` model should implement any methods needed to authenticate a username & password. |
| 45 | + |
| 46 | +### Creating a new session |
| 47 | + |
| 48 | +When a user has been authenticated, you can simply set `current_user` to the user |
| 49 | +you wish to login. You may have a method like this in a controller. |
| 50 | + |
| 51 | +```ruby |
| 52 | +class AuthenticationController < ApplicationController |
| 53 | + |
| 54 | + skip_before_filter: login_required |
| 55 | + |
| 56 | + def login |
| 57 | + if request.post? |
| 58 | + if user = User.authenticate(params[:username], params[:password]) |
| 59 | + self.current_user = user |
| 60 | + redirect_to root_path |
| 61 | + else |
| 62 | + flash.now[:alert] = "Username/password was invalid" |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | +end |
| 68 | +``` |
| 69 | + |
| 70 | +### Checking whether user's are logged in |
| 71 | + |
| 72 | +On any subsequent request, you should make sure that your user is logged in. |
| 73 | +You may wish to implement a `login_required` controller method which is called |
| 74 | +before every action in your application. |
| 75 | + |
| 76 | +```ruby |
| 77 | +class ApplicationController < ActionController::Base |
| 78 | + |
| 79 | + before_filter :login_required |
| 80 | + |
| 81 | + private |
| 82 | + |
| 83 | + def login_required |
| 84 | + unless logged_in? |
| 85 | + redirect_to login_path, :alert => "You must login to view this resource" |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | +end |
| 90 | +``` |
| 91 | + |
| 92 | +### Accessing the current user (and session) |
| 93 | + |
| 94 | +There are a few controller methods which you can call which will return information about the current session: |
| 95 | + |
| 96 | +* `current_user` - returns the currently logged in user model |
| 97 | +* `auth_session` - returns the current auth session |
| 98 | +* `logged_in?` - returns a true if there's a session or false if no user is logged in |
| 99 | + |
| 100 | +### Logging out |
| 101 | + |
| 102 | +In order to invalidate a session you can simply invalidate it. |
| 103 | + |
| 104 | +``` |
| 105 | +def logout |
| 106 | + auth_session.invalidate! |
| 107 | + redirect_to login_path, :notice => "Logged out successfully." |
| 108 | +end |
| 109 | +``` |
| 110 | + |
| 111 | +### Persisting sessions |
| 112 | + |
| 113 | +In some cases, you may wish users to have a permanent sessions. In this case, you should ask users after they have logged in if they wish to "persist" their session across browser restarts. If they do wish to do this, just do something like this: |
| 114 | + |
| 115 | +```ruby |
| 116 | +def persist_session |
| 117 | + auth_session.persist! |
| 118 | + redirect_to root_path, :notice => "You will now be remembered!" |
| 119 | +end |
| 120 | +``` |
| 121 | + |
| 122 | +### Accessing all user sessions |
| 123 | + |
| 124 | +If you want to provide users with a list of their sessions, you can access all active sessions for a user. The best way to do this will be to add a `has_many` association to your User model. |
| 125 | + |
| 126 | +```ruby |
| 127 | +class User < ActiveRecord::Base |
| 128 | + has_many :sessions, :class_name => 'Authie::Session', :foriegn_key => 'user_id', :dependent => :destroy |
| 129 | +end |
| 130 | +``` |
0 commit comments