Could any of you help me?
I want to modify the Daml UI template with as little change as possible to use Auth0 login.
My plan is to keep the login button as is, ignore the two text input fields on the login page, trigger an Auth0 login via the login button, retrieve the access token from Auth0, get the Daml ledger party id from the token, and pass the party and token variables to the UI template.
I have set up the Auth0 user profile so that I specify the custom Daml ledger claims (including the party id as the only element of the actAs
list) in the user profile. In this way, if the user authenticates with Auth0, Auth0 will return the correct Daml access token.
The current form of the loginUser
function in the Daml UI template is the following:
function loginUser(
dispatch : React.Dispatch<LoginAction>,
party : string,
userToken : string,
history : History,
setIsLoading : React.Dispatch<React.SetStateAction<boolean>>,
setError : React.Dispatch<React.SetStateAction<boolean>>) {
setError(false);
setIsLoading(true);
if (!!party) {
const token = userToken || createToken(party)
localStorage.setItem(damlPartyKey, party);
localStorage.setItem(damlTokenKey, token);
dispatch({ type: "LOGIN_SUCCESS", token, party });
setError(false);
setIsLoading(false);
history.push("/app");
} else {
dispatch({ type: "LOGIN_FAILURE" });
setError(true);
setIsLoading(false);
}
}
See in the UserContext.tsx file: daml-ui-template/UserContext.tsx at eecf1f7983ec5a65c87f0140908a0ac151a1619c · digital-asset/daml-ui-template · GitHub
From Auth0, I have the @auth0/auth0-react
package, see: GitHub - auth0/auth0-react: Auth0 SDK for React Single Page Applications (SPA)
In it, the useAuth0
hook exports the functions which I need for the login, and for token retrieval: auth0-react/use-auth0.tsx at 9a15c540fee6d53514eaaea77cbadfb767b14891 · auth0/auth0-react · GitHub
* const {
* // Auth state:
* error,
* isAuthenticated,
* isLoading,
* user,
* // Auth methods:
* getAccessTokenSilently,
* getAccessTokenWithPopup,
* getIdTokenClaims,
* loginWithRedirect,
* loginWithPopup,
* logout,
* } = useAuth0<TUser>();
I want to use loginWithRedirect
for login, and getAccessTokenSilently
for retrieving the access token.
The point where I’m stuck is that the original loginUser
function is not async, whereas both functions which I want to use from useAuth0
are async functions.
In theory, I know that I have to use here React hooks, as does the current version of the Login component of the Daml UI template, but it seems I don’t have enough experience yet with React state management to find the simple solution for transforming the loginUser
function into an async function, preserving its current handling of the party
and token
variables.
I would appreciate any hint.