devise
DESCRIPTION
A short introduction to user authentication in Rails using DeviseTRANSCRIPT
![Page 1: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/1.jpg)
Devise
Taking care of your users
Andreas Bjärlestam 2011-02-22
![Page 2: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/2.jpg)
Andreas Bjärlestam
mailto:[email protected]://twitter.com/bjarlestam
Andreas Bjärlestam
mailto:[email protected]://twitter.com/bjarlestam
![Page 3: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/3.jpg)
So you are building a site with rails?
![Page 4: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/4.jpg)
Hopefully you will have some
users lining up...
![Page 5: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/5.jpg)
They will probably f#&k things up
USER USER
![Page 6: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/6.jpg)
This could require quite some coding
Signup page Login page
Account confirmation mails
Password reset mails
Password reset page
Checks and redirects
![Page 7: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/7.jpg)
You?
![Page 8: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/8.jpg)
What to do?
![Page 9: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/9.jpg)
DEVISE can do it!
![Page 10: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/10.jpg)
DEVISE
• User authentication
• User signup
• Account confirmaiton
• Password recovery
• Remembering via cookie
• Session expiration
• Account locking
http://github.com/plataformatec/devise
![Page 11: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/11.jpg)
Lets code…
![Page 12: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/12.jpg)
Create a rails project
$>rails new devise_test
$>cd devise_test
$>gem install devise
$>echo "gem 'devise'" >> Gemfile
$>rails generate devise:install
$>cat config/routes.rb | sed s/'# root :to'/'root :to'/ >> config/routes.rb
![Page 13: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/13.jpg)
Create a User model
$>rails generate devise User
$>rake db:migrate
![Page 14: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/14.jpg)
Test it
$>rails server
$>open http://localhost:3000/users/sign_up
$>open http://localhost:3000/users/sign_in
$>open http://localhost:3000/users/sign_out
![Page 15: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/15.jpg)
![Page 16: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/16.jpg)
What happened?
![Page 17: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/17.jpg)
configuration
Configured routes
config/routes.rbdevise_for :users
Configuration file and localization
config/initializers/devise.rb
config/initializers/locales/devise.en.yml
![Page 18: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/18.jpg)
User model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
![Page 19: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/19.jpg)
helpers available
before_filter :authenticate_user!
user_signed_in?
current_user
![Page 20: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/20.jpg)
customize views
$>rails generate devise:views create app/views/devise
create app/views/devise/confirmations/new.html.erb
create app/views/devise/mailer/confirmation_instructions.html.erb
create app/views/devise/mailer/reset_password_instructions.html.erb
create app/views/devise/mailer/unlock_instructions.html.erb
create app/views/devise/passwords/edit.html.erb
create app/views/devise/passwords/new.html.erb
create app/views/devise/registrations/edit.html.erb
create app/views/devise/registrations/new.html.erb
create app/views/devise/sessions/new.html.erb
create app/views/devise/shared/_links.erb
create app/views/devise/unlocks/new.html.erb
![Page 21: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/21.jpg)
![Page 22: Devise](https://reader035.vdocument.in/reader035/viewer/2022070316/55593258d8b42a543d8b498e/html5/thumbnails/22.jpg)
Attributions
• http://www.flickr.com/photos/weelakeo/3815108314/
• http://www.flickr.com/photos/nikio/3899114449/
• http://www.flickr.com/photos/zigazou76/3636704536/
• http://www.flickr.com/photos/myklroventine-events/3832058915/
• http://www.flickr.com/photos/cokaigne/1401246909/
• http://www.flickr.com/photos/alpha/5434014952
• http://www.flickr.com/photos/alpha/5433405433