Commit af9bf72a authored by Karen Konou's avatar Karen Konou

[Session] add userId to session

parent 37a4d4d1
......@@ -13,6 +13,7 @@ type Username = String
type Password = String
type Token = String
type TreeId = Int
type UserId = Int
newtype AuthRequest = AuthRequest
{ username :: Username
......@@ -41,6 +42,7 @@ derive newtype instance JSON.WriteForeign AuthInvalid
newtype AuthData = AuthData
{ token :: Token
, tree_id :: TreeId
, user_id :: UserId
}
derive instance Generic AuthData _
derive instance Newtype AuthData _
......@@ -50,5 +52,5 @@ derive newtype instance JSON.WriteForeign AuthData
instance Eq AuthData where
eq = genericEq
_AuthData :: Iso' AuthData { token :: Token, tree_id :: TreeId }
_AuthData :: Iso' AuthData { token :: Token, tree_id :: TreeId, user_id :: UserId }
_AuthData = iso (\(AuthData v) -> v) AuthData
......@@ -117,8 +117,8 @@ postAuthRequest backend ar@(AuthRequest {username}) =
decode (Left _err) = Left "Error when sending REST.post"
decode (Right (AuthResponse ar2))
| {inval: Just (AuthInvalid {message})} <- ar2 = Left message
| {valid: Just (AuthData {token, tree_id})} <- ar2 =
Right $ Session { backend, caches: Map.empty, token, treeId: tree_id, username }
| {valid: Just (AuthData {token, tree_id, user_id})} <- ar2 =
Right $ Session { backend, caches: Map.empty, token, treeId: tree_id, username, userId: user_id }
| otherwise = Left "Invalid response from server"
get :: forall a p. JSON.ReadForeign a => ToUrl Session p =>
......
......@@ -6,6 +6,8 @@ module Gargantext.Sessions.Types
, cleanBackendUrl
) where
import Gargantext.Prelude
import Data.Array as A
import Data.Either (Either(..))
import Data.Eq.Generic (genericEq)
......@@ -23,10 +25,9 @@ import Data.Show.Generic (genericShow)
import Data.String as DST
import Data.Tuple (Tuple)
import Foreign.Object as Object
import Gargantext.Components.Login.Types (TreeId)
import Gargantext.Components.Login.Types (TreeId, UserId)
import Gargantext.Components.Nodes.Lists.Types as NT
import Gargantext.Ends (class ToUrl, Backend(..), backendUrl, sessionPath)
import Gargantext.Prelude
import Gargantext.Routes (SessionRoute)
import Gargantext.Types (NodePath, SessionId(..), nodePath)
import Gargantext.Utils.JSON as GJSON
......@@ -43,6 +44,7 @@ newtype Session = Session
, token :: String
, treeId :: TreeId
, username :: String
, userId :: UserId
}
------------------------------------------------------------------------
......@@ -57,8 +59,8 @@ instance JSON.ReadForeign Session where
let rUp = r { caches = Map.fromFoldable (GUT.mapFst (fromMaybe 0 <<< Int.fromString) <$> objTuple) }
pure $ Session rUp
instance JSON.WriteForeign Session where
writeImpl (Session { backend, caches, token, treeId, username }) =
JSON.writeImpl { backend, caches: caches', token, treeId, username }
writeImpl (Session { backend, caches, token, treeId, username, userId}) =
JSON.writeImpl { backend, caches: caches', token, treeId, username, userId }
where
caches' = JSON.writeImpl $ Object.fromFoldable (GUT.mapFst show <$> Map.toUnfoldable caches :: Array (Tuple String NT.CacheState))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment