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:migrations
bundle 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/login
provided by the railtie, in any page. - Create a custom login form with an
email
field, apassword
field and the action set tologin_path
. On a successful login, site will be redirected to thenext
field, if any.
On a successful login:
-
current_account
will be set to the logged in account. - If an
on_login
action onAccountsController
is defined, that will be invoked. - If it is not defined:
- If a
redirect
field is set in the form, site will be redirected to its value. - If a
redirect
field is not set, site will be redirected back to the referrer.
- If a
On a failed login:
-
current_account
will be nil. - Site will be redirected back to the referrer.
Notes:
- You can render a logged in experience based on whether
current_account
was 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_login
action in yourAccountsController
that 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/signup
provided by the railtie, in any page. - Create a custom form with an
email
field, apassword
field an the action set tosignup_path
.
On a successful signup:
- An account will be created in the database, logged in, and
current_account
set to this signed up and logged in account. - If an
on_signup
action onAccountsController
is defined, that will be invoked. - If it is not defined:
- If a
redirect
field is set in the form, site will be redirected to its value. - If a
redirect
field is not set, site will be redirected back to the referrer.
- If a
On a failed signup:
-
current_account
will be nil. - Site will be redirected back to the referrer.
Notes:
- You can render a logged in experience based on whether
current_account
was 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_signup
action in yourAccountsController
that performs the redirect.