Permalink
Browse files

Fix bug in "remember me" functionality

  • Loading branch information...
1 parent 7f2cf2a commit 540962ba68003b0ab19907eb8c84e6b4e9f9800a @ozataman ozataman committed Sep 15, 2011
Showing with 13 additions and 9 deletions.
  1. +9 −5 src/Snap/Snaplet/Auth.hs
  2. +4 −4 src/Snap/Snaplet/Auth/Backends/JsonFile.hs
@@ -55,7 +55,7 @@ import qualified Data.ByteString.Char8 as B
import Data.ByteString (ByteString)
import Data.Maybe (isJust)
import Data.Time
-import Data.Text.Encoding (decodeUtf8)
+import Data.Text.Encoding (encodeUtf8, decodeUtf8)
import Data.Text (Text)
import Snap.Core
@@ -106,10 +106,14 @@ loginByUsername unm pwd rm = do
case res of
Left e -> return $ Left e
Right au'' -> do
- when rm $ do
- token <- liftIO $ randomToken 64
- setRememberToken sk cn rp token
- return $ Right au''
+ case rm of
+ True -> do
+ token <- liftIO $ randomToken 64
+ setRememberToken sk cn rp token
+ let au''' = au'' { userRememberToken = Just (decodeUtf8 token) }
+ saveUser au'''
+ return $ Right au'''
+ False -> return $ Right au''
------------------------------------------------------------------------------
@@ -200,12 +200,12 @@ instance IAuthBackend JsonFileAuthManager where
let lc = if oldLogin /= userLogin u
then HM.insert newLogin uid . HM.delete oldLogin $ loginCache cache
else loginCache cache
- let tc = if oldToken /= userRememberToken u && isJust oldToken
+ let tc = if oldToken /= newToken && isJust oldToken
then HM.delete (fromJust oldToken) $ loginCache cache
else tokenCache cache
- let tc' = if isJust newToken
- then HM.insert (fromJust newToken) uid tc
- else tc
+ let tc' = case newToken of
+ Just t -> HM.insert t uid tc
+ Nothing -> tc
let u' = u { userUpdatedAt = Just now }
let new = cache {
uidCache = HM.insert uid u' $ uidCache cache

0 comments on commit 540962b

Please sign in to comment.