const { execute, loading, data, error } = useLogin();
const { execute, loading, data, error } = useLogin();
const address = ...
const login = (profileId: ProfileId) => {
execute({
address: address,
profileId: profileId,
});
};
if (loading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error.message}</div>;
}
if (data) {
return <div>Logged in as {data.profile.id}</div>;
}
Combine with useLazyProfile
to login with Profile handle
const { execute: fetchProfile } = useLazyProfile();
const { execute: login, loading, data, error } = useLogin();
const address = ...
const login = async (handle: string) => {
const profileResult = await fetchProfile({ forHandle: handle });
if (profileResult.isFailure()) {
toast.error(profileResult.error.message);
return;
}
const profile = profileResult.value;
const loginResult = execute({
address: address,
profileId: profile.id,
});
if (loginResult.isFailure()) {
toast.error(loginResult.error.message);
return;
}
// continue with successful login flow for example use
// your routing library to redirect the user somewhere.
};
// continue as before
const { execute, loading, data, error } = useLogin();
const login = (address: string) => {
const result = await execute({ address })
You can handle possible failure scenarios by checking the result
object.
const { execute, loading, data, error } = useLogin();
const login = (profileId: ProfileId) => {
const result = await execute({
address: address,
profileId: profileId,
});
if (result.isFailure()) {
switch (result.error.name) {
case 'PendingSigningRequestError':
console.log(
'There is a pending signing request in your wallet. ' +
'Approve it or discard it and try again.'
);
break;
case 'WalletConnectionError':
console.log('There was an error connecting to your wallet', error.message);
break;
case 'UserRejectedError':
// the user decided to not sign, usually this is silently ignored by UIs
break;
}
return;
}
// ...
};
useLogin
is React Hook that allows you to login with the Lens API.To login with a Lens Profile you need to provide the Profile Id and an EVM address.
The address can be:
Optionally you can login just with an EVM address. In this case the authenticated session returned by useSession will be of type SessionType.JustWallet type and will not contain any Profile information. The credentials associated with this session are limited to:
See the respective hooks documentation for more information.