Hey folks, I have this working code that I would love to get right. I feel like I could have done all this with a something much simpler.
The purpose is to get back a jwtToken and pass to another function. Here a auth client, obviously. Any feedback would be appreciated.
type idTokenT = {
idToken,
} and idToken = {jwtToken: string};
module Decode = {
type t = idTokenT;
let decodeJwt = json => Json.Decode.{jwtToken: json |> field("jwtToken",string)}
let decodeToken = json => Json.Decode.{
idToken: json |> field("idToken", decodeJwt)
};
let decodeResponse = json => decodeToken(json);
let response = (json: string) : Belt.Result.t(t, string) =>
try (json |> Json.parseOrRaise |> decodeResponse |> (u => Belt.Result.Ok(u))) {
| Json.Decode.DecodeError(decodeError) => Belt.Result.Error(decodeError)
| e => Belt.Result.Error(e |> Js.String.make)
};
};
type auth;
[@bs.module "@aws-amplify/auth/lib"] external auth: auth = "default";
[@bs.send] external getCurrentSession:(auth,unit)=> Js.Promise.t('a) = "currentSession";
[@genType]
let getJwtToken = () => {
open PromiseMonad;
getCurrentSession(auth,())
>>= (response => {
let jwtToken = Decode.response(response);
Js.log(jwtToken);
return(jwtToken);
})
// >>| (err => {
// let errMsg = err |> Js.Json.stringifyAny |> Js.Option.getWithDefault("");
// Js.log2("Failure!!", err);
// return(err)
// });
};