Skip to content

Commit

Permalink
Allow setting the session token manually
Browse files Browse the repository at this point in the history
  • Loading branch information
mikker committed Jun 17, 2023
1 parent 7189a25 commit 30eac00
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 9 additions & 4 deletions app/models/passwordless/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ class Session < ApplicationRecord

before_validation :set_defaults

# save the token in memory so we can put it in emails but only save the
# hashed version in the database
attr_accessor :token

scope(
:available,
lambda { where("expires_at > ?", Time.current) }
)

# save the token in memory so we can put it in emails but only save the
# hashed version in the database
attr_reader :token

def token=(plaintext)
self.token_digest = Passwordless.digest(plaintext)
@token = (plaintext)
end

def expired?
expires_at <= Time.current
end
Expand Down
6 changes: 6 additions & 0 deletions test/models/passwordless/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def call(_session)
Passwordless.token_generator = old_generator
end

test("setting token manually") do
session = Session.new(token: "hi")
assert_equal "hi", session.token
assert_equal Passwordless.digest("hi"), session.token_digest
end

test("with a custom expire at function") do
custom_expire_at = Time.parse("01-01-2100").utc
old_expires_at = Passwordless.expires_at
Expand Down

0 comments on commit 30eac00

Please sign in to comment.