feat: login gmail

This commit is contained in:
Dian Bayu Nugroho 2024-02-20 14:43:43 +07:00
parent 8b1b3e950f
commit 9e2304990c
5 changed files with 203 additions and 82 deletions

View File

@ -0,0 +1,14 @@
class LoginGmailModel {
String idToken;
String accessToken;
LoginGmailModel({
required this.idToken,
required this.accessToken,
});
Map<String, String> toJson() => {
"idToken": idToken,
"accessToken": accessToken,
};
}

View File

@ -5,9 +5,10 @@ import 'package:cims_apps/application/theme/color_palette.dart';
import 'package:cims_apps/core/route/route.dart'; import 'package:cims_apps/core/route/route.dart';
import 'package:cims_apps/core/utils/size_config.dart'; import 'package:cims_apps/core/utils/size_config.dart';
import 'package:cims_apps/features/auth/login/view/login_view.dart'; import 'package:cims_apps/features/auth/login/view/login_view.dart';
import 'package:cims_apps/features/auth/registration/view/initial_registration_step.dart';
import 'package:cims_apps/features/auth/registration/view/registration_view.dart'; import 'package:cims_apps/features/auth/registration/view/registration_view.dart';
import 'package:cims_apps/features/dashboard/dashboard_public/viewmodel/dashboard_public_viewmodel.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class DashboardPublicView extends StatelessWidget { class DashboardPublicView extends StatelessWidget {
static const routeName = '/DashboardPublicView'; static const routeName = '/DashboardPublicView';
@ -37,6 +38,9 @@ class DashboardPublicView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => DashboardPublicViewModel(),
builder: (context, child) {
return Scaffold( return Scaffold(
body: SingleChildScrollView( body: SingleChildScrollView(
padding: const EdgeInsets.only( padding: const EdgeInsets.only(
@ -45,7 +49,9 @@ class DashboardPublicView extends StatelessWidget {
left: 24.0, left: 24.0,
right: 24.0, right: 24.0,
), ),
child: Column( child: Consumer<DashboardPublicViewModel>(
builder: (context, provider, child) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
@ -96,7 +102,7 @@ class DashboardPublicView extends StatelessWidget {
width: 26, width: 26,
), ),
onPressed: () { onPressed: () {
routePush(context, page: const InitialRegistrationStep()); provider.loginGoogle(context);
}, },
), ),
Row( Row(
@ -117,8 +123,10 @@ class DashboardPublicView extends StatelessWidget {
], ],
) )
], ],
), );
}),
), ),
); );
});
} }
} }

View File

@ -0,0 +1,50 @@
import 'package:cims_apps/core/route/route.dart';
import 'package:cims_apps/features/auth/registration/view/initial_registration_step.dart';
import 'package:cims_apps/features/dashboard/dashboard_public/model/login_gmail_model.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
class DashboardPublicViewModel extends ChangeNotifier {
String emailGoogle = '';
final GoogleSignIn googleSignIn = GoogleSignIn(
scopes: [
'email',
'https://www.googleapis.com/auth/contacts.readonly',
],
);
Future<LoginGmailModel?> _getGmail() async {
LoginGmailModel? loginGmailModel;
try {
final signInResult = await googleSignIn.signIn();
if (signInResult != null) {
emailGoogle = signInResult.email;
final signInAuth = await signInResult.authentication;
final accessToken = signInAuth.accessToken;
final idToken = signInAuth.idToken;
if (idToken != null && accessToken != null) {
loginGmailModel = LoginGmailModel(
accessToken: accessToken,
idToken: idToken,
);
}
}
} catch (e) {
debugPrint('catch error $e');
}
return loginGmailModel;
}
Future<bool> loginGoogle(BuildContext context) async {
bool loginSuccess = false;
loginSuccess = await _getGmail().then((payload) async {
bool result = false;
if (payload != null) {
debugPrint('objectzz ${payload.toJson()}');
routePush(context, page: InitialRegistrationStep());
googleSignIn.disconnect();
}
return result;
});
return loginSuccess;
}
}

View File

@ -264,6 +264,54 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
google_identity_services_web:
dependency: transitive
description:
name: google_identity_services_web
sha256: "0c56c2c5d60d6dfaf9725f5ad4699f04749fb196ee5a70487a46ef184837ccf6"
url: "https://pub.dev"
source: hosted
version: "0.3.0+2"
google_sign_in:
dependency: "direct main"
description:
name: google_sign_in
sha256: "0b8787cb9c1a68ad398e8010e8c8766bfa33556d2ab97c439fb4137756d7308f"
url: "https://pub.dev"
source: hosted
version: "6.2.1"
google_sign_in_android:
dependency: transitive
description:
name: google_sign_in_android
sha256: bfd42c81c30c6faba16e0f62968d5505a87504aaa672b3155ee931461abb0a49
url: "https://pub.dev"
source: hosted
version: "6.1.21"
google_sign_in_ios:
dependency: transitive
description:
name: google_sign_in_ios
sha256: f3336d9e44d4d28063ac90271f6db5caf99f0480cb07281330e7a432edb95226
url: "https://pub.dev"
source: hosted
version: "5.7.3"
google_sign_in_platform_interface:
dependency: transitive
description:
name: google_sign_in_platform_interface
sha256: "1f6e5787d7a120cc0359ddf315c92309069171306242e181c09472d1b00a2971"
url: "https://pub.dev"
source: hosted
version: "2.4.5"
google_sign_in_web:
dependency: transitive
description:
name: google_sign_in_web
sha256: a278ea2d01013faf341cbb093da880d0f2a552bbd1cb6ee90b5bebac9ba69d77
url: "https://pub.dev"
source: hosted
version: "0.12.3+2"
group_button: group_button:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -52,6 +52,7 @@ dependencies:
dotted_border: ^2.1.0 dotted_border: ^2.1.0
shared_preferences: ^2.2.2 shared_preferences: ^2.2.2
calendar_date_picker2: ^0.5.3 calendar_date_picker2: ^0.5.3
google_sign_in: ^6.2.1