action_auth 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24c21d0f3f0275043c101a941be6587f6ab14dcd56be5951e410a15c66d2ce59
4
- data.tar.gz: 0406533a71d0411f164c20dc805e1f9e43988ffd7d979dc9ece4f8a20050bf30
3
+ metadata.gz: 6c85ae94ede51ba040295cfca1ec2f6a46f412642bc3e3a3ada8fd102bec41ab
4
+ data.tar.gz: bf218c5419f6cf1a7f8eb5b70f0cbb58f9af1710be3222ee0a333075af32e5c5
5
5
  SHA512:
6
- metadata.gz: 415a5088415bdc54b813b8a199486e733442b6346ec9ba30b2995bf9428983478aa86e8406adb2cf18bf7271669dad2f0de50f9658dbe0627d4b61b6423ee66f
7
- data.tar.gz: 795b6cc968062e90034ddfb08ad4f251a69b7bd5a50b580d706f98fa99c71276da291ef0bce15964153593a05aef4e5f3b171d2e5e980928754d6800b2324dea
6
+ metadata.gz: e09cef2c34868ff6e6bd0e4d81f6e5fa577d91c0b38d957f09b36aa48a2cf4a183b2ff0bc400c321eb03829aef23c3976188d108b3e9a2f6f887e2e3a86f7043
7
+ data.tar.gz: c3cce12a87a5bfdc1b785ed01cb2cbed07325f9e65150e4fe5e5a7b86c6f6a52f5357aad6d51510a9e87a92f389441b268462917f0ea619980d6d130bd681ad6
data/README.md CHANGED
@@ -102,6 +102,7 @@ ActionAuth.configure do |config|
102
102
  config.webauthn_origin = "http://localhost:3000" # or "https://example.com"
103
103
  config.webauthn_rp_name = Rails.application.class.to_s.deconstantize
104
104
  config.verify_email_on_sign_in = true
105
+ config.magic_link_enabled = true
105
106
  config.default_from_email = "from@example.com"
106
107
  end
107
108
  ```
@@ -124,7 +125,7 @@ These are the planned features for ActionAuth. The ones that are checked off are
124
125
 
125
126
  ✅ - Passkeys/Hardware Security Keys
126
127
 
127
- - Magic Links
128
+ - Magic Links
128
129
 
129
130
  ⏳ - OAuth with Google, Facebook, Github, Twitter, etc.
130
131
 
@@ -272,7 +273,7 @@ We can set the user to become a User record instead of an ActionAuth::User recor
272
273
  class Current < ActiveSupport::CurrentAttributes
273
274
  def user
274
275
  return unless ActionAuth::Current.user
275
- ActionAuth::Current.user.becomes(User)
276
+ ActionAuth::Current.user&.becomes(User)
276
277
  end
277
278
  end
278
279
  ```
@@ -0,0 +1,20 @@
1
+ module ActionAuth
2
+ class Magics::RequestsController < ApplicationController
3
+ def new
4
+ end
5
+
6
+ def create
7
+ user = User.find_or_initialize_by(email: params[:email])
8
+ if user.new_record?
9
+ password = SecureRandom.hex(32)
10
+ user.password = password
11
+ user.password_confirmation = password
12
+ user.save!
13
+ end
14
+
15
+ UserMailer.with(user: user).magic_link.deliver_later
16
+
17
+ redirect_to sign_in_path, notice: "Check your email for a magic link."
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module ActionAuth
2
+ class Magics::SignInsController < ApplicationController
3
+ def show
4
+ user = ActionAuth::User.find_by_token_for(:magic_token, params[:token])
5
+ if user
6
+ @session = user.sessions.create
7
+ cookies.signed.permanent[:session_token] = { value: @session.id, httponly: true }
8
+ user.update(verified: true)
9
+ redirect_to main_app.root_path, notice: "Signed In"
10
+ else
11
+ redirect_to sign_in_path, alert: "Authentication failed, please try again."
12
+ end
13
+ end
14
+ end
15
+ end
@@ -13,5 +13,12 @@ module ActionAuth
13
13
 
14
14
  mail to: @user.email, subject: "Verify your email"
15
15
  end
16
+
17
+ def magic_link
18
+ @user = params[:user]
19
+ @signed_id = @user.generate_token_for(:magic_token)
20
+
21
+ mail to: @user.email, subject: "Sign in to your account"
22
+ end
16
23
  end
17
24
  end
@@ -20,6 +20,10 @@ module ActionAuth
20
20
  password_salt.last(10)
21
21
  end
22
22
 
23
+ generates_token_for :magic_token, expires_in: 20.minutes do
24
+ password_salt.last(10)
25
+ end
26
+
23
27
  validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
24
28
  validates :password, allow_nil: true, length: { minimum: 12 }
25
29
 
@@ -0,0 +1,21 @@
1
+ <h1>Sign up</h1>
2
+
3
+ <%= form_with(url: magics_requests_path) do |form| %>
4
+ <div class="mb-3">
5
+ <%= form.label :email, style: "display: block" %>
6
+ <%= form.email_field :email, required: true, autofocus: true, autocomplete: "email" %>
7
+ </div>
8
+
9
+ <div class="mb-3">
10
+ <%= form.submit "Request Magic Link", class: "btn btn-primary" %>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="mb-3">
15
+ <%= link_to "Sign In", sign_in_path %> |
16
+ <%= link_to "Sign Up", sign_up_path %> |
17
+ <%= link_to "Reset Password", new_identity_password_reset_path %>
18
+ <% if ActionAuth.configuration.verify_email_on_sign_in %>
19
+ | <%= link_to "Verify Email", identity_email_verification_path %>
20
+ <% end %>
21
+ </div>
@@ -36,6 +36,9 @@
36
36
 
37
37
  <div class="mb-3">
38
38
  <%= link_to "Sign In", sign_in_path %> |
39
+ <% if ActionAuth.configuration.magic_link_enabled? %>
40
+ <%= link_to "Magic Link", new_magics_requests_path %> |
41
+ <% end %>
39
42
  <%= link_to "Reset Password", new_identity_password_reset_path %>
40
43
  <% if ActionAuth.configuration.verify_email_on_sign_in %>
41
44
  | <%= link_to "Verify Email", identity_email_verification_path %>
@@ -21,6 +21,9 @@
21
21
 
22
22
  <div class="mb-3">
23
23
  <%= link_to "Sign Up", sign_up_path %> |
24
+ <% if ActionAuth.configuration.magic_link_enabled? %>
25
+ <%= link_to "Magic Link", new_magics_requests_path %> |
26
+ <% end %>
24
27
  <%= link_to "Reset Password", new_identity_password_reset_path %>
25
28
  <% if ActionAuth.configuration.verify_email_on_sign_in %>
26
29
  | <%= link_to "Verify Email", identity_email_verification_path %>
@@ -0,0 +1,3 @@
1
+ <p>
2
+ Use this <%= link_to "link", magics_sign_ins_url(token: @signed_id) %> to sign in.
3
+ </p>
data/config/routes.rb CHANGED
@@ -18,4 +18,11 @@ ActionAuth::Engine.routes.draw do
18
18
 
19
19
  resource :webauthn_credential_authentications, only: [:new, :create]
20
20
  end
21
+
22
+ if ActionAuth.configuration.magic_link_enabled?
23
+ namespace :magics do
24
+ resource :sign_ins, only: [:show]
25
+ resource :requests, only: [:new, :create]
26
+ end
27
+ end
21
28
  end
@@ -5,6 +5,7 @@ module ActionAuth
5
5
  attr_accessor :webauthn_origin
6
6
  attr_accessor :webauthn_rp_name
7
7
  attr_accessor :verify_email_on_sign_in
8
+ attr_accessor :magic_link_enabled
8
9
  attr_accessor :default_from_email
9
10
 
10
11
  def initialize
@@ -12,6 +13,7 @@ module ActionAuth
12
13
  @webauthn_origin = "http://localhost:3000"
13
14
  @webauthn_rp_name = Rails.application.class.to_s.deconstantize
14
15
  @verify_email_on_sign_in = true
16
+ @magic_link_enabled = true
15
17
  @default_from_email = "from@example.com"
16
18
  end
17
19
 
@@ -19,5 +21,9 @@ module ActionAuth
19
21
  @webauthn_enabled.respond_to?(:call) ? @webauthn_enabled.call : @webauthn_enabled
20
22
  end
21
23
 
24
+ def magic_link_enabled?
25
+ @magic_link_enabled.respond_to?(:call) ? @magic_link_enabled.call : @magic_link_enabled
26
+ end
27
+
22
28
  end
23
29
  end
@@ -1,3 +1,3 @@
1
1
  module ActionAuth
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dave Kimura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-06 00:00:00.000000000 Z
11
+ date: 2024-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -56,6 +56,8 @@ files:
56
56
  - app/controllers/action_auth/identity/email_verifications_controller.rb
57
57
  - app/controllers/action_auth/identity/emails_controller.rb
58
58
  - app/controllers/action_auth/identity/password_resets_controller.rb
59
+ - app/controllers/action_auth/magics/requests_controller.rb
60
+ - app/controllers/action_auth/magics/sign_ins_controller.rb
59
61
  - app/controllers/action_auth/passwords_controller.rb
60
62
  - app/controllers/action_auth/registrations_controller.rb
61
63
  - app/controllers/action_auth/sessions_controller.rb
@@ -73,12 +75,14 @@ files:
73
75
  - app/views/action_auth/identity/emails/edit.html.erb
74
76
  - app/views/action_auth/identity/password_resets/edit.html.erb
75
77
  - app/views/action_auth/identity/password_resets/new.html.erb
78
+ - app/views/action_auth/magics/requests/new.html.erb
76
79
  - app/views/action_auth/passwords/edit.html.erb
77
80
  - app/views/action_auth/registrations/new.html.erb
78
81
  - app/views/action_auth/sessions/index.html.erb
79
82
  - app/views/action_auth/sessions/new.html.erb
80
83
  - app/views/action_auth/user_mailer/email_verification.html.erb
81
84
  - app/views/action_auth/user_mailer/email_verification.text.erb
85
+ - app/views/action_auth/user_mailer/magic_link.html.erb
82
86
  - app/views/action_auth/user_mailer/password_reset.html.erb
83
87
  - app/views/action_auth/user_mailer/password_reset.text.erb
84
88
  - app/views/action_auth/webauthn_credential_authentications/new.html.erb