A Ruby gem for transferring sessions via QR code in Rails applications.
Add this line to your application's Gemfile:
gem 'session_transfer_qr'
And then execute:
bundle install
Or install it yourself as:
gem install session_transfer_qr
Run the following command to set up the necessary files in your Rails application:
rails generate session_transfer_qr:install
This will generate a controller (app/controllers/session_transfer_controller.rb
) and add the required routes to your config/routes.rb
file.
In your view where you want to display the QR code for session transfer, add the following code:
<%= link_to 'Transfer Session', session_transfer_new_path %>
This will create a link that directs users to the page displaying the QR code.
On the device where you want to transfer the session, scan the QR code displayed on the original device. This will send a POST request to the /session_transfer/create
endpoint with the encoded session data.
The SessionTransferController
will handle the POST request, decode the session data from the QR code, and update the session on the new device.
If the session transfer is successful, the user will be redirected to the root_path
with a success notice. If the QR code is invalid, the user will be redirected to the root_path
with an alert message.
By default, the gem uses your Rails application's secret_key_base
to encode and decode the session data. If you want to use a different secret key, you can set it in an initializer:
# config/initializers/session_transfer_qr.rb
SessionTransferQR::Transfer::SECRET_KEY = 'your_custom_secret_key'
The gem is available as open source under the terms of the MIT License.