revise_auth 0.1.1 → 0.2.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 +1 -1
- data/app/controllers/revise_auth/sessions_controller.rb +1 -1
- data/app/controllers/revise_auth_controller.rb +8 -0
- 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 +58 -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/backports.rb +24 -0
- data/lib/revise_auth/current.rb +6 -0
- data/lib/revise_auth/engine.rb +10 -1
- data/lib/revise_auth/model.rb +6 -3
- 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 +5 -59
- metadata +21 -5
- 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: 9fac687711be2bb236ceab0cb252c397a71775ccd00bf3e2b77ab61888555d16
|
4
|
+
data.tar.gz: 4ce79cc15599316b649ea7e8ef3c1c54cd1c74f4ce0bb26685932b7c4c4e3303
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cb6ca3ca5f50d741c5e5ee319f438f1912496ab4c1f025c6225f6b6297f5dc94b06d025ed2f95e75bc56f8dc2915e81f8cf05af2faa08fc8dc537f811fc0428
|
7
|
+
data.tar.gz: 5c861bee5d4506055a967a8d755ac6b3959ccdcd5b399544f00444330ef52fa99e83abe536bed22be4868fca47202332bec38e8dfa0ad75a442acfb11798d296
|
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 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
|
@@ -6,7 +6,7 @@ class ReviseAuth::EmailController < ReviseAuthController
|
|
6
6
|
if User.find_by(confirmation_token: params[:confirmation_token])&.confirm_email_change
|
7
7
|
flash[:notice] = I18n.t("revise_auth.email_confirmed")
|
8
8
|
user_signed_in?
|
9
|
-
redirect_to
|
9
|
+
redirect_to(user_signed_in? ? profile_path : root_path)
|
10
10
|
else
|
11
11
|
redirect_to root_path, alert: I18n.t("revise_auth.email_confirm_failed")
|
12
12
|
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
|
@@ -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,58 @@
|
|
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
|
+
"confirmation_token:string",
|
51
|
+
"confirmed_at:datetime",
|
52
|
+
"confirmation_sent_at:datetime",
|
53
|
+
"unconfirmed_email:string"
|
54
|
+
] + @original_attributes
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
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
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ReviseAuth
|
2
|
+
module Backports
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
class_methods do
|
6
|
+
# Prevent timing-based enumeration attacks.
|
7
|
+
# This can be removed when Rails 7.1 is released.
|
8
|
+
def authenticate_by(attributes)
|
9
|
+
passwords, identifiers = attributes.to_h.partition do |name, value|
|
10
|
+
!has_attribute?(name) && has_attribute?("#{name}_digest")
|
11
|
+
end.map(&:to_h)
|
12
|
+
|
13
|
+
raise ArgumentError, "One or more password arguments are required" if passwords.empty?
|
14
|
+
raise ArgumentError, "One or more finder arguments are required" if identifiers.empty?
|
15
|
+
if (record = find_by(identifiers))
|
16
|
+
record if passwords.count { |name, value| record.send(:"authenticate_#{name}", value) } == passwords.size
|
17
|
+
else
|
18
|
+
new(passwords)
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
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
@@ -3,15 +3,18 @@ module ReviseAuth
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
+
include Backports if Rails.gem_version < Gem::Version.new("7.1")
|
7
|
+
|
6
8
|
has_secure_password
|
7
9
|
has_secure_token :confirmation_token
|
8
10
|
|
9
11
|
validates :email, format: {with: URI::MailTo::EMAIL_REGEXP}, presence: true, uniqueness: true
|
10
12
|
validates :unconfirmed_email, format: {with: URI::MailTo::EMAIL_REGEXP}, allow_blank: true
|
13
|
+
validates_length_of :password, minimum: 12, allow_nil: true
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
+
before_validation do
|
16
|
+
email&.downcase!&.strip!
|
17
|
+
unconfirmed_email&.downcase!
|
15
18
|
end
|
16
19
|
end
|
17
20
|
|
@@ -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,11 @@
|
|
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 :Backports, "revise_auth/backports"
|
8
|
+
autoload :Current, "revise_auth/current"
|
5
9
|
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
|
10
|
+
autoload :RouteConstraint, "revise_auth/route_constraint"
|
65
11
|
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.2.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-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -58,17 +58,33 @@ 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/backports.rb
|
74
|
+
- lib/revise_auth/current.rb
|
64
75
|
- lib/revise_auth/engine.rb
|
65
76
|
- lib/revise_auth/model.rb
|
77
|
+
- lib/revise_auth/route_constraint.rb
|
78
|
+
- lib/revise_auth/routes.rb
|
66
79
|
- lib/revise_auth/version.rb
|
67
80
|
- lib/tasks/revise_auth_tasks.rake
|
68
81
|
homepage: https://github.com/excid3/revise_auth
|
69
82
|
licenses:
|
70
83
|
- MIT
|
71
|
-
metadata:
|
84
|
+
metadata:
|
85
|
+
homepage_uri: https://github.com/excid3/revise_auth
|
86
|
+
source_code_uri: https://github.com/excid3/revise_auth
|
87
|
+
changelog_uri: https://github.com/excid3/revise_auth/blob/main/CHANGELOG.md
|
72
88
|
post_install_message:
|
73
89
|
rdoc_options: []
|
74
90
|
require_paths:
|
@@ -84,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
100
|
- !ruby/object:Gem::Version
|
85
101
|
version: '0'
|
86
102
|
requirements: []
|
87
|
-
rubygems_version: 3.4.
|
103
|
+
rubygems_version: 3.4.7
|
88
104
|
signing_key:
|
89
105
|
specification_version: 4
|
90
106
|
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
|