Skip to content

Commit

Permalink
google auth added
Browse files Browse the repository at this point in the history
  • Loading branch information
klukovka committed Jun 2, 2021
1 parent 60dd601 commit d0f6aaf
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 58 deletions.
23 changes: 19 additions & 4 deletions chem_solution_mobile/lib/bloc/auth_bloc.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
Expand All @@ -31,6 +48,4 @@ class AuthBloc {
authService.logout();
googleSignin.signOut();
}


}
30 changes: 29 additions & 1 deletion chem_solution_mobile/lib/models/Autorisation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
Expand All @@ -42,6 +43,33 @@ abstract class Autorisation {
}
}

static Future<void> 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<void> setUser() async {
currentUser = await User.fetchObject();
}
Expand Down
11 changes: 6 additions & 5 deletions chem_solution_mobile/lib/models/BlogPost.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<User> u = [];
o['users'].forEach((e) {
u.add(User.fromObject(e));
Expand All @@ -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());
}
}

Expand All @@ -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) {
Expand All @@ -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();

Expand Down
102 changes: 54 additions & 48 deletions chem_solution_mobile/lib/profile/profile_page.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -42,50 +44,6 @@ class _ProfileState extends State<Profile> 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<Offset> _offsetAnimationToLeft;
Animation<Offset> _offsetAnimationToRight;
Expand Down Expand Up @@ -120,6 +78,55 @@ class _ProfileState extends State<Profile> with SingleTickerProviderStateMixin {

@override
Widget build(BuildContext context) {
final authBloc = Provider.of<AuthBloc>(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,
Expand All @@ -129,9 +136,8 @@ class _ProfileState extends State<Profile> with SingleTickerProviderStateMixin {
SlideTransition(
position: _offsetAnimationToLeft,
child: GestureDetector(
onTap: () {
return alertDialogShow(
context, autorisation(refresh), 400);
onTap: () {
return alertDialogShow(context, autorisation(refresh), 400);
},
child: _card(
context,
Expand Down Expand Up @@ -262,7 +268,7 @@ class _ProfileState extends State<Profile> with SingleTickerProviderStateMixin {
position: _offsetAnimationToLeft,
child: GestureDetector(
onTap: () {
return alertDialogShow(context, _exitDialog(context), 200);
return alertDialogShow(context, _exitDialog(), 200);
},
child: _card(
context,
Expand Down
1 change: 1 addition & 0 deletions chem_solution_mobile/lib/widgets/auth_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d0f6aaf

Please sign in to comment.