Skip to content

Commit

Permalink
Two page flow, fixed login race
Browse files Browse the repository at this point in the history
  • Loading branch information
RawToast committed Apr 2, 2018
1 parent e52b4a7 commit 2fc89bf
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
14 changes: 8 additions & 6 deletions dokusho/src/App.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ let component = ReasonReact.reducerComponent("App");
let mapUrlToRoute = (url: ReasonReact.Router.url) => {
switch url.path {
| ["callback"] => {
let _token = LoginButton.Auth.handleAuth(url);
Routes.Home;
let token = LoginButton.Auth.handleAuth(url);
Routes.Home(token);
}
| [] => {
Js.Console.log("Home");
Routes.Home;
let token = LoginButton.Auth.getAccessToken();
if(token == "") Routes.NoAuth else Routes.Home(token);
}
| _ => {
Routes.Home;
Routes.NoAuth;
} /* Routes.NotFound */
}
};

let make = _children => {
...component,
reducer,
initialState: () => { Routes.Home },
initialState: () => { Routes.NoAuth },
subscriptions: (self) => [
Sub(
() => ReasonReact.Router.watchUrl((url) => self.send(ChangeRoute(url |> mapUrlToRoute))),
Expand All @@ -47,7 +48,8 @@ let make = _children => {
<ReactToolbox.ThemeProvider theme>
<div className="app">
(switch self.state {
| Routes.Home => <Dokusho/>
| Routes.Home(token) => <Dokusho token=token/>
| Routes.NoAuth => <LoginButton />
})
</div>
</ReactToolbox.ThemeProvider>
Expand Down
14 changes: 7 additions & 7 deletions dokusho/src/app/Client.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Client = {
let accessToken = (default) => sessionStorage |> getItem("accessToken") |> Rationale.Option.default(default);
let backendURI = "http://35.189.70.144:8080";
let jsonHeader = Fetch.HeadersInit.make({"Content-Type": "application/json"});
let authHeader = () => Fetch.HeadersInit.makeWithArray([|( "Content-Type", "application/json" ), ( "accessToken", accessToken("accessToken") )|]);
let authHeader = (default) => Fetch.HeadersInit.makeWithArray([|( "Content-Type", "application/json" ), ( "accessToken", accessToken(default) )|]);

let parseResponse = (json: Js.Json.t) => {
Json.Decode.{
Expand All @@ -32,40 +32,40 @@ module Client = {
};

/* Fetches the given user's reading history, or an empty one */
let userHistory: string => Js.Promise.t(serverResponse) = (_userId:string) => {
let userHistory = (userId:string) => {
Js.Console.log("Get history: " ++ LoginButton.Auth.getAccessToken());
Js.Promise.(
Fetch.fetchWithInit(backendURI ++ "/history",
Fetch.RequestInit.make(
~method_=Get,
~headers=authHeader(),
~headers=authHeader(userId),
()))
|> then_(Fetch.Response.text)
|> then_(resp => resp |> parseResponseT |> resolve)
);
};

/* Adds a new reading entry for today to a user's reading history */
let newEntry = (_userId:string, kind: pageType, value: int) => {
let newEntry = (userId:string, kind: pageType, value: int) => {
Js.Promise.(
Fetch.fetchWithInit(backendURI ++ "/history/add",
Fetch.RequestInit.make(
~method_=Post,
~body=Fetch.BodyInit.make(Encoders.endcodeInput(kind, value) |> Js.Json.stringify),
~headers=authHeader(),
~headers=authHeader(userId),
()))
|> then_(Fetch.Response.json)
|> then_(resp => resp |> parseResponse |> resolve)
);
};

/* Resets a user's reading history */
let resetUser = (_userId:string) => {
let resetUser = (userId:string) => {
Js.Promise.(
Fetch.fetchWithInit(backendURI ++ "/history/reset",
Fetch.RequestInit.make(
~method_=Put,
~headers=authHeader(),
~headers=authHeader(userId),
()))
|> then_(Fetch.Response.json)
|> then_(resp => resp |> parseResponse |> resolve)
Expand Down
5 changes: 2 additions & 3 deletions dokusho/src/app/Dokusho.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Dokusho {
selectedDate: Js.Date.make()
};

let make = (_children) => {
let make = (~token, _children) => {
...component,
initialState: () => initState(),
reducer: (action, { readingData, selectedEntry, selectedDate }) =>
Expand All @@ -30,7 +30,7 @@ module Dokusho {
ReasonReact.Update({readingData: readingData, selectedEntry: selectedEntry, selectedDate: date});
},
didMount: (_self) => {
Actions.loadUserData(testUser);
Actions.loadUserData(token);
},
render: (self) => {
let dateKey = self.state.selectedDate |> DateUtil.dateWithoutTime |> DateUtil.asDateKey;
Expand All @@ -42,7 +42,6 @@ module Dokusho {
let availableDates = DateUtil.availableDates(self.state.readingData);

<div>
<LoginButton />
<div className="title">
(ReasonReact.stringToElement("Dokusho"))
</div>
Expand Down
4 changes: 2 additions & 2 deletions dokusho/src/app/LoginButton.re
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ module Auth {
let authOptions = {
"domain": "dokusho.eu.auth0.com",
"clientID": "mal13rQ1KYJSpAjTBvs72ioa8xhnq8wh",
"redirectUri": "http://35.189.106.56:3000/callback",
/* "redirectUri": "http://localhost:3000/callback", */
/* "redirectUri": "http://35.189.106.56:3000/callback", */
"redirectUri": "http://localhost:3000/callback",
"responseType": "token id_token",
"scope": "openid"
};
Expand Down
3 changes: 2 additions & 1 deletion dokusho/src/app/Types.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ let testUser = "fully";

module Routes {
type route =
| Home;
| Home(string)
| NoAuth;
};

type pageType =
Expand Down
4 changes: 2 additions & 2 deletions dokusho/src/test/Dokusho_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ describe("Dokusho.initialState", () => {
});

describe("Dokusho.make", () => {
test("can render without any provided parameters", () => {
let component = ReactShallowRenderer.renderWithRenderer(<Dokusho />);
test("requires a string value to render", () => {
let component = ReactShallowRenderer.renderWithRenderer(<Dokusho token=""/>);

expect(Js.Undefined.return(component)) |> toBeDefined;
});
Expand Down

0 comments on commit 2fc89bf

Please sign in to comment.