From d0f6aafba0984becf1190571bc8ef3bc39058acb Mon Sep 17 00:00:00 2001 From: klukovka Date: Wed, 2 Jun 2021 14:06:22 +0300 Subject: [PATCH] google auth added --- chem_solution_mobile/lib/bloc/auth_bloc.dart | 23 +++- .../lib/models/Autorisation.dart | 30 +++++- chem_solution_mobile/lib/models/BlogPost.dart | 11 +- .../lib/profile/profile_page.dart | 102 +++++++++--------- .../lib/widgets/auth_widget.dart | 1 + 5 files changed, 109 insertions(+), 58 deletions(-) diff --git a/chem_solution_mobile/lib/bloc/auth_bloc.dart b/chem_solution_mobile/lib/bloc/auth_bloc.dart index 010afc4..9a45fc3 100644 --- a/chem_solution_mobile/lib/bloc/auth_bloc.dart +++ b/chem_solution_mobile/lib/bloc/auth_bloc.dart @@ -1,3 +1,5 @@ +import 'package:chem_solution_mobile/main.dart'; +import 'package:chem_solution_mobile/models/Autorisation.dart'; import 'package:chem_solution_mobile/services/auth_serv.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:google_sign_in/google_sign_in.dart'; @@ -20,8 +22,23 @@ class AuthBloc { //fire base sign in final result = await authService.signInWithCredential(credential); - - print('${result.user.displayName}'); + print('------------------------'); + Autorisation.signIn( + '${result.user.email}', '${result.user.uid}', () => {}); + if (!autorised) { + Autorisation.signUp( + '${result.user.email}', + '${result.user.displayName}', + '${result.user.metadata.creationTime}', + '${result.user.uid}', + ); + Autorisation.signIn( + '${result.user.email}', '${result.user.uid}', () => {}); + } + print('Nick ${result.user.displayName}'); + print('Email ${result.user.email}'); + print('Password ${result.user.uid}'); + print('${result.user.metadata.creationTime}'); } catch (e) { print(e); } @@ -31,6 +48,4 @@ class AuthBloc { authService.logout(); googleSignin.signOut(); } - - } diff --git a/chem_solution_mobile/lib/models/Autorisation.dart b/chem_solution_mobile/lib/models/Autorisation.dart index eb183e8..96a2a4a 100644 --- a/chem_solution_mobile/lib/models/Autorisation.dart +++ b/chem_solution_mobile/lib/models/Autorisation.dart @@ -20,7 +20,7 @@ abstract class Autorisation { try { final response = await http.get( Uri.http(chemURL, 'getjwt', {'email': email, 'password': password})); - + print('Авторизация ${response.statusCode}'); if ('${response.statusCode}'.indexOf('2') == 0) { try { await storage.write( @@ -29,6 +29,7 @@ abstract class Autorisation { RegExp(r'n":"(\S)+",'))); autorised = true; setUser(); + print('${await storage.read(key: 'token')}'); } catch (ex) { print(ex); autorised = false; @@ -42,6 +43,33 @@ abstract class Autorisation { } } + static Future signUp(String userEmail, String userName, + String dateOfBirth, String password) async { + try { + var date = '${dateOfBirth.replaceFirst(' ', 'T')}Z'; + print(date); + var userBody = { + "userEmail": userEmail, + "userName": userName, + "dateOfBirth": date, + "password": password + }; + + final response = await http.post( + Uri.https(chemURL, 'Users'), + headers: { + "Accept": "application/json", + "content-type": "application/json" + }, + body: json.encode(userBody), + encoding: Encoding.getByName("utf-8"), + ); + print('Регистрация ${response.statusCode}'); + } catch (ex) { + print(ex); + } + } + static Future setUser() async { currentUser = await User.fetchObject(); } diff --git a/chem_solution_mobile/lib/models/BlogPost.dart b/chem_solution_mobile/lib/models/BlogPost.dart index b220955..381e3c8 100644 --- a/chem_solution_mobile/lib/models/BlogPost.dart +++ b/chem_solution_mobile/lib/models/BlogPost.dart @@ -63,7 +63,7 @@ class BlogPost extends Model { bp.category = o['category']; bp.information = o['information']; bp.isLocked = o['isLocked']; - bp.image = o['image']?? notFound; + bp.image = o['image'] ?? notFound; /* List u = []; o['users'].forEach((e) { u.add(User.fromObject(e)); @@ -83,8 +83,8 @@ class BlogPost extends Model { throw Exception('Failed to load'); } } catch (ex) { - showToast('Помилка підключення', themeRed, themeDarkRed, - Icons.error, FToast()); + showToast( + 'Помилка підключення', themeRed, themeDarkRed, Icons.error, FToast()); } } @@ -107,9 +107,10 @@ class BlogPost extends Model { String token = await storage.read(key: 'token'); try { final response = await http.post( - Uri.http(chemURL, 'Users/liked/add/${this.blogPostId}'), + Uri.https(chemURL, 'Users/liked/add/${this.blogPostId}'), headers: {HttpHeaders.authorizationHeader: 'Bearer $token'}); + print(response.statusCode); Autorisation.setUser(); return response.statusCode == 200; } catch (ex) { @@ -123,7 +124,7 @@ class BlogPost extends Model { try { final response = await http.post( - Uri.http(chemURL, 'Users/liked/remove/${this.blogPostId}'), + Uri.https(chemURL, 'Users/liked/remove/${this.blogPostId}'), headers: {HttpHeaders.authorizationHeader: 'Bearer $token'}); Autorisation.setUser(); diff --git a/chem_solution_mobile/lib/profile/profile_page.dart b/chem_solution_mobile/lib/profile/profile_page.dart index 43bbece..3baa75e 100644 --- a/chem_solution_mobile/lib/profile/profile_page.dart +++ b/chem_solution_mobile/lib/profile/profile_page.dart @@ -1,3 +1,4 @@ +import 'package:chem_solution_mobile/bloc/auth_bloc.dart'; import 'package:chem_solution_mobile/profile/achivements_page.dart'; import 'package:chem_solution_mobile/profile/liked_posts.dart'; import 'package:chem_solution_mobile/profile/materials_page.dart'; @@ -8,6 +9,7 @@ import 'package:chem_solution_mobile/main.dart'; import 'package:community_material_icon/community_material_icon.dart'; import 'package:chem_solution_mobile/assets/alerts.dart'; import 'package:chem_solution_mobile/assets/colors.dart'; +import 'package:provider/provider.dart'; class Profile extends StatefulWidget { Profile({Key key}) : super(key: key); @@ -42,50 +44,6 @@ class _ProfileState extends State with SingleTickerProviderStateMixin { ); } - Widget _exitDialog(BuildContext context) { - return AlertDialog( - elevation: 24, - title: Text( - 'Увага!', - style: TextStyle( - color: themeDark, - fontWeight: FontWeight.bold, - ), - ), - content: Text( - 'Ви точно хочете вийти?', - style: TextStyle( - color: themeDark, - ), - ), - actions: [ - // ignore: deprecated_member_use - FlatButton( - onPressed: () { - autorised = false; - storage.delete(key: 'token'); - currentUser = null; - refresh(); - Navigator.of(context).pop(); - }, - child: Text( - 'Так', - style: TextStyle(color: Colors.red), - )), - // ignore: deprecated_member_use - FlatButton( - onPressed: () { - Navigator.of(context).pop(); - }, - child: Text( - 'Ні', - style: TextStyle(color: themeBlue, fontWeight: FontWeight.w700), - ), - ), - ], - ); - } - AnimationController _controller; Animation _offsetAnimationToLeft; Animation _offsetAnimationToRight; @@ -120,6 +78,55 @@ class _ProfileState extends State with SingleTickerProviderStateMixin { @override Widget build(BuildContext context) { + final authBloc = Provider.of(context, listen: false); + + Widget _exitDialog() { + return AlertDialog( + elevation: 24, + title: Text( + 'Увага!', + style: TextStyle( + color: themeDark, + fontWeight: FontWeight.bold, + ), + ), + content: Text( + 'Ви точно хочете вийти?', + style: TextStyle( + color: themeDark, + ), + ), + actions: [ + // ignore: deprecated_member_use + FlatButton( + onPressed: () { + autorised = false; + storage.delete(key: 'token'); + currentUser = null; + try { + authBloc.logoutGoogle(); + } catch (ex) {} + refresh(); + Navigator.of(context).pop(); + }, + child: Text( + 'Так', + style: TextStyle(color: Colors.red), + )), + // ignore: deprecated_member_use + FlatButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: Text( + 'Ні', + style: TextStyle(color: themeBlue, fontWeight: FontWeight.w700), + ), + ), + ], + ); + } + if (!autorised) { return Container( alignment: Alignment.center, @@ -129,9 +136,8 @@ class _ProfileState extends State with SingleTickerProviderStateMixin { SlideTransition( position: _offsetAnimationToLeft, child: GestureDetector( - onTap: () { - return alertDialogShow( - context, autorisation(refresh), 400); + onTap: () { + return alertDialogShow(context, autorisation(refresh), 400); }, child: _card( context, @@ -262,7 +268,7 @@ class _ProfileState extends State with SingleTickerProviderStateMixin { position: _offsetAnimationToLeft, child: GestureDetector( onTap: () { - return alertDialogShow(context, _exitDialog(context), 200); + return alertDialogShow(context, _exitDialog(), 200); }, child: _card( context, diff --git a/chem_solution_mobile/lib/widgets/auth_widget.dart b/chem_solution_mobile/lib/widgets/auth_widget.dart index ba19e5b..80b9987 100644 --- a/chem_solution_mobile/lib/widgets/auth_widget.dart +++ b/chem_solution_mobile/lib/widgets/auth_widget.dart @@ -12,6 +12,7 @@ import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:provider/provider.dart'; +// ignore: must_be_immutable class AuthWidget extends StatefulWidget { Function() update; AuthWidget({Key key, Function() update}) : super(key: key);