-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
130 additions
and
83 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Auth { | ||
type generatedAuth0Client = {. | ||
"authorize": [@bs.meth] (unit => unit) | ||
}; | ||
|
||
type clientOptions = { | ||
. | ||
"domain": string, | ||
"clientID": string, | ||
"redirectUri": string, | ||
"responseType": string, | ||
"scope": string | ||
}; | ||
|
||
[@bs.module "auth0-js"] [@bs.new] external createClient : (clientOptions => generatedAuth0Client) = "WebAuth"; | ||
|
||
let matchAccessToken = [%re "/access_token=([^\$&]+)/g"]; | ||
let matchExpiresIn = [%re "/expires_in=([^\$&]+)/g"]; | ||
let matchIdToken = [%re "/id_token=([^\$&]+)/g"]; | ||
|
||
let resolveOption = (opt) => switch opt { | ||
| None => "" | ||
| Some(s) => s | ||
}; | ||
|
||
let resolveRegex = (exp, str) => { | ||
let res = exp |> Js.Re.exec(str); | ||
switch res { | ||
| None => "" | ||
| Some(result) => { | ||
let captures = result |> Js.Re.captures; | ||
switch captures { | ||
| [|_, token|] => token |> Js.Nullable.to_opt |> resolveOption | ||
| _ => "" | ||
}; | ||
} | ||
}; | ||
}; | ||
|
||
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); | ||
Js.Console.log("Storing auth: " ++ accessToken); | ||
let expiry = Js.Date.now() +. (float_of_string(expiresIn) *. 1000.); | ||
sessionStorage |> setItem("accessToken", accessToken); | ||
sessionStorage |> setItem("id_token", idToken); | ||
sessionStorage |> setItem("expiresIn", string_of_float(expiry)); | ||
|
||
accessToken; | ||
}; | ||
|
||
let getIdToken = () => sessionStorage |> getItem("id_token") |> resolveOption; | ||
|
||
let getAccessToken = () => (sessionStorage |> getItem("accessToken") |> resolveOption); | ||
let tokenStillValid = () => (sessionStorage | ||
|> getItem("expiresIn") | ||
|> Rationale.Option.fmap(e => float_of_string(e)) | ||
|> Rationale.Option.fmap(e => e > Js.Date.now()) | ||
|> Rationale.Option.default(false)); | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,15 @@ | ||
open Types; | ||
|
||
let combineEntries = (pt, entries) => | ||
if (Rationale.RList.any((a => a.kind == pt), entries)) { | ||
entries | ||
|> List.filter(e => e.kind == pt) | ||
|> List.fold_left((a, b) => {id: a.id, kind: pt, value: a.value + b.value}, {id: 0, kind: pt, value: 0}) | ||
|> (entry: entry) => (<Entry key=(string_of_int(entry.id)) entry />) | ||
} else ReasonReact.nullElement; | ||
|
||
let component = ReasonReact.statelessComponent("Entry"); | ||
|
||
let make = (~entries: list(entry), _children) => { | ||
...component, | ||
render: (_) => | ||
render: (_) => { | ||
<div className="entries"> | ||
(if (Rationale.RList.any((a => a.kind == Book), entries)) combineEntries(Book, entries) else ReasonReact.nullElement) | ||
(combineEntries(Lyric, entries)) | ||
(combineEntries(Manga, entries)) | ||
(combineEntries(Net, entries)) | ||
(combineEntries(News, entries)) | ||
(entries | ||
|> List.map((entry: entry) => <Entry key=(string_of_int(entry.id)) entry />) | ||
|> Array.of_list | ||
|> ReasonReact.arrayToElement) | ||
</div> | ||
} | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
let component = ReasonReact.statelessComponent("Login"); | ||
|
||
let make = (_) => { | ||
...component, | ||
render: (_) => | ||
<div> | ||
<div className="title"> (ReasonReact.stringToElement("Dokusho")) </div> | ||
<div className="desc"> | ||
<text> | ||
(ReasonReact.stringToElement( | ||
"Dokusho is a japanese reading tracker" | ||
) | ||
) | ||
</text> | ||
</div> | ||
<div className="login"> <LoginButton /> </div> | ||
</div> | ||
}; |
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
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