searls-auth 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +675 -0
- data/README.md +1 -0
- data/Rakefile +6 -0
- data/app/controllers/searls/auth/base_controller.rb +35 -0
- data/app/controllers/searls/auth/logins_controller.rb +44 -0
- data/app/controllers/searls/auth/registrations_controller.rb +35 -0
- data/app/controllers/searls/auth/verifications_controller.rb +64 -0
- data/app/helpers/searls/auth/application_helper.rb +82 -0
- data/app/javascript/controllers/searls_auth_login_controller.js +37 -0
- data/app/javascript/controllers/searls_auth_otp_controller.js +21 -0
- data/app/mailers/searls/auth/base_mailer.rb +8 -0
- data/app/mailers/searls/auth/login_link_mailer.rb +24 -0
- data/app/views/searls/auth/layouts/mailer.html.erb +54 -0
- data/app/views/searls/auth/login_link_mailer/login_link.html.erb +37 -0
- data/app/views/searls/auth/login_link_mailer/login_link.text.erb +20 -0
- data/app/views/searls/auth/logins/show.html.erb +20 -0
- data/app/views/searls/auth/registrations/show.html.erb +19 -0
- data/app/views/searls/auth/verifications/show.html.erb +23 -0
- data/config/importmap.rb +2 -0
- data/config/routes.rb +12 -0
- data/lib/searls/auth/authenticates_user.rb +32 -0
- data/lib/searls/auth/config.rb +47 -0
- data/lib/searls/auth/creates_user.rb +37 -0
- data/lib/searls/auth/emails_link.rb +15 -0
- data/lib/searls/auth/engine.rb +28 -0
- data/lib/searls/auth/railtie.rb +20 -0
- data/lib/searls/auth/resets_session.rb +13 -0
- data/lib/searls/auth/version.rb +5 -0
- data/lib/searls/auth.rb +63 -0
- metadata +87 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative "engine"
|
2
|
+
|
3
|
+
module Searls
|
4
|
+
module Auth
|
5
|
+
class CreatesUser
|
6
|
+
include Engine.routes.url_helpers
|
7
|
+
Result = Struct.new(:user, :success?, :error_messages)
|
8
|
+
|
9
|
+
def call(params)
|
10
|
+
user = Searls::Auth.config.user_finder_by_email.call(params[:email]) ||
|
11
|
+
Searls::Auth.config.user_initializer.call(params)
|
12
|
+
|
13
|
+
if user.persisted?
|
14
|
+
Result.new(nil, false, ["An account already exists for that email address. <a href=\"#{login_path(**forwardable_params(params))}\">Log in</a> instead?".html_safe])
|
15
|
+
elsif (errors = Searls::Auth.config.validate_registration.call(user, params, [])).any?
|
16
|
+
Result.new(nil, false, errors)
|
17
|
+
elsif user.save
|
18
|
+
Result.new(user, true)
|
19
|
+
else
|
20
|
+
Result.new(user, false, simplified_error_messages(user))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def forwardable_params(params)
|
27
|
+
params.permit(:redirect_path, :redirect_subdomain, :email)
|
28
|
+
end
|
29
|
+
|
30
|
+
def simplified_error_messages(model)
|
31
|
+
model.errors.details.keys.map { |attr|
|
32
|
+
model.errors.full_messages_for(attr).first
|
33
|
+
}.join
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Searls
|
2
|
+
module Auth
|
3
|
+
class EmailsLink
|
4
|
+
def email(user:, short_code:, redirect_path: nil, redirect_subdomain: nil)
|
5
|
+
LoginLinkMailer.with(
|
6
|
+
user:,
|
7
|
+
token: Searls::Auth.config.token_generator.call(user),
|
8
|
+
short_code:,
|
9
|
+
redirect_path:,
|
10
|
+
redirect_subdomain:
|
11
|
+
).login_link.deliver_later
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Searls
|
2
|
+
module Auth
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
isolate_namespace Searls::Auth
|
5
|
+
|
6
|
+
initializer "searls.auth.helpers" do
|
7
|
+
ActiveSupport.on_load(:action_controller) do
|
8
|
+
helper Searls::Auth::Engine.helpers
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
initializer "searls.auth.url_options" do |app|
|
13
|
+
Searls::Auth::Engine.routes.default_url_options = app.routes.default_url_options
|
14
|
+
end
|
15
|
+
|
16
|
+
initializer "searls.auth.assets" do |app|
|
17
|
+
app.config.assets.paths << root.join("app/javascript")
|
18
|
+
end
|
19
|
+
|
20
|
+
initializer "searls.auth.importmap", before: "importmap" do |app|
|
21
|
+
if app.config.respond_to?(:importmap)
|
22
|
+
app.config.importmap.paths << root.join("config/importmap.rb")
|
23
|
+
app.config.importmap.cache_sweepers << root.join("app/javascript")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Searls
|
2
|
+
module Auth
|
3
|
+
class Railtie < ::Rails::Railtie
|
4
|
+
# Register rake tasks if needed
|
5
|
+
# rake_tasks do
|
6
|
+
# load "tasks/searls_auth_tasks.rake"
|
7
|
+
# end
|
8
|
+
|
9
|
+
# Register generators if needed
|
10
|
+
# generators do
|
11
|
+
# require "generators/searls_auth_generator"
|
12
|
+
# end
|
13
|
+
|
14
|
+
# Initialize configuration defaults
|
15
|
+
initializer "searls.auth.configure" do |app|
|
16
|
+
# Set up any configuration defaults here
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Searls
|
2
|
+
module Auth
|
3
|
+
class ResetsSession
|
4
|
+
def reset(receiver, except_for:)
|
5
|
+
backup = Searls::Auth.config.preserve_session_keys_after_logout.map { |key|
|
6
|
+
[key, receiver.session[key]]
|
7
|
+
}
|
8
|
+
receiver.reset_session
|
9
|
+
backup.each { |key, value| receiver.session[key] = value }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/searls/auth.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative "auth/authenticates_user"
|
2
|
+
require_relative "auth/config"
|
3
|
+
require_relative "auth/creates_user" if defined?(Rails)
|
4
|
+
require_relative "auth/emails_link"
|
5
|
+
require_relative "auth/engine" if defined?(Rails)
|
6
|
+
require_relative "auth/railtie" if defined?(Rails)
|
7
|
+
require_relative "auth/resets_session"
|
8
|
+
require_relative "auth/version"
|
9
|
+
|
10
|
+
module Searls
|
11
|
+
module Auth
|
12
|
+
class Error < StandardError; end
|
13
|
+
|
14
|
+
DEFAULT_CONFIG = {
|
15
|
+
# Data setup
|
16
|
+
user_finder_by_email: ->(email) { User.find_by(email:) },
|
17
|
+
user_finder_by_id: ->(id) { User.find_by(id:) },
|
18
|
+
user_finder_by_token: ->(token) { User.find_by_token_for(:email_auth, token) },
|
19
|
+
user_initializer: ->(params) { User.new(params[:email]) },
|
20
|
+
user_name_field: "name",
|
21
|
+
token_generator: ->(user) { user.generate_token_for(:email_auth) },
|
22
|
+
token_expiry_minutes: 30,
|
23
|
+
# Controller setup
|
24
|
+
preserve_session_keys_after_logout: [],
|
25
|
+
# View setup
|
26
|
+
layout: "application",
|
27
|
+
register_view: "searls/auth/registrations/show",
|
28
|
+
login_view: "searls/auth/logins/show",
|
29
|
+
verify_view: "searls/auth/verifications/show",
|
30
|
+
mail_layout: "searls/auth/layouts/mailer",
|
31
|
+
mail_login_template_path: "searls/auth/login_link_mailer",
|
32
|
+
mail_login_template_name: "login_link",
|
33
|
+
# Route setup
|
34
|
+
redirect_path_after_register: ->(user, params, request, routes) {
|
35
|
+
# Not every app defines a root_path, so guarding here:
|
36
|
+
routes.respond_to?(:root_path) ? routes.root_path : "/"
|
37
|
+
},
|
38
|
+
default_redirect_path_after_login: ->(user, params, request, routes) {
|
39
|
+
# Not every app defines a root_path, so guarding here:
|
40
|
+
routes.respond_to?(:root_path) ? routes.root_path : "/"
|
41
|
+
},
|
42
|
+
# Hook setup
|
43
|
+
validate_registration: ->(user, params, errors) { errors },
|
44
|
+
after_login_success: nil,
|
45
|
+
# Branding setup
|
46
|
+
app_name: nil,
|
47
|
+
app_url: nil,
|
48
|
+
support_email_address: nil,
|
49
|
+
email_background_color: "#d8d7ed",
|
50
|
+
email_button_color: "#c664f3",
|
51
|
+
email_banner_image_path: nil
|
52
|
+
}.freeze
|
53
|
+
|
54
|
+
CONFIG = Config.new(**DEFAULT_CONFIG)
|
55
|
+
def self.configure(&blk)
|
56
|
+
yield CONFIG
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.config
|
60
|
+
CONFIG.dup.freeze
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: searls-auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Searls
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: railties
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '8.0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '8.0'
|
26
|
+
email:
|
27
|
+
- searls@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- ".standard.yml"
|
33
|
+
- CHANGELOG.md
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- app/controllers/searls/auth/base_controller.rb
|
38
|
+
- app/controllers/searls/auth/logins_controller.rb
|
39
|
+
- app/controllers/searls/auth/registrations_controller.rb
|
40
|
+
- app/controllers/searls/auth/verifications_controller.rb
|
41
|
+
- app/helpers/searls/auth/application_helper.rb
|
42
|
+
- app/javascript/controllers/searls_auth_login_controller.js
|
43
|
+
- app/javascript/controllers/searls_auth_otp_controller.js
|
44
|
+
- app/mailers/searls/auth/base_mailer.rb
|
45
|
+
- app/mailers/searls/auth/login_link_mailer.rb
|
46
|
+
- app/views/searls/auth/layouts/mailer.html.erb
|
47
|
+
- app/views/searls/auth/login_link_mailer/login_link.html.erb
|
48
|
+
- app/views/searls/auth/login_link_mailer/login_link.text.erb
|
49
|
+
- app/views/searls/auth/logins/show.html.erb
|
50
|
+
- app/views/searls/auth/registrations/show.html.erb
|
51
|
+
- app/views/searls/auth/verifications/show.html.erb
|
52
|
+
- config/importmap.rb
|
53
|
+
- config/routes.rb
|
54
|
+
- lib/searls/auth.rb
|
55
|
+
- lib/searls/auth/authenticates_user.rb
|
56
|
+
- lib/searls/auth/config.rb
|
57
|
+
- lib/searls/auth/creates_user.rb
|
58
|
+
- lib/searls/auth/emails_link.rb
|
59
|
+
- lib/searls/auth/engine.rb
|
60
|
+
- lib/searls/auth/railtie.rb
|
61
|
+
- lib/searls/auth/resets_session.rb
|
62
|
+
- lib/searls/auth/version.rb
|
63
|
+
homepage: https://github.com/searls/searls-auth
|
64
|
+
licenses:
|
65
|
+
- GPL-3.0-only
|
66
|
+
metadata:
|
67
|
+
homepage_uri: https://github.com/searls/searls-auth
|
68
|
+
source_code_uri: https://github.com/searls/searls-auth/tree/main
|
69
|
+
changelog_uri: https://github.com/searls/searls-auth/blob/main/CHANGELOG.md
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 3.4.0
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubygems_version: 3.6.7
|
85
|
+
specification_version: 4
|
86
|
+
summary: Searls-flavored login for Rails apps
|
87
|
+
test_files: []
|