Archived Content from Web3Auth Community
This topic was originally posted by harishiv on 9/13/2023.
This content has been migrated from our previous community forum to preserve valuable discussions.
Provided following details for Flutter integration
-
SDK Version:
web3auth_flutter: ^2.0.3andweb3dart: ^2.4.1 -
Screenshots of error:
-
Related to Custom Authentication? Please provide the following info too: (Optional)
- Verifier Name: toad-test-verifier
- Sample idToken(JWT) from TorusUserInfo class :
eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlRZT2dnXy01RU9FYmxhWS1WVlJZcVZhREFncHRuZktWNDUzNU1aUEMwdzAifQ.eyJpYXQiOjE2OTQ0OTkxMDQsImF1ZCI6IkJJLWxxcGFHWllDRW1CRkota09JQ0xDUXNidXd1U1hJcXlONUdQazhOMjdIMG5pM2E4VzA4a1dyRGF6LWRybWRhNjZpbUtPVFFwMHZPSlNmWkJFaDJnSSIsIm5vbmNlIjoiMDNkOGFjOTVjNTJkOWRjMWNhNzIxN2IwMzk2MzQ2YzVhODk1NzcxOTUxZTAzYTZiYWY4Nzc4YTFkZDliMDc4YzYxIiwiaXNzIjoiaHR0cHM6Ly9hcGktYXV0aC53ZWIzYXV0aC5pbyIsIndhbGxldHMiOlt7InB1YmxpY19rZXkiOiIwMmY1ZmY5MGFiNDM1NTQ5YzQzOWQyYTE4NTdlOGE2ZDYxNDRkNDI2OWQ5NDQ4NzRlM2JlYzQ5YWIzODE0MDgyOWEiLCJ0eXBlIjoid2ViM2F1dGhfYXBwX2tleSIsImN1cnZlIjoic2VjcDI1NmsxIn1dLCJlbWFpbCI6ImhhcmlzaGl2QHJvdmVyeC50ZWFtIiwibmFtZSI6IkhhcmlzaGl2IFNpbmdoIiwicHJvZmlsZUltYWdlIjoiaHR0cHM6Ly9saDMuZ29vZ2xldXNlcmNvbnRlbnQuY29tL2EvQUFjSFR0ZUhHaGlFTnRyRDFNcWtJQy1GYzRhbWwwLW1FckUtZFZMdmJXVEtIUnJtPXM5Ni1jIiwidmVyaWZpZXIiOiJ0b2FkLXRlc3QtdmVyaWZpZXIiLCJ2ZXJpZmllcklkIjoiNmN3WDRMUzNFaWVldnByZEI4eDVHamY3ekdNMiIsImFnZ3JlZ2F0ZVZlcmlmaWVyIjoidG9hZC10ZXN0LXZlcmlmaWVyIiwiZXhwIjoxNjk0NTg1NTA0fQ.MQmzN-TpTeYovKs5CHa7yzAoXtigZ1R5fGpomoI_zPXZdE4i-F-B_lTt4BUQx5WisTV0xXoHaI83WctLfq0koQ
Web3Auth initialization and login code snippet below:
Initialisation:
Future<void> _initPlatformState() async { final themeMap = HashMap<String, String>(); themeMap['primary'] = "#F5820D";Uri redirectUrl; if (Platform.isAndroid) { redirectUrl = Uri.parse('w3a://com.example.app/auth'); } else if (Platform.isIOS) { redirectUrl = Uri.parse('com.example.app://openlogin'); } else { throw UnKnownException('Unknown platform'); } final loginConfig = HashMap<String, LoginConfigItem>(); loginConfig['jwt'] = LoginConfigItem( verifier: "toad-test-verifier", // get it from web3auth dashboard typeOfLogin: TypeOfLogin.jwt, name: "Custom JWT Login", clientId: dotenv.env['WEB3AUTH_CLIENT_ID'], ); await Web3AuthFlutter.init( Web3AuthOptions( clientId: dotenv.env['WEB3AUTH_CLIENT_ID'] ?? '', network: Network.testnet, redirectUrl: redirectUrl, whiteLabel: WhiteLabelData( dark: true, name: "Toad Cash", theme: themeMap, ), loginConfig: loginConfig, ), );
}
Login code:
VoidCallback _login(Future<Web3AuthResponse> Function() method) { return () async { context.showProgressBar(); try { final Web3AuthResponse response = await method(); SharedPreference.setPrivateKey(response.privKey.toString()); SharedPreference.setIdToken( response.userInfo?.idToken.toString() ?? ''); UserDetails userDetails = UserDetails( name: response.userInfo?.name, email: response.userInfo?.email, profileImage: response.userInfo?.profileImage); _proceedToSuccessScreen(userDetails); } on UserCancelledException { context.dismissProgressBar(); print("User cancelled."); } on UnKnownException { context.dismissProgressBar(); print("Unknown exception occurred"); } }; }Future<Web3AuthResponse> _googleSignInWithJWT() async {
var idToken = await _getFirebaseIdTokenFromGoogleSignIn();return Web3AuthFlutter.login( LoginParams( loginProvider: Provider.jwt, mfaLevel: MFALevel.NONE, extraLoginOptions: ExtraLoginOptions( id_token: idToken, domain: 'https://toad.cash', ), ), );}
Future<String> _getFirebaseIdTokenFromGoogleSignIn() async {
var idToken = “”;
try {
final GoogleSignInAccount? result = await GoogleSignIn().signIn();
final GoogleSignInAuthentication? googleAuth =
await result?.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
final userCredential =
await FirebaseAuth.instance.signInWithCredential(credential);idToken = (await userCredential.user?.getIdToken(true)).toString(); } on FirebaseAuthException catch (e) { if (e.code == 'user-not-found') { print('No user found for that email.'); } else if (e.code == 'wrong-password') { print('Wrong password provided for that user.'); } } return idToken;
}
_login(_googleSignInWithJWT) method called on Login button’s onPressed.
For iOS integration I have followed the steps mentioned here: https://web3auth.io/docs/sdk/pnp/flutter/install
