-
Notifications
You must be signed in to change notification settings - Fork 4
User Authentication
https://github.com/plataformatec/devise
Add gem 'devise'
to your gemfile, then bundle install
Run
$ rails g devise:install
// 'g' is short for generate
Ensure you have set a default url, for example: root to: "home:index"
in config/routes.rb
Add default url for each environment. for example:
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
within config/environments/development.rb
Run $ rails g devise:views
to automatically create a devise directly under views
Create your user model by running rails g devise User
Run $ rails db:migrate
(if tables exist, first run $ rails db:drop
followed by rails db:setup
)
Migrate Test Databases
- Add flash messages to application.html.erb (instructions included when you run
$ rails g devise:install
)
Run $ rails g contoller Welcome
Within spec/features/user_can_sign_up_spec.rb
Added
<%= link_to "Sign Up", new_registration_path(User) %><br />
within views/welcome/index.html.erb
Within spec/features/user_can_log_in_spec.rb
Added web_helpers.rb
file to 'spec' folder
Added
def create_user
User.create(first_name: 'John', last_name: 'Doe', email: '[email protected]', password: 'password')
end
within spec/web_helpers.rb
Added
require 'web_helpers'
within spec/spec_helpers.rb
Added
<%= link_to "Log In", new_session_path(User) %><br />
within views/welcome/index.html.erb
Within spec/features/user_can_log_out_spec.rb
Added
<%= link_to('Logout', destroy_user_session_path, method: :delete) %>
within views/welcome/index.html.erb
Run $rails generate migration AddNamesToUsers
Added
add_column :users, :first_name, :string, null: false
add_column :users, :last_name, :string, null: false
add_column :users, :avatar, :string
within db/migrate/20191105146713_add_names_to_user.rb
Added
users = User.create([
{first_name: 'John', last_name: 'Doe', email: '[email protected]', password: 'password'},
{first_name: 'Anne', last_name: 'Other', email: '[email protected]', password: 'password'}
])
within db/migrate/seeds.rb
Run `$rails db:drop`
Run `$rails db:setup`
Run `$rails db:migrate RAILS_ENV=test`
Added
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true, autocomplete: "first name" %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, autofocus: true, autocomplete: "last name" %>
</div>
within app/view/registrations/...
Added
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password) }
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:first_name, :last_name, :password, :current_password) }
end
within app/controllers/application_controller.rb