I'm using omniauth-github gem and I noticed that the user is kept in a session cookie:
SessionsController:
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
...
end
Do you know an easy way to persist the session after the browser is closed?
I know it could be achieved with an integration with Devise: https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview ...but I'm hoping for a simpler solution.
Thanks
Solution:
A 'token' column is added to the User model and then:
class User < ActiveRecord::Base
before_save :generate_token
def generate_token
self.token = SecureRandom.urlsafe_base64
end
end
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
cookies.permanent[:token] = user.token
end
end
class ApplicationController < ActionController::Base
def current_user
@current_user ||= User.find_by_token(cookies[:token]) if cookies[:token]
end
end