rails auth -d [ ]
cd auth
rm public/index.html
[ config/database.yml]
rake db:create
script/plugin install git://github.com/technoweenie/restful-authentication.git
script/generate authenticated user sessions
Copy Source | Copy HTML<br/> ActionController::Routing ::Routes.draw do |map|<br/> map.logout '/logout' , :controller => 'sessions' , :action => 'destroy' <br/> map.login '/login' , :controller => 'sessions' , :action => 'new' <br/> map.register '/register' , :controller => 'users' , :action => 'create' <br/> map.signup '/signup' , :controller => 'users' , :action => 'new' <br/> map.resources :users<br/> map.resource :session<br/> end <br/>
sessions/new [GET]
sessions [POST]
sessions [DELETE] logout
users/new [GET]
users [POST]
Copy Source | Copy HTML<br/> require 'digest/sha1' <br/> <br/> class User < ActiveRecord::Base <br/> include Authentication<br/> include Authentication::ByPassword<br/> include Authentication::ByCookieToken<br/> <br/> validates_presence_of : login <br/> validates_length_of : login , :within => 3 .. 40 <br/> validates_uniqueness_of : login <br/> validates_format_of : login , :with => Authentication.login_regex, :message => Authentication.bad_login_message<br/> <br/> validates_format_of :name, :with => Authentication.name_regex, :message => Authentication.bad_name_message, :allow_nil => true <br/> validates_length_of :name, :maximum => 100 <br/> <br/> validates_presence_of : email <br/> validates_length_of : email , :within => 6 .. 100 <br/> validates_uniqueness_of : email <br/> validates_format_of : email , :with => Authentication.email_regex, :message => Authentication.bad_email_message<br/> <br/> attr_accessible : login , : email , :name, :password, :password_confirmation<br/> <br/> def self .authenticate( login , password)<br/> return nil if login .blank? || password.blank?<br/> u = find_by_login( login .downcase) # need to get the salt <br/> u && u.authenticated?(password) ? u : nil <br/> end <br/> <br/> def login =(value)<br/> write_attribute : login , (value ? value.downcase : nil )<br/> end <br/> <br/> def email =(value)<br/> write_attribute : email , (value ? value.downcase : nil )<br/> end <br/> end <br/>
Copy Source | Copy HTML<br/> require 'digest/sha1' <br/> <br/> class User < ActiveRecord::Base <br/> include Authentication<br/> include Authentication::ByPassword<br/> include Authentication::ByCookieToken<br/> <br/> validates_format_of :name, :with => Authentication.name_regex, :message => Authentication.bad_name_message, :allow_nil => true <br/> validates_length_of :name, :maximum => 100 <br/> <br/> validates_presence_of : email <br/> validates_length_of : email , :within => 6 .. 100 <br/> validates_uniqueness_of : email <br/> validates_format_of : email , :with => Authentication.email_regex, :message => Authentication.bad_email_message<br/> <br/> attr_accessible : email , :name, :password, :password_confirmation<br/> <br/> def self .authenticate(login, password)<br/> return nil if login.blank? || password.blank?<br/> u = find_by_email(login.downcase) # need to get the salt <br/> u && u.authenticated?(password) ? u : nil <br/> end <br/> <br/> def email =(value)<br/> write_attribute : email , (value ? value.downcase : nil )<br/> end <br/> end <br/>
< h1 > Sign up as a new user </ h1 > <br> <% @user.password = @user.password_confirmation = nil %> <br><br> <% = error_messages_for :user %> <br> <% form_for :user, :url => users_path do |f| - %> <br><br> < p > <% = label_tag 'email' %> < br /> <br> <% = f.text_field :email %> </ p > <br><br> < p > <% = label_tag 'password' %> < br /> <br> <% = f.password_field :password %> </ p > <br><br> < p > <% = label_tag 'password_confirmation' , 'Confirm Password' %> < br /> <br> <% = f.password_field :password_confirmation %> </ p > <br><br> < p > <% = label_tag 'name' %> < br /> <br> <% = f.text_field :name %> </ p > <br><br> < p > <% = submit_tag 'Sign up' %> </ p > <br> <% end - %> <br> <br> * This source code was highlighted with Source Code Highlighter .
script/generate controller home index
map.root :controller => 'home'
< html > <br> < head > <br> < meta http-equiv ="Content-type" content ="text/html; charset=utf-8" /> <br> < title > auth demo </ title > <br> </ head > <br> < body > <br> <% = render : partial => 'users/user_bar' %> <br> <% = yield %> <br> </ body > <br> </ html > <br><br> * This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/53254/
All Articles