Skip to content

Commit

Permalink
Merge pull request #1464 from digitallyinduced/basic-master
Browse files Browse the repository at this point in the history
Added DataSync messages for handling login
  • Loading branch information
mpscholten authored May 23, 2022
2 parents af5a4cf + 894f0fa commit 620482b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
7 changes: 5 additions & 2 deletions IHP/AuthSupport/Lockable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ lockDuration = let timeInSecs = 60 * 60 in secondsToNominalDiffTime timeInSecs
isLocked :: forall user. (HasField "lockedAt" user (Maybe UTCTime)) => user -> IO Bool
isLocked user = do
now <- getCurrentTime
let currentLockedAt :: Maybe UTCTime = getField @"lockedAt" user
pure $! case currentLockedAt of
pure (isLocked' now user)

isLocked' :: forall user. (HasField "lockedAt" user (Maybe UTCTime)) => UTCTime -> user -> Bool
isLocked' now user =
case getField @"lockedAt" user of
Just lockedAt ->
let diff = diffUTCTime now lockedAt
in diff < lockDuration
Expand Down
2 changes: 1 addition & 1 deletion IHP/DataSync/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ instance (
run = do
ensureRLSEnabled <- makeCachedEnsureRLSEnabled
installTableChangeTriggers <- ChangeNotifications.makeCachedInstallTableChangeTriggers
runDataSyncController ensureRLSEnabled installTableChangeTriggers (receiveData @ByteString) sendJSON
runDataSyncController ensureRLSEnabled installTableChangeTriggers (receiveData @ByteString) sendJSON (\_ _ -> pure ())
onClose = cleanupAllSubscriptions
6 changes: 3 additions & 3 deletions IHP/DataSync/ControllerImpl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ runDataSyncController ::
, Typeable CurrentUserRecord
, HasNewSessionUrl CurrentUserRecord
, Show (PrimaryKey (GetTableName CurrentUserRecord))
) => _ -> _ -> _ -> _ -> IO ()
runDataSyncController ensureRLSEnabled installTableChangeTriggers receiveData sendJSON = do
) => _ -> _ -> _ -> _ -> _ -> IO ()
runDataSyncController ensureRLSEnabled installTableChangeTriggers receiveData sendJSON handleCustomMessage = do
setState DataSyncReady { subscriptions = HashMap.empty, transactions = HashMap.empty, asyncs = [] }

let pgListener = ?applicationContext |> get #pgListener
Expand Down Expand Up @@ -318,7 +318,7 @@ runDataSyncController ensureRLSEnabled installTableChangeTriggers receiveData se
sendJSON DidCommitTransaction { requestId, transactionId = id }
handleMessage otherwise = handleCustomMessage sendJSON otherwise
forever do
message <- Aeson.eitherDecodeStrict' <$> receiveData
Expand Down
7 changes: 7 additions & 0 deletions IHP/DataSync/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ data DataSyncMessage
| StartTransaction { requestId :: !Int }
| RollbackTransaction { requestId :: !Int, id :: !UUID }
| CommitTransaction { requestId :: !Int, id :: !UUID }
| LoginWithEmailAndPassword { requestId :: !Int, email :: !Text, password :: !Text }
| LoginWithJWT { requestId :: !Int, jwt :: !Text }
deriving (Eq, Show)

data DataSyncResponse
Expand All @@ -45,6 +47,11 @@ data DataSyncResponse
| DidRollbackTransaction { requestId :: !Int, transactionId :: !UUID }
| DidCommitTransaction { requestId :: !Int, transactionId :: !UUID }

| LoginSuccessful { requestId :: !Int, userId :: !UUID, jwt :: !Text }
| UserLocked { requestId :: !Int }
| UserUnconfirmed { requestId :: !Int }
| InvalidCredentials { requestId :: !Int }

data GraphQLResult = GraphQLResult { graphQLResult :: !UndecodedJSON, requestId :: !Int }

data DataSyncTransaction
Expand Down

0 comments on commit 620482b

Please sign in to comment.