User authentication engine requires Rails 3.2.1 or above.
Configuring the application:
-
Add the following to the
Gemfile
gem 'user_authentication' -
Add the following to
config/application.rb:
config.railties_order = [UserAuthentication::Engine, :main_app, :all]
as the first line after
class Application < Rails::Application -
Add the following to
config/routes.rb:
UserAuthentication::Engine.routes
as the first line after
YourApplication::Application.routes.draw do
Creating the Account model:
If the model does not exist, which is the most common case, run the following:
bundle exec rake user_authentication:install:migrationsbundle exec rake db:migrate
Else in the unlikely case that your application already has the model:
- Ensure that the account model has an
email(VARCHAR(255)) and apassword_digest(VARCHAR(255)) field. - Add the following line at the top of app/models/account.rb:
require File.join UserAuthentication::Engine.config.root, 'app/models/account.rb'
Creating a login form:
There are three ways of creating a login form:
- Use the ready made login page provided by the railtie at
/login. - Use the ready made partial
shared/loginprovided by the railtie, in any page. - Create a custom login form with an
emailfield, apasswordfield and the action set tologin_path. On a successful login, site will be redirected to thenextfield, if any.
On a successful login:
-
current_accountwill be set to the logged in account. - If an
on_loginaction onAccountsControlleris defined, that will be invoked. - If it is not defined:
- If a
redirectfield is set in the form, site will be redirected to its value. - If a
redirectfield is not set, site will be redirected back to the referrer.
- If a
On a failed login:
-
current_accountwill be nil. - Site will be redirected back to the referrer.
Notes:
- You can render a logged in experience based on whether
current_accountwas set. - If you want to set the redirect URL, you can render the partial directly as
render "shared/login", redirect: <custom_url>or define anon_loginaction in yourAccountsControllerthat performs the redirect.
Creating a signup form:
There are three ways of creating a signup form:
- Use the ready made signup provided by the railtie at
/signup. - Use the ready made partial
shared/signupprovided by the railtie, in any page. - Create a custom form with an
emailfield, apasswordfield an the action set tosignup_path.
On a successful signup:
- An account will be created in the database, logged in, and
current_accountset to this signed up and logged in account. - If an
on_signupaction onAccountsControlleris defined, that will be invoked. - If it is not defined:
- If a
redirectfield is set in the form, site will be redirected to its value. - If a
redirectfield is not set, site will be redirected back to the referrer.
- If a
On a failed signup:
-
current_accountwill be nil. - Site will be redirected back to the referrer.
Notes:
- You can render a logged in experience based on whether
current_accountwas set. - If you want to set the redirect URL, you can render the partial directly as
render "shared/signup", redirect: <custom_url>or define anon_signupaction in yourAccountsControllerthat performs the redirect.