forked from humanprotocol/human-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from blockydevs/HAF-53-add-verification-signat…
…ure-at-beginning-of-operator-sign-up-flow Haf 53 add verification signature at beginning of operator sign up flow
- Loading branch information
Showing
13 changed files
with
256 additions
and
49 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
35 changes: 35 additions & 0 deletions
35
packages/apps/human-app/frontend/src/api/servieces/operator/prepare-signature.ts
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,35 @@ | ||
import { z } from 'zod'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { apiClient } from '@/api/api-client'; | ||
import { apiPaths } from '@/api/api-paths'; | ||
|
||
export enum PrepareSignatureType { | ||
SignUp = 'SIGNUP', | ||
DisableOperator = 'DISABLE_OPERATOR', | ||
} | ||
|
||
export const prepareSignatureSuccessSchema = z.object({ | ||
from: z.string(), | ||
to: z.string(), | ||
contents: z.string(), | ||
}); | ||
|
||
export type SignatureData = z.infer<typeof prepareSignatureSuccessSchema>; | ||
|
||
export function usePrepareSignature({ | ||
address, | ||
type, | ||
}: { | ||
address: string; | ||
type: PrepareSignatureType; | ||
}) { | ||
return useQuery({ | ||
queryFn: () => | ||
apiClient(apiPaths.operator.web3Auth.prepareSignature.path, { | ||
successSchema: prepareSignatureSuccessSchema, | ||
options: { method: 'POST', body: JSON.stringify({ address, type }) }, | ||
}), | ||
queryKey: [address, type], | ||
refetchInterval: 0, | ||
}); | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/apps/human-app/frontend/src/api/servieces/operator/web3-signup.ts
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,21 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { z } from 'zod'; | ||
import { useConnectedWallet } from '@/auth-web3/use-connected-wallet'; | ||
import { apiClient } from '@/api/api-client'; | ||
import { apiPaths } from '@/api/api-paths'; | ||
|
||
export function useWeb3SignUp() { | ||
const { address, chainId } = useConnectedWallet(); | ||
|
||
return useMutation({ | ||
mutationFn: async ({ signature }: { signature: string }) => | ||
apiClient(apiPaths.operator.web3Auth.signUp.path, { | ||
successSchema: z.unknown(), | ||
options: { | ||
method: 'POST', | ||
body: JSON.stringify({ address, signature }), | ||
}, | ||
}), | ||
mutationKey: ['web3SignUp', address, chainId], | ||
}); | ||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/apps/human-app/frontend/src/auth-web3/use-connected-wallet.tsx
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
92 changes: 92 additions & 0 deletions
92
packages/apps/human-app/frontend/src/auth-web3/web3-auth-context.tsx
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,92 @@ | ||
import { useState, createContext, useEffect } from 'react'; | ||
import { jwtDecode } from 'jwt-decode'; | ||
import { z } from 'zod'; | ||
import type { SignInSuccessResponse } from '@/api/servieces/worker/sign-in'; | ||
import { web3browserAuthProvider } from '@/auth-web3/web3-browser-auth-provider'; | ||
|
||
const web3userDataSchema = z.object({ | ||
// TODO add valid schema that defines JWT payload | ||
address: z.string().nullable().optional(), | ||
}); | ||
|
||
type Web3UserData = z.infer<typeof web3userDataSchema>; | ||
|
||
type AuthStatus = 'loading' | 'error' | 'success' | 'idle'; | ||
export interface Web3AuthenticatedUserContextType { | ||
user: Web3UserData; | ||
status: AuthStatus; | ||
signOut: () => void; | ||
signIn: (singIsSuccess: SignInSuccessResponse) => void; | ||
} | ||
|
||
interface Web3UnauthenticatedUserContextType { | ||
user: null; | ||
status: AuthStatus; | ||
signOut: () => void; | ||
signIn: (singIsSuccess: SignInSuccessResponse) => void; | ||
} | ||
|
||
export const Web3AuthContext = createContext< | ||
Web3AuthenticatedUserContextType | Web3UnauthenticatedUserContextType | null | ||
>(null); | ||
|
||
export function Web3AuthProvider({ children }: { children: React.ReactNode }) { | ||
const [web3AuthState, setWeb3AuthState] = useState<{ | ||
user: Web3UserData | null; | ||
status: AuthStatus; | ||
}>({ user: null, status: 'loading' }); | ||
|
||
// TODO update SignInSuccessResponse according to new endpoint web3/auth/signin | ||
const handleSignIn = () => { | ||
try { | ||
const accessToken = web3browserAuthProvider.getAccessToken(); | ||
if (!accessToken) { | ||
setWeb3AuthState({ user: null, status: 'idle' }); | ||
return; | ||
} | ||
const userData = jwtDecode(accessToken); | ||
const validUserData = web3userDataSchema.parse(userData); | ||
setWeb3AuthState({ user: validUserData, status: 'success' }); | ||
web3browserAuthProvider.subscribeSignOut(() => { | ||
setWeb3AuthState({ user: null, status: 'idle' }); | ||
}); | ||
} catch { | ||
web3browserAuthProvider.signOut(); | ||
setWeb3AuthState({ user: null, status: 'error' }); | ||
} | ||
}; | ||
// TODO correct interface of singIsSuccess from auth/web3/signin | ||
const signIn = (singIsSuccess: SignInSuccessResponse) => { | ||
web3browserAuthProvider.signIn(singIsSuccess); | ||
handleSignIn(); | ||
}; | ||
|
||
const signOut = () => { | ||
web3browserAuthProvider.signOut(); | ||
setWeb3AuthState({ user: null, status: 'idle' }); | ||
}; | ||
|
||
useEffect(() => { | ||
handleSignIn(); | ||
}, []); | ||
|
||
return ( | ||
<Web3AuthContext.Provider | ||
value={ | ||
web3AuthState.user && web3AuthState.status === 'success' | ||
? { | ||
...web3AuthState, | ||
signOut, | ||
signIn, | ||
} | ||
: { | ||
...web3AuthState, | ||
signOut, | ||
signIn, | ||
} | ||
} | ||
> | ||
{children} | ||
</Web3AuthContext.Provider> | ||
); | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/apps/human-app/frontend/src/auth-web3/web3-browser-auth-provider.ts
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,46 @@ | ||
import type { SignInSuccessResponse } from '@/api/servieces/worker/sign-in'; | ||
|
||
const web3accessTokenKey = btoa('web3_access_token'); | ||
|
||
const web3browserAuthProvider = { | ||
isAuthenticated: false, | ||
signOutCallback: (() => undefined) as () => void, | ||
signIn(singIsSuccess: SignInSuccessResponse) { | ||
web3browserAuthProvider.isAuthenticated = true; | ||
localStorage.setItem(web3accessTokenKey, btoa(singIsSuccess.access_token)); | ||
}, | ||
signOut() { | ||
web3browserAuthProvider.isAuthenticated = false; | ||
localStorage.removeItem(web3accessTokenKey); | ||
web3browserAuthProvider.triggerSignOutSubscriptions(); | ||
}, | ||
getAccessToken() { | ||
const result = localStorage.getItem(web3accessTokenKey); | ||
|
||
if (!result) { | ||
return null; | ||
} | ||
|
||
return atob(result); | ||
}, | ||
getRefreshToken() { | ||
const result = localStorage.getItem(web3accessTokenKey); | ||
|
||
if (!result) { | ||
return null; | ||
} | ||
|
||
return atob(result); | ||
}, | ||
subscribeSignOut(callback: () => void) { | ||
web3browserAuthProvider.signOutCallback = callback; | ||
}, | ||
unsubscribeSignOut() { | ||
web3browserAuthProvider.signOutCallback = () => undefined; | ||
}, | ||
triggerSignOutSubscriptions() { | ||
web3browserAuthProvider.signOutCallback(); | ||
}, | ||
}; | ||
|
||
export { web3browserAuthProvider }; |
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
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
Oops, something went wrong.