Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple serializers from Devise modules. You can use any serializer …
…at any time, as long the API required by the serializer is satisfied. Since session serializer is encrypted, it simply stores the id and the resource klass. Cookie is not encrypted, so it creates a random token and store it in the database, so in order to do the token cleanup, you need provide remember_me! and forget_me! methods, as in :rememberable.
- Loading branch information
Showing
10 changed files
with
95 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'devise/serializers/session' | ||
|
||
module Devise | ||
module Models | ||
module SessionSerializer | ||
# Hook to serialize user into session. Overwrite if you want. | ||
def serialize_into_session(record) | ||
[record.class, record.id] | ||
end | ||
|
||
# Hook to serialize user from session. Overwrite if you want. | ||
def serialize_from_session(keys) | ||
klass, id = keys | ||
raise "#{self} cannot serialize from #{klass} session since it's not one of its ancestors" unless klass <= self | ||
klass.find(:first, :conditions => { :id => id }) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'devise/serializers/base' | ||
|
||
module Devise | ||
module Serializers | ||
# This is a cookie serializer which stores the information if a :remember_me | ||
# is sent in the params and if the model responds to remember_me! as well. | ||
# As in Session serializer, the invoked methods are: | ||
# | ||
# User.serialize_into_cookie(@user) | ||
# User.serialize_from_cookie(*args) | ||
# | ||
# An implementation for such methods can be found at Devise::Models::Rememberable. | ||
# | ||
# Differently from session, this approach is based in a token which is stored in | ||
# the database. So if you want to sign out all clients at once, you just need to | ||
# clean up the token column. | ||
# | ||
class Cookie < Warden::Serializers::Cookie | ||
include Devise::Serializers::Base | ||
|
||
def store(record, scope) | ||
remember_me = params[scope].try(:fetch, :remember_me, nil) | ||
if Devise::TRUE_VALUES.include?(remember_me) && record.respond_to?(:remember_me!) | ||
record.remember_me! | ||
super | ||
end | ||
end | ||
|
||
def default_options(record) | ||
super.merge!(:expires => record.remember_expires_at) | ||
end | ||
|
||
def delete(scope, record=nil) | ||
if record && record.respond_to?(:forget_me!) | ||
record.forget_me! | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
Warden::Serializers.add(:cookie, Devise::Serializers::Cookie) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'devise/serializers/base' | ||
|
||
module Devise | ||
module Serializers | ||
# This serializer stores sign in information in th client session. It just | ||
# extends Warden own serializer to move all the serialization logic to a | ||
# class. For example, if a @user resource is given, it will call the following | ||
# two methods to serialize and deserialize a record: | ||
# | ||
# User.serialize_into_session(@user) | ||
# User.serialize_from_session(*args) | ||
# | ||
# This can be used any strategy and the default implementation is available | ||
# at Devise::Models::SessionSerializer. | ||
# | ||
class Session < Warden::Serializers::Session | ||
include Devise::Serializers::Base | ||
end | ||
end | ||
end | ||
|
||
Warden::Serializers.add(:session, Devise::Serializers::Session) |