Untitled
unknown
plain_text
4 years ago
3.5 kB
5
Indexable
import 'dart:io'; import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; import 'package:device_info/device_info.dart'; import 'package:flutter/services.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:study_app/models/user.dart'; import 'constants.dart' as Constants; class LoginProvider extends ChangeNotifier { bool _isLoading = true; bool get isLoading => _isLoading; Map<String, dynamic> _deviceDetails = {}; Map<String, dynamic> get deviceDetails => _deviceDetails; User _user = User(); User get user => _user; Future<void> login() async { _checkIsUserLoggedIn(); if (_deviceDetails.isEmpty) { print("[INFO] NEW USER DEVICE DETECTED"); print("[INFO] LOGGING IN..."); _getDeviceDetails(); try { if (_deviceDetails.isNotEmpty) { // post request to server print(_deviceDetails); Map<String, String> requestBody = { 'device_id': _deviceDetails['deviceID']! }; http.Response response = await http .post(Uri.parse(Constants.LOGIN_ENDPOINT), body: requestBody); _user = userFromJson(response.body); _deviceDetails['userID'] = _user.data!.userId!; _deviceDetails['userName'] = _user.data!.name!; _deviceDetails['token'] = _user.data!.token!; _saveLoginDetails(_deviceDetails); print("[INFO] USER LOGGED IN SUCCESSFULLY"); } } catch (error) { print("[INFO] ERROR WHILE FETCHING DEVICE DETAILS: $error"); } } else { print("[INFO] THIS USER DEVICE IS ALREADY LOGGED IN"); print("[INFO] FETCHING EXISTING USER DETAILS"); // fetch _user again from post api. Map<String, dynamic> requestBody = { 'device_id': _deviceDetails['deviceID']! }; http.Response response = await http .post(Uri.parse(Constants.LOGIN_ENDPOINT), body: requestBody); _user = userFromJson(response.body); } _isLoading = false; notifyListeners(); } Future<void> _getDeviceDetails() async { final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin(); try { if (Platform.isAndroid) { AndroidDeviceInfo deviceInfo = await deviceInfoPlugin.androidInfo; _deviceDetails['deviceName'] = deviceInfo.model; _deviceDetails['deviceVersion'] = deviceInfo.version.toString(); _deviceDetails['deviceID'] = deviceInfo.androidId; } else if (Platform.isIOS) { IosDeviceInfo deviceInfo = await deviceInfoPlugin.iosInfo; _deviceDetails['deviceName'] = deviceInfo.name; _deviceDetails['deviceVersion'] = deviceInfo.systemVersion; _deviceDetails['deviceID'] = deviceInfo.identifierForVendor; } } on PlatformException { print("[INFO] Cannot get Device Information"); } notifyListeners(); } Future<void> _saveLoginDetails(Map<String, dynamic> deviceID) async { final SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setString(Constants.LOGGED_IN_USER, json.encode(deviceID)); } Future<void> _checkIsUserLoggedIn() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); _deviceDetails = json.decode(prefs.getString(Constants.LOGGED_IN_USER)!) as Map<String, dynamic>; notifyListeners(); } }
Editor is loading...