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