Skip to content

Instantly share code, notes, and snippets.

@idkjs idkjs/Amplify.re Secret
Last active Feb 19, 2019

Embed
What would you like to do?
type t;
[@bs.module "aws-amplify"] external amplify: t = "default";
module Configure = {
let config = Config.ampconfig;
[@bs.send] external configure: (t, Config.c) => unit = "configure";
let configure = configure(amplify, config);
};
let configure = () => Configure.configure;
module CognitoUserSession = {
type t = {
.
"accessToken": string, /* I'm guessing these are all strings */
"idToken": idToken,
"refreshToken": token,
}
and idToken = {. "jwtToken": string}
and token = {. "token": string};
};
module Auth = {
[@bs.module "aws-amplify"] [@bs.scope "Auth"]
// external currentSession: unit => Js.Promise.t(CognitoUserSession.t) = "";
external currentSession: unit => Js.Promise.t(Js.Json.t) = "";
external currentCredentials: unit => Js.Promise.t(Js.Json.t) = "";
};
module Decode = {
let decodeJwt = json =>
Json.Decode.{"jwtToken": json |> field("jwtToken", string)};
let decodeToken = json =>
Json.Decode.{"token": json |> field("token", string)};
let decodeSession = json =>
Json.Decode.{
"idToken": json |> field("idToken", decodeJwt),
"accessToken": json |> field("accessToken", decodeJwt),
"refreshToken": json |> field("refreshToken", decodeToken),
};
};
[@genType]
let getJwtToken = () => {
PromiseMonad.(
Auth.currentSession()
>>= (
json => {
Js.log2("RESPONSE_VALUE_TOKEN_AMPLIFY", json);
let session = Decode.decodeSession(json);
let jwtToken = session##idToken##jwtToken;
// Js.log2("RESPONSE_VALUE_jwtToken_AMPLIFY", jwtToken);
return(jwtToken);
}
)
);
};
type t;
type jwtToken;
type auth = {
.
"region": string,
"url": string,
"auth" : {.
"type_": string,
"jwtToken": string,
}
};
[@bs.module "aws-appsync"] [@bs.new] external makeAWSAppSyncClient: auth => t = "AWSAppSyncClient";
external toString: 'a => string = "%identity";
let config = Config.authconfig;
let client = () => {
// Js.log(Amplify.getJwtToken);
let token = Amplify.getJwtToken()
|> Js.Promise.then_(data =>
data |> Js.log |> Js.Promise.resolve)
|> Js.Promise.catch(error =>
error |> Js.log |> Js.Promise.resolve);
let clientOptions = {
"url":config##url,
"region":config##region,
"auth": {
"type_":config##authType,
"jwtToken":toString(token)
},
};
makeAWSAppSyncClient(clientOptions);
};
// We've found a bug for you!
// /Users/prisc_000/working/CHATAPPS/chat-js-to-re/packages/aws-appsync-chat-re/src/aws/aws/Amplify.re 55:9-25
// 53 ┆ // Js.log2("RESPONSE_VALUE_jwtToken_AMPLIFY", jwtToken);
// 54 ┆
// 55 ┆ return(jwtToken);
// 56 ┆ }
// 57 ┆ )
// This has type:
// Js.Promise.t(CognitoUserSession.t) (defined as
// Js.Promise.t(CognitoUserSession.t))
// But somewhere wanted:
// CognitoUserSession.t (defined as
// {. "accessToken": string, "idToken": CognitoUserSession.idToken,
// "refreshToken": CognitoUserSession.token})
let getJwtToken = () => {
PromiseMonad.(
Auth.currentSession()
>>= (
session:CognitoUserSession.t => {
Js.log2("RESPONSE_VALUE_TOKEN_AMPLIFY", session);
// let jwtToken:CognitoUserSession.t = session##idToken##jwtToken;
let jwtToken = session##idToken##jwtToken;
// Js.log2("RESPONSE_VALUE_jwtToken_AMPLIFY", jwtToken);
return(jwtToken);
}
)
);
};
{
"idToken": {
"jwtToken": "eyJraWQiOiJ4YTMxdUlLMnpoSU1cL09XdW9VajdsUHFCZUhlRWZoYStaVXJHOUtFaW94ST0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiI1NTYyZGE2Zi0zNDU5LTQxZmItYmUxYy04ZjY1M2U3OGJlZjkiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLmV1LXdlc3QtMS5hbWF6b25hd3MuY29tXC9ldS13ZXN0LTFfdTdzT0o1QWxJIiwicGhvbmVfbnVtYmVyX3ZlcmlmaWVkIjpmYWxzZSwiY29nbml0bzp1c2VybmFtZSI6Imlka2pzIiwiYXVkIjoiMWNtYmE2NjJ2ZGExaWw1Nm9qaDdqcXZqNGwiLCJldmVudF9pZCI6IjgwNjVkMzhiLTMzODMtMTFlOS04NmMwLTM5ODQ3NzhjNDUyMSIsInRva2VuX3VzZSI6ImlkIiwiYXV0aF90aW1lIjoxNTUwNDk3NTUwLCJwaG9uZV9udW1iZXIiOiIrMTU1NTU1NTU1NTUiLCJleHAiOjE1NTA1NjgwODAsImlhdCI6MTU1MDU2NDQ4MCwiZW1haWwiOiJhYXJtYW5kLmluYm94QGdtYWlsLmNvbSJ9.O7ld0ssoqxOrGjAxW9a1vE62f5Pm79GbLMYgEaghVFVCuDKDPdNj3PaCAD1agZGfYr9kctqzKnXZVQl8ZIrMjwTO1UDIe5elRgz8R_Uz1AtJHxUPDu5lxzi7CWARpUCgpCuCFSU5gjEDS-bq5KHhaBX70lMwMwptrSXalubYYSTlAy8FvUkqWdne0_2x5GhqXdp8yIPWeWm0Oqw0xXOGx0CkHi8cu5PcwJSfau6Xs5Q31-UXPUd34FkB4_i0WzpyECFuCnPT0rHwztdtO1-0jNsTTkr6T73hSkzdv8x8b2keYl8ExIB6r8PnprNx8eZw-i-Q--JBvZogQxG2Vy1NTA",
"payload": {
"sub": "5562da6f-3459-41fb-be1c-8f653e78bef9",
"email_verified": true,
"iss": "https://cognito-idp.eu-west-1.amazonaws.com/eu-west-1_u7sOJ5AlI",
"phone_number_verified": false,
"cognito:username": "idkjs",
"aud": "1cmba662vda1il56ojh7jqvj4l",
"event_id": "8065d38b-3383-11e9-86c0-3984778c4521",
"token_use": "id",
"auth_time": 1550497550,
"phone_number": "+15555555555",
"exp": 1550568080,
"iat": 1550564480,
"email": "aarmand.inbox@gmail.com"
}
},
"refreshToken": {
"token": "eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiUlNBLU9BRVAifQ.VirOYYYoADcsO0S1F4ER9IDK9sbw-3EFdSuolShmJ4C-lwJCfrlS6-slCC58gKjiN-GXkZBH_a9iexF_SPVYgyIY_JA4YcQkKVMDPSLFlJ_HKVBJJpmdnShpiX9L64K_kwKlSDhDoyK0gLJt5-KNxfRenn11OilyslVXOJ-f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WRranMifQ.QWZ9XGKs_SPYZnEu5rmLWcUs3VRd2SZWurCn-U0E3A30BKJQDSLoxO4BlbH7vy_NqetA01i7WuKnc8lrcV2bybm2ThqCBfysVYlt7O6JKKu3onfmuxHzPNuZcZGrqNqiZG4yy0OOL67TL2cpOlfQ1EvkJbcJvpKjHyVYizt8SaxTpawOzAdpMXVRpJb-KAiG8NUu-Ka9caBvvZP_UY4a6YjNr-PeSOtQfbrPYgALf_1tEUoZYCFMDZbSDk3CX9VAka3Ty1UXq8PrKRGpLDD2YgKK3E04WNqITHNI8AtvNPbC7QUBaOB6pisYLLCr3m9ic3hSHmtTIjYukMoFu-uAtw",
"payload": {
"sub": "5562da6f-3459-41fb-be1c-8f653e78bef9",
"event_id": "8065d38b-3383-11e9-86c0-3984778c4521",
"token_use": "access",
"scope": "aws.cognito.signin.user.admin",
"auth_time": 1550497550,
"iss": "https://cognito-idp.eu-west-1.amazonaws.com/eu-west-1_u7sOJ5AlI",
"exp": 1550568080,
"iat": 1550564480,
"jti": "3a565802-025c-471f-9560-ecb738dc8504",
"client_id": "1cmba662vda1il56ojh7jqvj4l",
"username": "idkjs"
}
},
"clockDrift": 0
}
module AccessTokenPayload = {
type t = {
.
"sub": string,
"event_id": string,
"token_use": string,
"scope": string,
"auth_time": int,
"iss": string,
"exp": int,
"iat": int,
"jti": string,
"client_id": string,
"username": string,
};
};
module AccessToken = {
type t = {
.
"jwtToken": string,
"payload": AccessTokenPayload.t,
};
};
module IDTokenPayload = {
type t = {
.
"sub": string,
"email_verified": bool,
"iss": string,
"phone_number_verified": bool,
"cognito_username": string,
"aud": string,
"event_id": string,
"token_use": string,
"auth_time": int,
"phone_number": string,
"exp": int,
"iat": int,
"email": string,
};
};
module IDToken = {
type t = {
.
"jwtToken": string,
"payload": IDTokenPayload.t,
};
};
module RefreshToken = {
type t = {
.
"token": string,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.