-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
paulfreund94
commented
Mar 3, 2021
- 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.
let getUserSignature = Dom.Storage2.getItem(_, u->ethAdrToLowerStr) | ||
switch(getUserSignature(Dom.Storage2.localStorage)){ |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
} | ||
|
||
let getUserAuthStatus = (user) => Apollo.getAuthHeaders(~user)->Option.isSome |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
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) |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
getUserAuthStatus
takes in aoption<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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome stuff :)