Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pf/dapp auth scaffold #30

Merged
merged 2 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions dApp/user/bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"reason": {
"react-jsx": 3
},
"bsc-flags": [
"-open Belt",
"-bs-super-errors"
],
"bsc-flags": ["-open Belt", "-bs-super-errors"],
"sources": [
{
"dir": "src",
Expand Down Expand Up @@ -36,10 +33,9 @@
"bs-moment",
"bs-css",
"bs-css-emotion",
"re-formality",
"rescript-apollo-client"
],
"ppx-flags": [
"@reasonml-community/graphql-ppx/ppx"
],
"ppx-flags": ["re-formality/ppx", "@reasonml-community/graphql-ppx/ppx"],
"refmt": 3
}
3 changes: 2 additions & 1 deletion dApp/user/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"moment": "^2.29.1",
"pouchdb-adapter-memory": "^7.2.2",
"raiden-ts": "^0.15.0",
"re-formality": "4.0.0-beta.7",
"react": "^17.0.0",
"react-dom": "^17.0.0",
"subscriptions-transport-ws": "^0.9.18"
Expand All @@ -39,7 +40,7 @@
"@babel/preset-typescript": "^7.12.7",
"@reasonml-community/graphql-ppx": "^1.0.2",
"@truffle/hdwallet-provider": "^1.2.1",
"bs-platform": "^8.4.2",
"bs-platform": "^9.0.1",
"customize-cra": "^1.0.0",
"get-graphql-schema": "^2.1.2",
"react-app-rewired": "^2.1.8",
Expand Down
54 changes: 44 additions & 10 deletions dApp/user/src/Apollo.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 35 additions & 4 deletions dApp/user/src/Apollo.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,43 @@ let graphqlEndpoint = "localhost:8080/v1/graphql"

let headers = {"x-hasura-admin-secret": "testing"}


type clientHeaders = {
@as("eth-address")
ethAddress: string,
@as("eth-signature")
ethSignature: string,
}

let getAuthHeaders = (~user) => {
open Ethers.Utils
switch(user){
| None => None
| Some(u) => {
let getUserSignature = Dom.Storage2.getItem(_, u->ethAdrToLowerStr)
switch(getUserSignature(Dom.Storage2.localStorage)){
Comment on lines +18 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, cool, I wrote my own bindings to this last time, convenient that it already exists :)

| None => None
| Some(uS) => Some({ethAddress: u->ethAdrToStr, ethSignature: uS})
}
}
}
}

let httpLink = ApolloClient.Link.HttpLink.make(
~uri=_ => "http://" ++ graphqlEndpoint,
~headers=Obj.magic(headers),
(),
)

let makeHttpLink = (~user) => ApolloClient.Link.HttpLink.make(
~uri= _ => "http://" ++ graphqlEndpoint,
~headers={
switch(getAuthHeaders(~user)){
| Some(headers) => headers->Obj.magic
| None => Js.Obj.empty->Obj.magic
}
}, ())

let wsLink = {
open ApolloClient.Link.WebSocketLink
make(
Expand All @@ -21,15 +52,15 @@ let wsLink = {
)
}

let terminatingLink = ApolloClient.Link.split(~test=({query}) => {
let terminatingLink = (~user) => ApolloClient.Link.split(~test=({query}) => {
let definition = ApolloClient.Utilities.getOperationDefinition(query)
switch definition {
| Some({kind, operation}) => kind === "OperationDefinition" && operation === "subscription"
| None => false
}
}, ~whenTrue=wsLink, ~whenFalse=httpLink)
}, ~whenTrue=wsLink, ~whenFalse=makeHttpLink(~user))

let client = {
let makeClient = (~user) => {
open ApolloClient
make(
~cache=Cache.InMemoryCache.make(),
Expand All @@ -40,7 +71,7 @@ let client = {
~watchQuery=DefaultWatchQueryOptions.make(~fetchPolicy=NetworkOnly, ~errorPolicy=All, ()),
(),
),
~link=terminatingLink,
~link=terminatingLink(~user),
(),
)
}
27 changes: 23 additions & 4 deletions dApp/user/src/App.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions dApp/user/src/App.res
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,26 @@ module Router = {
}
}
}

module GraphQl = {
@react.component
let make = (~children) => {
let user = RootProvider.useCurrentUser()
let client = React.useMemo1(() =>
Apollo.makeClient(
~user
)
, [user])

<ApolloClient.React.ApolloProvider client> children </ApolloClient.React.ApolloProvider>
}
}
@react.component
let make = () =>
<ApolloClient.React.ApolloProvider client=Apollo.client>
<RootProvider> <Router /> </RootProvider>
</ApolloClient.React.ApolloProvider>
<RootProvider>
<GraphQl>
<AuthProvider>
<Router />
</AuthProvider>
</GraphQl>
</RootProvider>
6 changes: 5 additions & 1 deletion dApp/user/src/Dapp.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion dApp/user/src/Dapp.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ let make = () => {
let optWeb3Provider = RootProvider.useWeb3()
let optSigner = RootProvider.useSigner()

let { isAuthorized } = AuthProvider.useAuthStatus()

React.useEffect2(() => {
switch (optWeb3Provider, optSigner) {
| (Some(web3Provider), Some(_signer)) => {
Expand All @@ -12,5 +14,16 @@ let make = () => {
}
None
}, (optWeb3Provider, optSigner))
<div> <p> {"something"->React.string} </p> </div>
<div>
{
if(!isAuthorized){
<AuthenticateButton/>
}else{
<SignUp/>
}
}
<p>
{"something"->React.string}
</p>
</div>
}
39 changes: 39 additions & 0 deletions dApp/user/src/components/Auth/AuthenticateButton.bs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions dApp/user/src/components/Auth/AuthenticateButton.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
open Ethers.Utils

let setSignInData = (~ethAddress: string, ~ethSignature: string) => Dom.Storage2.localStorage->Dom.Storage2.setItem(ethAddress, ethSignature)

@react.component
let make = () => {
let signer = RootProvider.useSignerExn()
let userAddress = RootProvider.useCurrentUserExn()
let { setIsAuthorized } = AuthProvider.useAuthStatus()
<button
onClick={(_) => {
let _ = Ethers.Wallet.signMessage(
signer,
`flows.finance-signin-string:${userAddress->ethAdrToStr}`,
)->JsPromise.map(result => {
setSignInData(~ethAddress=userAddress->ethAdrToLowerStr, ~ethSignature=result->Js.String2.make)
setIsAuthorized(_ => true)
})
}}
>
{"Authenticate"->React.string}
</button>
}
Loading