-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70d3152
commit b680675
Showing
8 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
engines/bops_core/app/controllers/concerns/bops_core/magic_link_authenticatable.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module MagicLinkAuthenticatable | ||
extend ActiveSupport::Concern | ||
|
||
SGID_FOR = "magic_link" | ||
|
||
included do | ||
rescue_from ActionController::ParameterMissing do |exception| | ||
render plain: "Not Found", status: :not_found | ||
end | ||
end | ||
|
||
def magic_link | ||
resource = GlobalID::Locator.locate_signed(sgid, for: SGID_FOR) | ||
|
||
if resource.nil? | ||
redirect_failed | ||
return | ||
end | ||
|
||
sign_in(resource) | ||
redirect_to after_magic_link_path_for(resource), notice: "Welcome!" | ||
end | ||
|
||
private | ||
|
||
def sgid | ||
params.require(:sgid) | ||
end | ||
|
||
def redirect_failed | ||
render plain: "Forbidden", status: :forbidden | ||
end | ||
|
||
def after_magic_link_path_for(resource) | ||
if resource.is_a?(Consultee) | ||
bops_consultees.dashboard_path | ||
else | ||
main_app.root_path | ||
end | ||
end | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
engines/bops_core/app/mailers/concerns/bops_core/magic_link_mailer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
class MagicLinkMailer < ApplicationMailer | ||
def magic_link_mail(resource:, subdomain:, subject: "Your magic link") | ||
@resource = resource | ||
@magic_link = resource.magic_link | ||
@subdomain = subdomain | ||
|
||
mail( | ||
to: resource.email_address, | ||
subject: | ||
) | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
engines/bops_core/app/models/concerns/bops_core/magic_linkable.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module MagicLinkable | ||
extend ActiveSupport::Concern | ||
include GlobalID::Identification | ||
|
||
def magic_link(expires_in: 1.day, for: "magic_link") | ||
to_sgid(expires_in:, for:).to_s | ||
end | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
engines/bops_core/app/views/bops_core/magic_link_mailer/magic_link_mail.text.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Hello <%= @resource.name %> | ||
|
||
Click the link below to access your dashboard: | ||
|
||
<%= link_to "Access Dashboard", bops_consultees.magic_link_url(sgid: @magic_link, subdomain: @subdomain) %> | ||
|
||
This link will expire in 24 hours. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
BopsCore::Engine.routes.draw do | ||
get "magic_link", to: "magic_links#show", as: :magic_link | ||
end |