blob: 06aba5a22259a413dc0dec68e708272de5409321 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
module Model.SignIn
( createSignInToken
, getSignIn
, signInTokenToUsed
, isLastValidToken
) where
import Data.Text (Text)
import Data.Time.Clock (getCurrentTime)
import Control.Monad.IO.Class (liftIO)
import Database.Persist
import Model.Database
import Model.UUID (generateUUID)
createSignInToken :: Text -> Persist Text
createSignInToken email = do
now <- liftIO getCurrentTime
token <- liftIO generateUUID
_ <- insert $ SignIn token now email False
return token
getSignIn :: Text -> Persist (Maybe (Entity SignIn))
getSignIn token =
selectFirst [SignInToken ==. token] []
signInTokenToUsed :: SignInId -> Persist ()
signInTokenToUsed tokenId =
update tokenId [SignInIsUsed =. True]
isLastValidToken :: SignIn -> Persist Bool
isLastValidToken signIn = do
maybe False ((== (signInToken signIn)) . signInToken . entityVal) <$>
selectFirst
[ SignInEmail ==. (signInEmail signIn)
, SignInIsUsed ==. True
]
[ Desc SignInCreation ]
|