Commit af9bf72a authored by Karen Konou's avatar Karen Konou

[Session] add userId to session

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