Rails working with Devise for authentication and allow login via Facebook account
1. Generate a new app demo_devise
sudo rails new demo_devise
2. Add new gem into Gemfile and install it
gem "devise"
3. Install devise with this command:
rails g devise:install
4. Generate User model
rails g devise User
Check the migration to uncomment fields for required modules
Check the model User to include required modules
confirmable:
if enabled, after user signed up, he will receive an email also
5. Run migrate db
rails db:migrate
6. Generate home controller with index action
rails g controller home index
7. Adding controller myspace with index action and requires authenticated users
rails g controller myspace index
8. Adding .env file, and gem 'dotenv-rails' into development group
Now try to signup by email address, a new account will be created and can login.
TODO: customize email template
9. Now add Bootstrap into the app
Update importmaps.rb to add new npm packages
pin "popper", to: 'popper.js', preload: true
pin "bootstrap", to: 'bootstrap.min.js', preload: true
10. Update js in application.js
import "popper"
import "bootstrap
10. Add bootstrap gem and its dependency
gem "bootstrap"
gem "sassc-rails"
Then add into application.css
@import "bootstrap";
11. Update manifest.js to add new lines
//= link popper.js
//= link bootstrap.min.js
12. Update HTML page for the layout
13. Add Facebook authentication
We need to create an application, add all required information for Facebook to review.
Then implement the action to handle returned data, and create user in our database.