Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
purescript-gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grégoire Locqueville
purescript-gargantext
Commits
8790f219
Commit
8790f219
authored
4 years ago
by
Przemyslaw Kaminski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[REST] set cookie based on token
parent
aec09ff0
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
11 deletions
+29
-11
REST.purs
src/Gargantext/Config/REST.purs
+19
-11
Reactix.js
src/Gargantext/Utils/Reactix.js
+5
-0
Reactix.purs
src/Gargantext/Utils/Reactix.purs
+5
-0
No files found.
src/Gargantext/Config/REST.purs
View file @
8790f219
...
...
@@ -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
, A
RH.A
ccept 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
, A
RH.A
ccept 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
, A
RH.A
ccept applicationJSON
] <>
foldMap (\token ->
[ RequestHeader "Authorization" $ "Bearer " <> token ]
[
ARH.
RequestHeader "Authorization" $ "Bearer " <> token ]
) mtoken
, content = Just $ formData fd
}
...
...
This diff is collapsed.
Click to expand it.
src/Gargantext/Utils/Reactix.js
View file @
8790f219
...
...
@@ -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
;
This diff is collapsed.
Click to expand it.
src/Gargantext/Utils/Reactix.purs
View file @
8790f219
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment