VoidCallback loginF(
GlobalKey<FormState> loginformKey,
BuildContext context,
Future<Web3AuthResponse> Function() method,
) {
return () async {
try {
// await context.read<ConnectivityCubit>().checkInternetAccess();
final Web3AuthResponse response = await method();
BlocProvider.of<AuthCubit>(context).btnLoader(false);
final prefs = await SharedPreferences.getInstance();
log('${response}fahad');
await prefs.setString('privateKey', response.privKey.toString());
print(response.userInfo!.email);
print(response.userInfo!.name);
String key = Constant.webAuthKey;
final claimSet = JwtClaim(otherClaims: <String, dynamic>{
"decodePrivateKey": response.privKey.toString()
}, expiry: DateTime.now().add(const Duration(days: 7)));
final jwttoken = issueJwtHS256(claimSet, key);
AuthRepository auth = AuthRepository();
auth.generateWalllet(context, jwttoken, response.userInfo!.email!);
// Call onSuccessNavigation if login is successful
} on UnKnownException {
log("Unknown exception occurred");
} catch (e) {
print(e);
}
};
}
Future<Web3AuthResponse> withEmailPasswordless(
GlobalKey<FormState> loginformKey, context, String userEmail) async {
BlocProvider.of<AuthCubit>(context).btnLoader(true);
print('press');
Timer(const Duration(seconds: 2),
() => BlocProvider.of<AuthCubit>(context).btnLoader(false));
bool check = await Globals.isInternetConnected();
if (!check) {
Globals.showToast(
'Network Error: There was a problem connecting to our servers. Please check your internet connection and try again.');
return Future.error("No internet connection");
}
if (state.emailController!.text != '' &&
RegExp(r'^[a-zA-Z0-9]+[a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$',
caseSensitive: false
//r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))+'
// r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+"
)
.hasMatch(state.emailController!.text)) {
try {
emit(state.copywith(eemailError: ''));
print('dksah');
return await Web3AuthFlutter.login(LoginParams(
loginProvider: Provider.email_passwordless,
extraLoginOptions: ExtraLoginOptions(login_hint: userEmail),
));
// IMP END - Login
} catch (e) {
print("Error during email/passwordless login: $e");
// Handle the error as needed
// You might want to show a user-friendly message or log the error
return Future.error("Login failed$e");
}
} else {
if (state.emailController!.text.isEmpty &&
state.emailController != null) {
BlocProvider.of<AuthCubit>(context)
.updateEmailError("Please enter your email");
BlocProvider.of<AuthCubit>(context).updatelogin(false);
} else if (!RegExp(
r'^[a-zA-Z0-9]+[a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$',
caseSensitive: false)
.hasMatch(state.emailController!.text)) {
BlocProvider.of<AuthCubit>(context)
.updateEmailError("Invalid email address");
BlocProvider.of<AuthCubit>(context).updatelogin(false);
}
}
return Future.error("Login failed");
// return Future.error("Login failed");
}
after calling this on tap and inserting email the above functions runs for passwordless otp on web3auth
()=>BlocProvider.of<AuthCubit>(
context)
.loginF(
_formKey,
context,
() => BlocProvider.of<
AuthCubit>(context)
.withEmailPasswordless(
_formKey,
context,
state.emailController!
.text
.trim()),
)
some time it is re-produce, the private key is generated and the function login is successful and it allows to navigate based on our login but something after private key is generate the error wont be their but it will stop the navigation after success and will need to allow login again.
@shahbaz