Skip to content

Commit

Permalink
Store tokens in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
RawToast committed Mar 31, 2018
1 parent 6c2c7ea commit eefc267
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
29 changes: 20 additions & 9 deletions dokusho/src/App.re
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,30 @@ let reducer = (action, _state) =>

let component = ReasonReact.reducerComponent("App");

let mapUrlToRoute = (url: ReasonReact.Router.url) => {
switch url.path {
| [] => {
Routes.Home;
}
| ["callback"] => {
LoginButton.Auth.handleAuth(url);
}
| _ => {
Routes.Home;
} /* Routes.NotFound */
}
};

let make = _children => {
...component,
reducer,
initialState: () => { Routes.Home },
subscriptions: (self) => [
Sub(
() => ReasonReact.Router.watchUrl((url) => self.send(ChangeRoute(url |> mapUrlToRoute))),
ReasonReact.Router.unwatchUrl
)
],
render: self =>
<ReactToolbox.ThemeProvider theme>
<div className="app">
Expand All @@ -30,12 +50,3 @@ let make = _children => {
</div>
</ReactToolbox.ThemeProvider>
};

let mapUrlToRoute = (url: ReasonReact.Router.url) =>
switch url.path {
| [] => Routes.Home
| ["callback"] => {
LoginButton.Auth.handleAuth(url);
}
| _ => Routes.Home /* Routes.NotFound */
};
7 changes: 6 additions & 1 deletion dokusho/src/app/Client.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type serverResponse = {
};

module Client = {
open Dom.Storage;

let accessToken = () => localStorage |> getItem("accessToken") |> Rationale.Option.default("no_token");

let backendURI = "http://35.189.70.144:8080";
let jsonHeader = Fetch.HeadersInit.make({"Content-Type": "application/json"});
Expand All @@ -18,12 +21,14 @@ module Client = {
};
};
/* Fetches the given user's reading history */
let userHistory = (userId:string) =>
let userHistory = (userId:string) => {
Js.Console.log("Get history " ++ accessToken());
Js.Promise.(
Fetch.fetch(backendURI ++ "/history/" ++ userId)
|> then_(Fetch.Response.json)
|> then_(resp => resp |> parseResponse |> resolve)
);
};

/* Adds a new reading entry for today to a user's reading history */
let newEntry = (userId:string, kind: pageType, value: int) => {
Expand Down
1 change: 1 addition & 0 deletions dokusho/src/app/LoginButton.re
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Auth {
open Dom.Storage;

let handleAuth = (url: ReasonReact.Router.url) => {

let accessToken = url.hash |> resolveRegex(matchAccessToken);
let idToken = url.hash |> resolveRegex(matchIdToken);
let expiresIn = url.hash |> resolveRegex(matchExpiresIn);
Expand Down
1 change: 1 addition & 0 deletions dokusho/src/index.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
[@bs.module "./registerServiceWorker"] external register_service_worker : unit => unit = "default";

ReactDOMRe.renderToElementWithId(<App />, "root");
ReasonReact.Router.push("");

register_service_worker();

0 comments on commit eefc267

Please sign in to comment.