Commit 8790f219 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[REST] set cookie based on token

parent aec09ff0
...@@ -2,10 +2,11 @@ module Gargantext.Config.REST where ...@@ -2,10 +2,11 @@ module Gargantext.Config.REST where
import Affjax (defaultRequest, printResponseFormatError, request) import Affjax (defaultRequest, printResponseFormatError, request)
import Affjax.RequestBody (RequestBody(..), formData, formURLEncoded, string) import Affjax.RequestBody (RequestBody(..), formData, formURLEncoded, string)
import Affjax.RequestHeader (RequestHeader(..)) import Affjax.RequestHeader as ARH
import Affjax.ResponseFormat as ResponseFormat import Affjax.ResponseFormat as ResponseFormat
import DOM.Simple.Console (log) import DOM.Simple.Console (log)
import Data.Argonaut (class DecodeJson, decodeJson, class EncodeJson, encodeJson) import Data.Argonaut (class DecodeJson, decodeJson, class EncodeJson, encodeJson)
import Data.Array as A
import Data.Either (Either(..)) import Data.Either (Either(..))
import Data.Foldable (foldMap) import Data.Foldable (foldMap)
import Data.FormURLEncoded as FormURLEncoded import Data.FormURLEncoded as FormURLEncoded
...@@ -16,9 +17,11 @@ import Data.Tuple (Tuple(..)) ...@@ -16,9 +17,11 @@ import Data.Tuple (Tuple(..))
import Effect.Aff (Aff, throwError) import Effect.Aff (Aff, throwError)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Effect.Exception (error) import Effect.Exception (error)
import Prelude (Unit, bind, pure, ($), (<$>), (<<<), (<>))
import Web.XHR.FormData as XHRFormData import Web.XHR.FormData as XHRFormData
import Gargantext.Prelude
import Gargantext.Utils.Reactix as R2
type Token = String type Token = String
-- TODO too much duplicate code in `postWwwUrlencoded` -- TODO too much duplicate code in `postWwwUrlencoded`
...@@ -29,14 +32,19 @@ send m mtoken url reqbody = do ...@@ -29,14 +32,19 @@ send m mtoken url reqbody = do
{ url = url { url = url
, responseFormat = ResponseFormat.json , responseFormat = ResponseFormat.json
, method = Left m , method = Left m
, headers = [ ContentType applicationJSON , headers = [ ARH.ContentType applicationJSON
, Accept applicationJSON , ARH.Accept applicationJSON
] <> ] <>
foldMap (\token -> foldMap (\token ->
[RequestHeader "Authorization" $ "Bearer " <> token] [ARH.RequestHeader "Authorization" $ "Bearer " <> token]
) mtoken ) mtoken
, content = (Json <<< encodeJson) <$> reqbody , content = (Json <<< encodeJson) <$> reqbody
} }
case mtoken of
Nothing -> pure unit
Just token -> liftEffect $ do
let cookie = "JWT-Cookie=" <> token <> "; Path=/;" --" HttpOnly; Secure; SameSite=Lax"
R2.setCookie cookie
case affResp.body of case affResp.body of
Left err -> do Left err -> do
_ <- liftEffect $ log $ printResponseFormatError err _ <- liftEffect $ log $ printResponseFormatError err
...@@ -78,11 +86,11 @@ postWwwUrlencoded mtoken url bodyParams = do ...@@ -78,11 +86,11 @@ postWwwUrlencoded mtoken url bodyParams = do
{ url = url { url = url
, responseFormat = ResponseFormat.json , responseFormat = ResponseFormat.json
, method = Left POST , method = Left POST
, headers = [ ContentType applicationFormURLEncoded , headers = [ ARH.ContentType applicationFormURLEncoded
, Accept applicationJSON , ARH.Accept applicationJSON
] <> ] <>
foldMap (\token -> foldMap (\token ->
[RequestHeader "Authorization" $ "Bearer " <> token] [ARH.RequestHeader "Authorization" $ "Bearer " <> token]
) mtoken ) mtoken
, content = Just $ formURLEncoded urlEncodedBody , content = Just $ formURLEncoded urlEncodedBody
} }
...@@ -108,11 +116,11 @@ postMultipartFormData mtoken url body = do ...@@ -108,11 +116,11 @@ postMultipartFormData mtoken url body = do
{ url = url { url = url
, responseFormat = ResponseFormat.json , responseFormat = ResponseFormat.json
, method = Left POST , method = Left POST
, headers = [ ContentType multipartFormData , headers = [ ARH.ContentType multipartFormData
, Accept applicationJSON , ARH.Accept applicationJSON
] <> ] <>
foldMap (\token -> foldMap (\token ->
[ RequestHeader "Authorization" $ "Bearer " <> token ] [ ARH.RequestHeader "Authorization" $ "Bearer " <> token ]
) mtoken ) mtoken
, content = Just $ formData fd , content = Just $ formData fd
} }
......
...@@ -15,6 +15,11 @@ function stringify(j, indent) { ...@@ -15,6 +15,11 @@ function stringify(j, indent) {
return JSON.stringify(j, null, indent); return JSON.stringify(j, null, indent);
} }
function setCookie(c) {
document.cookie = c;
}
exports._addRootElement = addRootElement; exports._addRootElement = addRootElement;
exports._getSelection = getSelection; exports._getSelection = getSelection;
exports._stringify = stringify; exports._stringify = stringify;
exports._setCookie = setCookie;
...@@ -283,3 +283,8 @@ useLocalStorageState key s = do ...@@ -283,3 +283,8 @@ useLocalStorageState key s = do
setItem key json storage setItem key json storage
pure (Tuple state setState) pure (Tuple state setState)
foreign import _setCookie :: EffectFn1 String Unit
setCookie :: String -> Effect Unit
setCookie = runEffectFn1 _setCookie
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