Created
August 29, 2018 06:44
-
-
Save eyeccc/62d147d49bb7bee562c7111ce04f4313 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Keycloak V2 */ | |
type keycloak; | |
type init; | |
type initParam; | |
[@bs.module] external keycloak : string => keycloak = "keycloak-js"; | |
[@bs.obj] external makeInitParam : (~onLoad: string, unit) => initParam = ""; | |
[@bs.send] external init : (keycloak, initParam) => Js.Promise.t(bool) = ""; | |
let myConfig = "./keycloakConfig.json"; | |
let auth = keycloakWithConfig => keycloakWithConfig |. init(makeInitParam(~onLoad="login-required", ()))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type state = { | |
keycloak: option(Keycloak.keycloak), | |
isAuthed: bool | |
}; | |
type action = | |
| SetAuthenticated(bool) | |
| SetKeycloak(option(Keycloak.keycloak)); | |
let component = ReasonReact.reducerComponent("KeycloakAppV2"); | |
let make = (_children) => { | |
...component, | |
initialState: () => {isAuthed: false, keycloak: None}, | |
reducer: (action, _state) => | |
switch action { | |
| SetAuthenticated(isAuthed) => ReasonReact.Update({...state, isAuthed}) | |
| SetKeycloak(keycloak) => ReasonReact.Update({...state, keycloak}) | |
}, | |
didMount: ({send}) => | |
let keycloakWithConfig = Keycloak.myConfig |> Keycloak.keycloak; | |
keycloakWithConfig | |
|> Keycloak.auth | |
|> Js.Promise.then_( | |
(authenticated) => { | |
authenticated |. SetAuthenticated |> send; | |
if(authenticated){Some(keycloakWithConfig) |. SetKeycloak |> send)}; | |
Js.Promise.resolve() | |
} | |
) | |
|> ignore, | |
render: ({state}) => | |
switch state.isAuthed { | |
| false => <div> ("You're not authorized." |> ReasonReact.str) </div> | |
| true => <MySecuredApp keycloak=state.keycloak /> | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment