authenticate Method
There are two ways to authenticate new users on your flutter app using Okto Flutter Sdk.
Authenticate user with idToken
received from Google OAuth2.
Parameters
idToken
- Token received from Google OAuth2.
If the user does not have a wallet, a wallet prompt will be shown to the user to create a wallet.
try {
await okto.authenticate(idToken: 'idToken');
print('success');
} catch (e) {
print(e);
}
Example
import 'package:my_app/okto.dart';
import 'package:my_app/screens/home/home_page.dart';
import 'package:flutter/material.dart';
class LoginWithGoogle extends StatefulWidget {
const LoginWithGoogle({super.key});
@override
State<LoginWithGoogle> createState() => _LoginWithGoogleState();
}
class _LoginWithGoogleState extends State<LoginWithGoogle> {
final GoogleSignIn googleSignIn = GoogleSignIn(
scopes: [
'email',
'https://www.googleapis.com/auth/userinfo.profile',
'openid',
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xff5166EE),
body: SafeArea(
child: Column(
children: [
Expanded(
child: Container(
alignment: Alignment.center,
margin: const EdgeInsets.all(40),
child: const Text(
'Login with google',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.w800, fontSize: 30),
),
),
),
ElevatedButton(
onPressed: () async {
try {
final GoogleSignInAccount? googleUser = await googleSignIn.signIn();
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
if (googleAuth != null) {
final String? idToken = googleAuth.idToken;
await okto!.authenticate(idToken: idToken!);
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const HomePage()));
}
} catch (e) {
print(e.toString());
setState(() {
error = e.toString();
});
}
},
child: const Text('Login with Google')),
Text(error),
const SizedBox(height: 20)
],
),
),
);
}
}