Reputation: 310
How can I store a session token in database on login? Everytime user login user's session token should get updated and when it is about to expire it should get updated to. I saw in rails guide that using rails g active_record:session_migration
will create table and that will store all the sessions. Then YourApp::Application.config.session_store :active_record_store
. How session will get store in this table?? do i need to write a logic for that? Please help.
Thanks
Upvotes: 2
Views: 2200
Reputation: 1793
Full details are on the gem's readme:
https://github.com/rails/activerecord-session_store
You may configure the table name, primary key, and data column. For example, at the end of config/application.rb:
ActiveRecord::SessionStore::Session.table_name = 'legacy_session_table'
ActiveRecord::SessionStore::Session.primary_key = 'session_id'
ActiveRecord::SessionStore::Session.data_column_name = 'legacy_session_data'
Note that setting the primary key to the session_id frees you from having a separate id column if you don't want it. However, you must set session.model.id = session.session_id by hand! A before filter on ApplicationController is a good place.
Since the default class is a simple Active Record, you get timestamps for free if you add created_at and updated_at datetime columns to the sessions table, making periodic session expiration a snap.
You may provide your own session class implementation, whether a feature-packed Active Record or a bare-metal high-performance SQL store, by setting
ActionDispatch::Session::ActiveRecordStore.session_class = MySessionClass
You must implement these methods:
self.find_by_session_id(session_id)
initialize(hash_of_session_id_and_data, options_hash = {})
attr_reader :session_id
attr_accessor :data
save
destroy
The example SqlBypass class is a generic SQL session store. You may use it as a basis for high-performance database-specific stores.
Upvotes: 1