revise_auth 0.1.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +38 -9
- data/Rakefile +16 -1
- data/app/controllers/revise_auth/email_controller.rb +2 -3
- data/app/controllers/revise_auth/sessions_controller.rb +1 -1
- data/app/controllers/revise_auth_controller.rb +8 -0
- data/app/mailers/revise_auth/mailer.rb +4 -1
- data/app/views/revise_auth/mailer/confirm_email.html.erb +2 -2
- data/config/locales/de.yml +16 -0
- data/config/locales/el.yml +16 -0
- data/config/locales/fr.yml +16 -0
- data/config/locales/nl.yml +16 -0
- data/config/locales/tr.yml +16 -0
- data/config/locales/zh-TW.yml +16 -0
- data/lib/generators/revise_auth/model_generator.rb +56 -0
- data/lib/generators/revise_auth/templates/README +4 -0
- data/lib/generators/revise_auth/views_generator.rb +21 -0
- data/lib/revise_auth/authentication.rb +56 -0
- data/lib/revise_auth/current.rb +6 -0
- data/lib/revise_auth/engine.rb +10 -1
- data/lib/revise_auth/model.rb +14 -24
- data/lib/revise_auth/route_constraint.rb +15 -0
- data/lib/revise_auth/routes.rb +49 -0
- data/lib/revise_auth/version.rb +1 -1
- data/lib/revise_auth.rb +4 -59
- metadata +22 -7
- data/config/routes.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5d5afd1201c6a751a50e0c153589fa552e949c45cad6da9f3147670e6aaf274
|
4
|
+
data.tar.gz: 9d546d451af57bbc97d4e6dbae0dc83b00b6818fa14210f1a32735f79295686e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39331cdedcd79bc56c9237436bbf844ea5b1b863242b3cf4b5935a48bc492e2ebfdc8d5e5bc987f92b22556f58558876e418e94c2850d1eb0be659e54067ec1e
|
7
|
+
data.tar.gz: d1a39f4b6f81989be85657b540fcad3f0fbcb0a52ea3b184ea86263391ea204e192adf7038e164da190c03d6837da4c2ce8f3157779b6489cad65981e78f877f
|
data/README.md
CHANGED
@@ -1,28 +1,57 @@
|
|
1
1
|
# ReviseAuth
|
2
|
-
Short description and motivation.
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/revise_auth.svg)](https://badge.fury.io/rb/revise_auth)
|
4
|
+
|
5
|
+
A pure Ruby on Rails authentication system like Devise.
|
6
6
|
|
7
7
|
## Installation
|
8
|
+
|
8
9
|
Add this line to your application's Gemfile:
|
9
10
|
|
10
11
|
```ruby
|
11
|
-
|
12
|
+
bundle add "revise_auth"
|
12
13
|
```
|
13
14
|
|
14
|
-
And then execute:
|
15
|
+
And then execute the following to generate a `User` model (optionally adding other fields such as `first_name` and `last_name`):
|
15
16
|
```bash
|
16
|
-
$
|
17
|
+
$ rails g revise_auth:model User first_name last_name
|
18
|
+
$ rails db:migrate
|
17
19
|
```
|
18
20
|
|
19
|
-
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
ReviseAuth is designed around a single `User` model.
|
24
|
+
|
25
|
+
### Roles / Other User Types
|
26
|
+
|
27
|
+
ReviseAuth only works with a single model to keep things simple. We recommend adding roles to handle other types of users.
|
28
|
+
|
29
|
+
You can accomplish this in a few different ways:
|
30
|
+
|
31
|
+
* A `roles` attribute on the `User` model
|
32
|
+
* The Rolify gem
|
33
|
+
|
34
|
+
## Customizing
|
35
|
+
|
36
|
+
To customize views, you can run:
|
37
|
+
|
20
38
|
```bash
|
21
|
-
$
|
39
|
+
$ rails g revise_auth:views
|
22
40
|
```
|
23
41
|
|
42
|
+
This will copy the views into `app/views/revise_auth` in your application.
|
43
|
+
|
24
44
|
## Contributing
|
25
|
-
|
45
|
+
|
46
|
+
If you have an issue you'd like to submit, please do so using the issue tracker in GitHub. In order for us to help you in the best way possible, please be as detailed as you can.
|
47
|
+
|
48
|
+
If you'd like to open a PR please make sure the following things pass:
|
49
|
+
|
50
|
+
```bash
|
51
|
+
bin/rails db:test:prepare
|
52
|
+
bin/rails test
|
53
|
+
bundle exec standardrb
|
54
|
+
```
|
26
55
|
|
27
56
|
## License
|
28
57
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -1,3 +1,18 @@
|
|
1
1
|
require "bundler/setup"
|
2
|
-
|
3
2
|
require "bundler/gem_tasks"
|
3
|
+
require "rake/testtask"
|
4
|
+
|
5
|
+
APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
|
6
|
+
load "rails/tasks/engine.rake"
|
7
|
+
load "rails/tasks/statistics.rake"
|
8
|
+
|
9
|
+
desc "Run tests"
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << "lib"
|
12
|
+
t.libs << "test"
|
13
|
+
t.pattern = "test/**/*_test.rb"
|
14
|
+
t.verbose = true
|
15
|
+
t.warning = false
|
16
|
+
end
|
17
|
+
|
18
|
+
task default: :test
|
@@ -3,10 +3,9 @@ class ReviseAuth::EmailController < ReviseAuthController
|
|
3
3
|
|
4
4
|
# GET /profile/email?confirmation_token=abcdef
|
5
5
|
def show
|
6
|
-
if User.
|
6
|
+
if User.find_by_token_for(:email_verification, params[:confirmation_token])&.confirm_email_change
|
7
7
|
flash[:notice] = I18n.t("revise_auth.email_confirmed")
|
8
|
-
user_signed_in?
|
9
|
-
redirect_to (user_signed_in? ? profile_path : root_path)
|
8
|
+
redirect_to(user_signed_in? ? profile_path : root_path)
|
10
9
|
else
|
11
10
|
redirect_to root_path, alert: I18n.t("revise_auth.email_confirm_failed")
|
12
11
|
end
|
@@ -3,7 +3,7 @@ class ReviseAuth::SessionsController < ReviseAuthController
|
|
3
3
|
end
|
4
4
|
|
5
5
|
def create
|
6
|
-
if user = User.
|
6
|
+
if (user = User.authenticate_by(email: params[:email], password: params[:password]))
|
7
7
|
login(user)
|
8
8
|
redirect_to root_path
|
9
9
|
else
|
@@ -1,2 +1,10 @@
|
|
1
1
|
class ReviseAuthController < ApplicationController
|
2
|
+
# Return true if it's a revise_auth_controller. false to all controllers unless
|
3
|
+
# the controllers defined inside revise_auth. Useful if you want to apply a before
|
4
|
+
# filter to all controllers, except the ones in revise_auth:
|
5
|
+
#
|
6
|
+
# before_action :authenticate_user!, except: :revise_auth_controller?
|
7
|
+
def revise_auth_controller?
|
8
|
+
is_a?(::ReviseAuthController)
|
9
|
+
end
|
2
10
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
<p>Welcome <%=
|
1
|
+
<p>Welcome <%= @user.unconfirmed_email %>!</p>
|
2
2
|
|
3
3
|
<p>You can confirm your account email through the link below:</p>
|
4
4
|
|
5
|
-
<p><%= link_to
|
5
|
+
<p><%= link_to "Confirm my account", profile_email_url(confirmation_token: @token) %></p>
|
6
6
|
|
7
7
|
<p>This link will expire in 24 hours.</p>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
de:
|
2
|
+
revise_auth:
|
3
|
+
account_deleted: "Dein Account wurde gelöscht."
|
4
|
+
account_updated: "Account wurde erfolgreich aktualisiert."
|
5
|
+
|
6
|
+
invalid_email_or_password: "Ungültige Email oder Passwort."
|
7
|
+
sign_up_or_login: "Registrieren oder anmelden um fortzufahren."
|
8
|
+
|
9
|
+
# Password changes
|
10
|
+
password_changed: "Dein Passwort wurde erfolgreich geändert."
|
11
|
+
incorrect_password: "Das Passwort ist ungültig. Bitte versuche es erneut."
|
12
|
+
|
13
|
+
# Email confirmations
|
14
|
+
email_confirmed: "Deine Email wurde erfogreich bestätigt."
|
15
|
+
email_confirm_failed: "Email Adresse kann nicht bestätigt werden."
|
16
|
+
confirmation_email_sent: "Eine Bestätigungsemail wurde versandt an %{email}"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
el:
|
2
|
+
revise_auth:
|
3
|
+
account_deleted: "Ο λογαριασμός σας έχει διαγραφεί."
|
4
|
+
account_updated: "Ο λογαριασμός σας έχει ενημερωθεί επιτυχώς."
|
5
|
+
|
6
|
+
invalid_email_or_password: "Μη έγκυρο email ή κωδικός πρόσβασης."
|
7
|
+
sign_up_or_login: "Εγγραφείτε ή συνδεθείτε για να συνεχίσετε."
|
8
|
+
|
9
|
+
# Password changes
|
10
|
+
password_changed: "Ο κωδικός πρόσβασής σας άλλαξε με επιτυχία."
|
11
|
+
incorrect_password: "Ο τρέχων κωδικός πρόσβασής σας είναι λανθασμένος. Παρακαλώ δοκιμάστε ξανά."
|
12
|
+
|
13
|
+
# Email confirmations
|
14
|
+
email_confirmed: "Η διεύθυνση email σας επιβεβαιώθηκε επιτυχώς."
|
15
|
+
email_confirm_failed: "Δεν είναι δυνατή η επιβεβαίωση της διεύθυνσης email."
|
16
|
+
confirmation_email_sent: "Ένα email επιβεβαίωσης έχει σταλεί στο %{email}"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
fr:
|
2
|
+
revise_auth:
|
3
|
+
account_deleted: "Votre compte a été supprimé."
|
4
|
+
account_updated: "Votre compte a été mis à jour."
|
5
|
+
|
6
|
+
invalid_email_or_password: "Email ou mot de passe incorrect."
|
7
|
+
sign_up_or_login: "Vous devez être connecté ou vous enregistrer pour continuer."
|
8
|
+
|
9
|
+
# Password changes
|
10
|
+
password_changed: "Votre mot de passe a été mis à jour avec succès."
|
11
|
+
incorrect_password: "Mot de passe incorrect. Merci de réessayer"
|
12
|
+
|
13
|
+
# Email confirmations
|
14
|
+
email_confirmed: "Votre adresse email vient d'être confirmé."
|
15
|
+
email_confirm_failed: "Impossible de confirmer votre adresse email."
|
16
|
+
confirmation_email_sent: "Un email de confirmation vient d'être envoyé à %{email}"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
nl:
|
2
|
+
revise_auth:
|
3
|
+
account_deleted: "Uw account is verwijderd."
|
4
|
+
account_updated: "Account succesvol bijgewerkt."
|
5
|
+
|
6
|
+
invalid_email_of_password: "Ongeldige e-mail of wachtwoord."
|
7
|
+
sign_up_or_login: "Aanmelden of inloggen om door te gaan."
|
8
|
+
|
9
|
+
# Password changes
|
10
|
+
password_changed: "Uw wachtwoord is succesvol gewijzigd."
|
11
|
+
incorrect_password: "Uw huidige wachtwoord is onjuist. Probeer het opnieuw."
|
12
|
+
|
13
|
+
# E-mail confirmations
|
14
|
+
email_confirmed: "Uw e-mailadres is succesvol bevestigd."
|
15
|
+
email_confirm_failed: "E-mailadres bevestigen niet mogelijk."
|
16
|
+
confirmation_email_sent: "Er is een bevestigingsmail verzonden naar %{email}"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
tr:
|
2
|
+
revise_auth:
|
3
|
+
account_deleted: "Hesabınız silindi."
|
4
|
+
account_updated: "Hesap başarıyla güncellendi."
|
5
|
+
|
6
|
+
invalid_email_or_password: "Geçersiz e-posta veya şifre."
|
7
|
+
sign_up_or_login: "Devam etmek için kaydol veya giriş yap."
|
8
|
+
|
9
|
+
# Password changes
|
10
|
+
password_changed: "Şifreniz başarıyla güncellendi."
|
11
|
+
incorrect_password: "Şu anki şifreniz yanlış. Lütfen tekrar deneyiniz."
|
12
|
+
|
13
|
+
# Email confirmations
|
14
|
+
email_confirmed: "E-posta adresiniz başarıyla onaylandı."
|
15
|
+
email_confirm_failed: "E-posta adresi doğrulanamıyor."
|
16
|
+
confirmation_email_sent: "%{email} adresine onay e-postası gönderildi."
|
@@ -0,0 +1,16 @@
|
|
1
|
+
zh-TW:
|
2
|
+
revise_auth:
|
3
|
+
account_deleted: "您的帳號已經被刪除"
|
4
|
+
account_updated: "帳號更新成功"
|
5
|
+
|
6
|
+
invalid_email_or_password: "錯誤的信箱或是密碼"
|
7
|
+
sign_up_or_login: "需要註冊或是登入才能進行"
|
8
|
+
|
9
|
+
# Password changes
|
10
|
+
password_changed: "您的密碼已經成功地更新"
|
11
|
+
incorrect_password: "您現在輸入的密碼不正確, 請重新嘗試"
|
12
|
+
|
13
|
+
# Email confirmations
|
14
|
+
email_confirmed: "您的電子信箱已經成功地通過驗證"
|
15
|
+
email_confirm_failed: "無法驗證您的電子信箱"
|
16
|
+
confirmation_email_sent: "電子信箱驗證信已寄往 %{email}"
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module ReviseAuth
|
2
|
+
module Generators
|
3
|
+
class ModelGenerator < Rails::Generators::NamedBase
|
4
|
+
include Rails::Generators::ResourceHelpers
|
5
|
+
|
6
|
+
desc "Generates a model for authentication, default User"
|
7
|
+
|
8
|
+
source_root File.expand_path("templates", __dir__)
|
9
|
+
|
10
|
+
argument :name, required: false, default: "User"
|
11
|
+
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
12
|
+
|
13
|
+
def initialize(args, *options)
|
14
|
+
@original_attributes = args[1..] || []
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def generate_model
|
19
|
+
generate :model, name, *model_attributes
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_revise_auth_model
|
23
|
+
inject_into_class model_path, class_name, " include ReviseAuth::Model\n"
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_uniq_to_email_index
|
27
|
+
insert_into_file migration_path, after: "#{name.downcase.pluralize}, :email", force: true do
|
28
|
+
", unique: true"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def done
|
33
|
+
readme "README" if behavior == :invoke
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def migration_path
|
39
|
+
@migration_path ||= Dir.glob(Rails.root.join("db/migrate/*")).max_by { |f| File.mtime(f) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def model_path
|
43
|
+
@model_path ||= File.join("app", "models", "#{file_path}.rb")
|
44
|
+
end
|
45
|
+
|
46
|
+
def model_attributes
|
47
|
+
[
|
48
|
+
"email:string:index",
|
49
|
+
"password_digest:string",
|
50
|
+
"confirmed_at:datetime",
|
51
|
+
"unconfirmed_email:string"
|
52
|
+
] + @original_attributes
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
module ReviseAuth
|
4
|
+
module Generators
|
5
|
+
class ViewsGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("../../../..", __FILE__)
|
7
|
+
|
8
|
+
class_option :views, aliases: "-v", type: :array, desc: "Select specific view directories to generate (confirmations, passwords, registrations, sessions, unlocks, mailer)"
|
9
|
+
|
10
|
+
def copy_views
|
11
|
+
if options[:views]
|
12
|
+
options[:views].each do |directory|
|
13
|
+
directory "app/views/revise_auth/#{directory}"
|
14
|
+
end
|
15
|
+
else
|
16
|
+
directory "app/views/revise_auth"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module ReviseAuth
|
2
|
+
module Authentication
|
3
|
+
# Provides methods for controllers and views for authentication
|
4
|
+
#
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
helper_method :user_signed_in?
|
9
|
+
helper_method :current_user
|
10
|
+
end
|
11
|
+
|
12
|
+
# Returns a boolean whether the user is signed in or not
|
13
|
+
def user_signed_in?
|
14
|
+
!!current_user
|
15
|
+
end
|
16
|
+
|
17
|
+
# Authenticates the user if not already authenticated
|
18
|
+
# Returns a User or nil
|
19
|
+
def current_user
|
20
|
+
Current.user ||= authenticate_user
|
21
|
+
end
|
22
|
+
|
23
|
+
# Authenticates a user or redirects to the login page
|
24
|
+
def authenticate_user!
|
25
|
+
redirect_to login_path, alert: I18n.t("revise_auth.sign_up_or_login") unless user_signed_in?
|
26
|
+
end
|
27
|
+
|
28
|
+
# Authenticates the current user
|
29
|
+
# - from session cookie
|
30
|
+
# - (future) from Authorization header
|
31
|
+
def authenticate_user
|
32
|
+
Current.user = authenticated_user_from_session
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns a user from session cookie
|
36
|
+
def authenticated_user_from_session
|
37
|
+
user_id = session[:user_id]
|
38
|
+
return unless user_id
|
39
|
+
User.find_by(id: user_id)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Logs in the user
|
43
|
+
# - Set Current.user for the current request
|
44
|
+
# - Save a session cookie so the next request is authenticated
|
45
|
+
def login(user)
|
46
|
+
Current.user = user
|
47
|
+
reset_session
|
48
|
+
session[:user_id] = user.id
|
49
|
+
end
|
50
|
+
|
51
|
+
def logout
|
52
|
+
Current.user = nil
|
53
|
+
reset_session
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/revise_auth/engine.rb
CHANGED
@@ -1,9 +1,18 @@
|
|
1
1
|
module ReviseAuth
|
2
2
|
class Engine < ::Rails::Engine
|
3
|
-
initializer
|
3
|
+
initializer :revise_auth_controller do
|
4
4
|
ActiveSupport.on_load(:action_controller_base) do
|
5
5
|
include ReviseAuth::Authentication
|
6
6
|
end
|
7
7
|
end
|
8
|
+
|
9
|
+
# Set default session expiration of 30 days if not specified
|
10
|
+
# Runs immediately after Rails defines the default session store
|
11
|
+
# https://github.com/rails/rails/blob/7-0-stable/railties/lib/rails/application/finisher.rb#L43-L49
|
12
|
+
initializer :revise_auth_cookie_session_expiry, after: :setup_default_session_store do |app|
|
13
|
+
if app.config.session_store == ActionDispatch::Session::CookieStore
|
14
|
+
app.config.session_options.with_defaults! expire_after: 30.days
|
15
|
+
end
|
16
|
+
end
|
8
17
|
end
|
9
18
|
end
|
data/lib/revise_auth/model.rb
CHANGED
@@ -2,44 +2,34 @@ module ReviseAuth
|
|
2
2
|
module Model
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
included do
|
5
|
+
included do |base|
|
6
|
+
base.const_set :EMAIL_VERIFICATION_TOKEN_VALIDITY, 1.day
|
7
|
+
|
6
8
|
has_secure_password
|
7
9
|
has_secure_token :confirmation_token
|
8
10
|
|
11
|
+
generates_token_for :email_verification, expires_in: base.const_get(:EMAIL_VERIFICATION_TOKEN_VALIDITY) do
|
12
|
+
email
|
13
|
+
end
|
14
|
+
|
9
15
|
validates :email, format: {with: URI::MailTo::EMAIL_REGEXP}, presence: true, uniqueness: true
|
10
16
|
validates :unconfirmed_email, format: {with: URI::MailTo::EMAIL_REGEXP}, allow_blank: true
|
17
|
+
validates_length_of :password, minimum: 12, allow_nil: true
|
11
18
|
|
12
|
-
|
13
|
-
|
14
|
-
|
19
|
+
before_validation do
|
20
|
+
email&.downcase!&.strip!
|
21
|
+
unconfirmed_email&.downcase!
|
15
22
|
end
|
16
23
|
end
|
17
24
|
|
18
25
|
# Generates a confirmation token and send email to the user
|
19
26
|
def send_confirmation_instructions
|
20
|
-
|
21
|
-
|
22
|
-
confirmation_sent_at: Time.current
|
23
|
-
)
|
24
|
-
ReviseAuth::Mailer.with(user: self).confirm_email.deliver_later
|
27
|
+
token = generate_token_for(:email_verification)
|
28
|
+
ReviseAuth::Mailer.with(user: self, token: token).confirm_email.deliver_later
|
25
29
|
end
|
26
30
|
|
27
|
-
# Confirms an email address change
|
28
31
|
def confirm_email_change
|
29
|
-
|
30
|
-
false
|
31
|
-
else
|
32
|
-
update(
|
33
|
-
confirmed_at: Time.current,
|
34
|
-
email: unconfirmed_email,
|
35
|
-
unconfirmed_email: nil
|
36
|
-
)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# Checks whether the confirmation token is within the valid time
|
41
|
-
def confirmation_period_expired?
|
42
|
-
confirmation_sent_at.before?(1.day.ago)
|
32
|
+
update(confirmed_at: Time.current, email: unconfirmed_email)
|
43
33
|
end
|
44
34
|
end
|
45
35
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ActionDispatch::Routing
|
2
|
+
class Mapper
|
3
|
+
def revise_auth
|
4
|
+
scope module: :revise_auth do
|
5
|
+
revise_registration
|
6
|
+
|
7
|
+
get "login", to: "sessions#new"
|
8
|
+
post "login", to: "sessions#create"
|
9
|
+
|
10
|
+
revise_profile
|
11
|
+
|
12
|
+
patch "profile/email", to: "email#update"
|
13
|
+
patch "profile/password", to: "password#update"
|
14
|
+
|
15
|
+
# Email confirmation
|
16
|
+
get "profile/email", to: "email#show"
|
17
|
+
|
18
|
+
delete "logout", to: "sessions#destroy"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Adds helpers for config/routes.rb to constraint routes with authentication
|
23
|
+
#
|
24
|
+
def authenticated
|
25
|
+
constraints ->(request) { ReviseAuth::RouteConstraint.new(request).user_signed_in? } do
|
26
|
+
yield
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def unauthenticated
|
31
|
+
constraints ->(request) { !ReviseAuth::RouteConstraint.new(request).user_signed_in? } do
|
32
|
+
yield
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def revise_registration
|
39
|
+
get "sign_up", to: "registrations#new"
|
40
|
+
post "sign_up", to: "registrations#create"
|
41
|
+
end
|
42
|
+
|
43
|
+
def revise_profile
|
44
|
+
get "profile", to: "registrations#edit"
|
45
|
+
patch "profile", to: "registrations#update"
|
46
|
+
delete "profile", to: "registrations#destroy"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/revise_auth/version.rb
CHANGED
data/lib/revise_auth.rb
CHANGED
@@ -1,65 +1,10 @@
|
|
1
1
|
require "revise_auth/version"
|
2
2
|
require "revise_auth/engine"
|
3
|
+
require "revise_auth/routes"
|
3
4
|
|
4
5
|
module ReviseAuth
|
6
|
+
autoload :Authentication, "revise_auth/authentication"
|
7
|
+
autoload :Current, "revise_auth/current"
|
5
8
|
autoload :Model, "revise_auth/model"
|
6
|
-
|
7
|
-
module Authentication
|
8
|
-
# Provides methods for controllers and views for authentication
|
9
|
-
#
|
10
|
-
extend ActiveSupport::Concern
|
11
|
-
|
12
|
-
included do
|
13
|
-
helper_method :user_signed_in?
|
14
|
-
helper_method :current_user
|
15
|
-
end
|
16
|
-
|
17
|
-
# Returns a boolean whether the user is signed in or not
|
18
|
-
def user_signed_in?
|
19
|
-
!!current_user
|
20
|
-
end
|
21
|
-
|
22
|
-
# Authenticates the user if not already authenticated
|
23
|
-
# Returns a User or nil
|
24
|
-
def current_user
|
25
|
-
Current.user ||= authenticate_user
|
26
|
-
end
|
27
|
-
|
28
|
-
# Authenticates a user or redirects to the login page
|
29
|
-
def authenticate_user!
|
30
|
-
redirect_to login_path, alert: I18n.t("revise_auth.sign_up_or_login") unless user_signed_in?
|
31
|
-
end
|
32
|
-
|
33
|
-
# Authenticates the current user
|
34
|
-
# - from session cookie
|
35
|
-
# - (future) from Authorization header
|
36
|
-
def authenticate_user
|
37
|
-
Current.user = authenticated_user_from_session
|
38
|
-
end
|
39
|
-
|
40
|
-
# Returns a user from session cookie
|
41
|
-
def authenticated_user_from_session
|
42
|
-
user_id = session[:user_id]
|
43
|
-
return unless user_id
|
44
|
-
User.find_by(id: user_id)
|
45
|
-
end
|
46
|
-
|
47
|
-
# Logs in the user
|
48
|
-
# - Set Current.user for the current request
|
49
|
-
# - Save a session cookie so the next request is authenticated
|
50
|
-
def login(user)
|
51
|
-
Current.user = user
|
52
|
-
session[:user_id] = user.id
|
53
|
-
end
|
54
|
-
|
55
|
-
def logout
|
56
|
-
Current.user = nil
|
57
|
-
session.delete(:user_id)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
class Current < ActiveSupport::CurrentAttributes
|
62
|
-
# Stores the current user for the request
|
63
|
-
attribute :user
|
64
|
-
end
|
9
|
+
autoload :RouteConstraint, "revise_auth/route_constraint"
|
65
10
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: revise_auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 7.0.
|
19
|
+
version: 7.1.0.beta1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 7.0.
|
26
|
+
version: 7.1.0.beta1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bcrypt
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,17 +58,32 @@ files:
|
|
58
58
|
- app/views/revise_auth/registrations/edit.html.erb
|
59
59
|
- app/views/revise_auth/registrations/new.html.erb
|
60
60
|
- app/views/revise_auth/sessions/new.html.erb
|
61
|
+
- config/locales/de.yml
|
62
|
+
- config/locales/el.yml
|
61
63
|
- config/locales/en.yml
|
62
|
-
- config/
|
64
|
+
- config/locales/fr.yml
|
65
|
+
- config/locales/nl.yml
|
66
|
+
- config/locales/tr.yml
|
67
|
+
- config/locales/zh-TW.yml
|
68
|
+
- lib/generators/revise_auth/model_generator.rb
|
69
|
+
- lib/generators/revise_auth/templates/README
|
70
|
+
- lib/generators/revise_auth/views_generator.rb
|
63
71
|
- lib/revise_auth.rb
|
72
|
+
- lib/revise_auth/authentication.rb
|
73
|
+
- lib/revise_auth/current.rb
|
64
74
|
- lib/revise_auth/engine.rb
|
65
75
|
- lib/revise_auth/model.rb
|
76
|
+
- lib/revise_auth/route_constraint.rb
|
77
|
+
- lib/revise_auth/routes.rb
|
66
78
|
- lib/revise_auth/version.rb
|
67
79
|
- lib/tasks/revise_auth_tasks.rake
|
68
80
|
homepage: https://github.com/excid3/revise_auth
|
69
81
|
licenses:
|
70
82
|
- MIT
|
71
|
-
metadata:
|
83
|
+
metadata:
|
84
|
+
homepage_uri: https://github.com/excid3/revise_auth
|
85
|
+
source_code_uri: https://github.com/excid3/revise_auth
|
86
|
+
changelog_uri: https://github.com/excid3/revise_auth/blob/main/CHANGELOG.md
|
72
87
|
post_install_message:
|
73
88
|
rdoc_options: []
|
74
89
|
require_paths:
|
@@ -84,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
99
|
- !ruby/object:Gem::Version
|
85
100
|
version: '0'
|
86
101
|
requirements: []
|
87
|
-
rubygems_version: 3.4.
|
102
|
+
rubygems_version: 3.4.19
|
88
103
|
signing_key:
|
89
104
|
specification_version: 4
|
90
105
|
summary: Simple authentication for Ruby on Rails apps
|
data/config/routes.rb
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
Rails.application.routes.draw do
|
2
|
-
scope module: :revise_auth do
|
3
|
-
get "sign_up", to: "registrations#new"
|
4
|
-
post "sign_up", to: "registrations#create"
|
5
|
-
|
6
|
-
get "login", to: "sessions#new"
|
7
|
-
post "login", to: "sessions#create"
|
8
|
-
|
9
|
-
get "profile", to: "registrations#edit"
|
10
|
-
patch "profile", to: "registrations#update"
|
11
|
-
delete "profile", to: "registrations#destroy"
|
12
|
-
|
13
|
-
patch "profile/email", to: "email#update"
|
14
|
-
patch "profile/password", to: "password#update"
|
15
|
-
|
16
|
-
# Email confirmation
|
17
|
-
get "profile/email", to: "email#show"
|
18
|
-
|
19
|
-
delete "logout", to: "sessions#destroy"
|
20
|
-
end
|
21
|
-
end
|