· 1 min read

Screencast: Authentifizierung in Rails 3.1

Downloads in verschiedenen Formaten:

source code mp4 m4v webm ogv

Resourcen:

bash

[bash] rails g model user email:string password_digest:string rails s -e production [/bash]

secret_controller.rb

[ruby] http_basic_authenticate_with :name => “frodo”, :password => “thering” [/ruby]

models/user.rb

[ruby] class User < ActiveRecord::Base attr_accessible :email, :password, :password_confirmation has_secure_password validates_presence_of :password, :on => :create end [/ruby]

sessions_controller.rb

[ruby] def create user = User.find_by_email(params[:email]) if user && user.authenticate(params[:password]) session[:user_id] = user.id redirect_to root_url, :notice => “Logged in!” else flash.now.alert = “Invalid email or password” render “new” end end

def destroy session[:user_id] = nil redirect_to root_url, :notice => “Logged out!” end [/ruby]

application_controller.rb

[ruby] force_ssl

private

def current_user @current_user ||= User.find(session[:user_id]) if session[:user_id] end helper_method :current_user [/ruby]

Back to Blog