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

allow users to be logged in without connected to metamask #38

Merged
merged 1 commit into from
Mar 16, 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
23 changes: 2 additions & 21 deletions dApp/user/src/Apollo.bs.js

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

22 changes: 1 addition & 21 deletions dApp/user/src/Apollo.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,6 @@ 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)){
| None => None
| Some(uS) => Some({ethAddress: u->ethAdrToStr, ethSignature: uS})
}
}
}
}

let httpLink = ApolloClient.Link.HttpLink.make(
~uri=_ => "http://" ++ graphqlEndpoint,
Expand All @@ -33,7 +13,7 @@ let httpLink = ApolloClient.Link.HttpLink.make(
let makeHttpLink = (~user) => ApolloClient.Link.HttpLink.make(
~uri= _ => "http://" ++ graphqlEndpoint,
~headers={
switch(getAuthHeaders(~user)){
switch(Auth.Headers.makeFromOpt(~optUser=user)){
| Some(headers) => headers->Obj.magic
| None => Js.Obj.empty->Obj.magic
}
Expand Down
25 changes: 17 additions & 8 deletions dApp/user/src/App.bs.js

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

44 changes: 36 additions & 8 deletions dApp/user/src/App.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ module OnlyLoggedIn = {
module Main = {
@react.component
let make = () => {
<OnlyLoggedIn> <h1> {"Main component"->React.string} </h1> <Dapp /> </OnlyLoggedIn>
open AuthProvider
let { loggedInStatus } = useAuthStatus()
<>
<h3>
{"Auth Status" -> React.string}
</h3>
<div>
{ loggedInStatus->loggedInStatusToStr->React.string }
</div>
<OnlyLoggedIn>
<h1> {"Main component"->React.string} </h1>
<Dapp />
</OnlyLoggedIn>
</>
}
}
module NotFound = {
Expand All @@ -38,22 +51,37 @@ module Router = {
module GraphQl = {
@react.component
let make = (~children) => {
let user = RootProvider.useCurrentUser()
let optWeb3User = RootProvider.useCurrentUser()
let { loggedInStatus } = AuthProvider.useAuthStatus()
let optDbOnlyUser = switch(loggedInStatus){
| DbOnly(user) => Some(user)
| _ => None
}

let optUser = if(Option.isSome(optWeb3User)){
optWeb3User
}else if(Option.isSome(optDbOnlyUser)){
optDbOnlyUser
} else{
Comment on lines +63 to +65
Copy link
Member

Choose a reason for hiding this comment

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

Looks like the auto formatter isn't working well here.

None
}


let client = React.useMemo1(() =>
Apollo.makeClient(
~user
~user=optUser
)
, [user])
, [optUser])

<ApolloClient.React.ApolloProvider client> children </ApolloClient.React.ApolloProvider>
}
}
@react.component
let make = () =>
<RootProvider>
<GraphQl>
<AuthProvider>
<Router />
<GraphQl>
<Router />
</GraphQl>
</AuthProvider>
</GraphQl>
</RootProvider>
</RootProvider>
20 changes: 19 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.

10 changes: 5 additions & 5 deletions dApp/user/src/Dapp.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let make = () => {
let optWeb3Provider = RootProvider.useWeb3()
let optSigner = RootProvider.useSigner()

let { isAuthorized } = AuthProvider.useAuthStatus()
let { loggedInStatus } = AuthProvider.useAuthStatus()

React.useEffect2(() => {
switch (optWeb3Provider, optSigner) {
Expand All @@ -16,10 +16,10 @@ let make = () => {
}, (optWeb3Provider, optSigner))
<div>
{
if(!isAuthorized){
<AuthenticateButton/>
}else{
<SignUp/>
switch(loggedInStatus){
| Web3Only => <AuthenticateButton/>
| NotLoggedIn => React.null
| _ => <SignUp/>
}
}
<p>
Expand Down
10 changes: 6 additions & 4 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.

11 changes: 7 additions & 4 deletions dApp/user/src/components/Auth/AuthenticateButton.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ let setSignInData = (~ethAddress: string, ~ethSignature: string) => Dom.Storage2
let make = () => {
let signer = RootProvider.useSignerExn()
let userAddress = RootProvider.useCurrentUserExn()
let { setIsAuthorized } = AuthProvider.useAuthStatus()
let { setLoggedInStatus } = 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)
Auth.LocalStorage.setSignInData(
~ethAddress=userAddress,
~ethSignature=result->Js.String2.make)
Auth.LocalStorage.setCurrentUser(~ethAddress=userAddress)
setLoggedInStatus(_ => Web3AndDb)
})
}}
>
{"Authenticate"->React.string}
</button>
}
}
66 changes: 66 additions & 0 deletions dApp/user/src/lib/Auth/Auth.bs.js

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

Loading