Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for encoding extra information in magic token #27

Closed
wants to merge 6 commits into from

Conversation

elucid
Copy link
Contributor

@elucid elucid commented May 9, 2023

Hey there. We have some use cases where we want to store extra information in the token. i.e. an id of a resource the user can claim by clicking on the URL. I wanted to extend the API so that this information can be optionally passed along, and then subsequently made available somewhere on the request env upon successful decode.

Here is an example of using it, adapting the template generated controller code:

class Devise::Passwordless::MagicLinksController < DeviseController
  prepend_before_action :require_no_authentication, only: :show
  prepend_before_action :allow_params_authentication!, only: :show
  prepend_before_action(only: [:show]) { request.env['devise.skip_timeout'] = true }

  def show
    self.resource = warden.authenticate!(auth_options)
    set_flash_message!(:notice, :signed_in)
    sign_in(resource_name, resource)
    yield resource if block_given?

    # everything up to here is the standard code. the following is new:
    extra = request.env['warden.magic_link_extra']
    process_magic_link_extra(extra) if extra
  end

@elucid
Copy link
Contributor Author

elucid commented May 12, 2023

@abevoelker the fails look like container hiccups rather than code issues.

@abevoelker
Copy link
Owner

abevoelker commented May 12, 2023 via email

@elucid
Copy link
Contributor Author

elucid commented May 12, 2023

That's great. Thanks as well.

elucid and others added 3 commits May 16, 2023 12:13
Co-authored-by: Jim Kane <jim@util.is>
Co-authored-by: Jim Kane <jim@util.is>
Co-authored-by: Jim Kane <jim@util.is>
@fastjames
Copy link
Contributor

I'm currently using this feature in a local fork, and it works great. Thanks for taking the time to share it!

@dansteele
Copy link

Hey @elucid, this looks good! Can I ask how you're using it in your application? I'm not that familiar with the insides of Warden and Devise. Are you directly adding to the request.env from the controller that called authenticate_user!? Or is there another API that's better?

Cheers

@abevoelker
Copy link
Owner

Thanks for your thoughtful contribution @elucid! This has been merged into a new branch I'm working on for a 1.0.0 release. 🎉

@abevoelker abevoelker closed this Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants