How to use a custom confirmations URL in Devise

564 Views Asked by At

I'm looking for a way to use a URL provided as a parameter in the POST request to create a user as the confirmation URL for confirming my Devise user model. The use case is that I have a rails application acting as an API for a frontend react app (setup using webpack) and I want the URL in the confirmations instructions email sent to the user to link to the frontend app, which would then send an API request to the backend to manually confirm the user, instead of the user visiting my backend API URLs themselves.

I want it so that the backend could potentially be used with other clients in the future, so the URL to be used must be variable. Currently I'm trying to pass it in the API request to create the user, and then somehow I would need a way to get this variable into the Devise mailer.

The end goal looks something like this:

# app/views/devise/mailer/confirmation_instructions.html.erb
<p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', @custom_confirmation_url ?  "#{@custom_confirmation_url}#{@token}" : confirmation_url(@resource, confirmation_token: @token) %></p>

where @custom_confirmation_url is the variable holding the value of the URL passed into the POST request to register a new user.

I was potentially thinking of simply redirecting the user to my frontend after the confirmation, with something like this but I don't see how I'd make it work with multiple frontend clients, if I went that route.

I'm open to thoughts on whether or not this approach is even worth considering, or if there's a more stylistically correct way of accomplishing this.

0

There are 0 best solutions below