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

Pf/dapp auth scaffold #30

merged 2 commits into from
Mar 8, 2021

Conversation

paulfreund94
Copy link
Contributor

  • Set-up Apollo Client for using eth-address and eth-signature headers.
  • Add a button that stores signature in local storage.
  • Add a provider that provides up to date info as to whether user's signature is in local storage
    • Current set-up should work unless user deletes / changes their local storage while using the app.
  • Add a form for signing up once signature is in local storage - per @wynstrol the idea is that users sign up with their Raiden, not MetaMask address.
    • Still needs to be hooked up to backend.
  • Add re-formality, update Ethers.res from FLOAT.

@moose-code moose-code requested a review from JasoonS March 3, 2021 17:14
Comment on lines +18 to +19
let getUserSignature = Dom.Storage2.getItem(_, u->ethAdrToLowerStr)
switch(getUserSignature(Dom.Storage2.localStorage)){
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 :)

}

module Providers = {
type t = providerType

@new @module("ethers") @scope("providers")
external makeProvider: string => Web3.rawProvider = "JsonRpcProvider"
external makeProvider: string => t = "JsonRpcProvider"
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we should even delete the Web3 module. And anything we find in it that we still use we should migrate to Ethers

Copy link
Member

Choose a reason for hiding this comment

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

#34

}
}

let getUserAuthStatus = (user) => Apollo.getAuthHeaders(~user)->Option.isSome
Copy link
Member

Choose a reason for hiding this comment

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

Cool!

Simple and works. I was thinking, maybe we want to allow users to remain 'authenticated' even when they aren't connected via their web3 provider. This would allow a nice UX where they can still do stuff in the DB related to their user, but not be annoyed with having to open up and sign in with their web3 provider (eg metamask) first. (I'd probably say this is more needed for float, but still nice to have here)

To do this I'd create another field in local storage with the key "CurrentUserAuth". Then the concept of being 'logged in' and being 'connected to the ethereum network' become independent things. (I hope this won't add too much complexity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah seems like a much better way of doing it - allows for a much more flexible set-up. Think we'll have to watch for syncing issues, presuming that when the user is connected with a wallet we want their wallet address to be their CurrentUserAuth, but shouldn't be too much of a hassle.

Copy link
Member

Choose a reason for hiding this comment

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

It should sync instantly in that useEffect function in the provider?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it will 😅

let (isAuthorized, setIsAuthorized) = React.useState(_ => false)

React.useEffect1(() => {
let _ = setIsAuthorized(_ => user->getUserAuthStatus)
Copy link
Member

Choose a reason for hiding this comment

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

The getUserAuthStatus takes in a option<string> right? Maybe we should make it take in a string?

Suggested change
let _ = setIsAuthorized(_ => user->getUserAuthStatus)
switch user {
|Some(_) =>
let isAuthorized = user->getUserAuthStatus->Option.map(usersAuth => {
let _ = setAuthStatus("CurrentUserAuth", usersAuth)
})->Option.isSome
let _ = setIsAuthorized(_ => isAuthorized)
| None =>
let _ = setIsAuthorized(_ => Some("CurrentUserAuth"->getUserAuthStatus)

Copy link
Member

Choose a reason for hiding this comment

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

Didn't test this code, probably got some errors, this is just the general idea.

Anyway, lets make this a new task :)

Copy link
Member

Choose a reason for hiding this comment

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

#35

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The getUserAuthStatus takes in a option<string> right? Maybe we should make it take in a string?

Takes in an option<string> as I'm using it also in the case that the user isn't connected to metamask. Probably better to refactor that logic out though, especially if we're moving away from having metamask and the DB auth necessarily connected.

Copy link
Member

Choose a reason for hiding this comment

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

You can make it take in a string and use let optionAuthStatus = optUser->Option.map(getUserAuthStatus) which is cleaner IMO.

Copy link
Member

@JasoonS JasoonS left a comment

Choose a reason for hiding this comment

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

Awesome stuff :)

@JasoonS JasoonS merged commit af9a26d into dev Mar 8, 2021
@JasoonS JasoonS deleted the pf/dapp-auth-scaffold branch March 8, 2021 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants