Sumitss

Untitled

Apr 23rd, 2021 (edited)
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.84 KB | None | 0 0
  1. import 'dart:convert';
  2.  
  3. import 'package:get/get.dart';
  4. import 'package:ferry/ferry.dart';
  5. import 'package:hive/hive.dart';
  6. import 'package:myapp/app/routes/app_pages.dart';
  7. import 'package:myapp/graphql/TokenAuthAPI.req.gql.dart';
  8. import 'package:get_it/get_it.dart';
  9. import 'package:flutter_secure_storage/flutter_secure_storage.dart';
  10. class LoginController extends GetxController {
  11.   //TODO: Implement LoginController
  12.   final authState="".obs();
  13.   @override
  14.   void onInit() {
  15.     super.onInit();
  16.   }
  17.  
  18.   @override
  19.   void onReady() {
  20.     super.onReady();
  21.   }
  22.  
  23.   @override
  24.   void onClose() {}
  25.  
  26.  
  27.   void signInWithEmailAndPassword(String email, String password) {
  28.  
  29.     final client= GetIt.instance<Client>(instanceName: 'authClient');
  30.     final loginReq = GTokenAuthAPIReq(
  31.             (b) => b
  32.           ..vars.email = email.toLowerCase()
  33.           ..vars.password = password
  34.  
  35.     );
  36.   //   final registerReq = GRegisterReq(
  37.   //           (b) => b
  38.   //             ..fetchPolicy = FetchPolicy.NetworkOnly
  39.   //         ..vars.r_email = "sumitisagodboy@gmail.com"
  40.   //         ..vars.r_password1 = "Sks1234@ak"
  41.   //             ..vars.r_password2 = "Sks1234@ak"
  42.   //             ..vars.r_username="Saran",
  43.   //   );
  44.     client.request(loginReq).listen((response) async {if(response.hasErrors) {
  45.       throw AuthenticationException(message: 'Wrong username or password');
  46.     } else if (response.data!.tokenAuth!.success==true){
  47.       var token = response.data!.tokenAuth!.token;
  48.       print(["token is", response.data!.tokenAuth!.token]);
  49.       var e= saveUserToken(token!);
  50.       // final authBox = await Hive.openBox("auth");
  51.       // authBox.put('token', token);
  52.       Get.toNamed(Routes.HOME,arguments:[token,e]);
  53.       authState("Authenticated");
  54.     }
  55.     else{
  56.       throw AuthenticationException(message: 'Wrong username or password');
  57.     }
  58.     });
  59.    }
  60.   Future<void> saveUserToken(String token) async {
  61.     try {
  62.       final FlutterSecureStorage secureStorage = const FlutterSecureStorage();
  63.       var containsEncryptionKey = await secureStorage.containsKey(key: 'key');
  64.       if (!containsEncryptionKey) {
  65.         var key = Hive.generateSecureKey();
  66.         await secureStorage.write(key: 'key', value: base64UrlEncode(key));
  67.       }
  68.       var encryptionKey = base64Url.decode((await secureStorage.read(key: 'key'))!);
  69.       print('Encryption key: $encryptionKey');
  70.       var encryptedBox = await Hive.openBox('auth', encryptionCipher: HiveAesCipher(encryptionKey));
  71.       await encryptedBox.put('token', token);
  72.       print(encryptedBox.get('token'));
  73.     } on Exception catch (e) {
  74.       throw Exception( e.toString());
  75.     }
  76.   }
  77.  
  78.   void signOut() {
  79.   }
  80. }
  81.  
  82. class AuthenticationException implements Exception {
  83.   final String message;
  84.   AuthenticationException({this.message = 'Unknown error occurred. '});
  85. }
  86.  
  87.  
Add Comment
Please, Sign In to add comment