<commit_before>import 'dart:convert';
import 'dart:io';

import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/widgets.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fivekmrun_flutter/constants.dart' as constants;

class AuthenticationResource extends ChangeNotifier {
  String _token;
  int _userId;

  Future<bool> authenticate(String username, String password) async {
    FirebaseCrashlytics.instance.log("authenticate username - $username");

    HttpClient httpClient = new HttpClient();
    HttpClientRequest request =
        await httpClient.postUrl(Uri.parse("https://5kmrun.bg/api/auth"));
    request.headers.set('content-type', 'application/x-www-form-urlencoded');
    request.write("userEmail=${username}&userPassword=${password}");

    HttpClientResponse response = await request.close();
    String reply = await response.transform(utf8.decoder).join();
    print(reply);
    httpClient.close();
    Map<String, dynamic> map = json.decode(reply);
    List<String> errors = List.from(map["errors"]);

    if (errors.isEmpty) {
      final token = map["answer"]["tkn"];
      final userId = map["answer"]["id"];

      if (token != null && token != "" && userId != null) {
        await this._onLoginSuccess(userId, token);

        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }

  Future<bool> authenticateWithUserId(int userId) async {
    FirebaseCrashlytics.instance.log("authenticate userID - $userId");

    await this._onLoginSuccess(userId, null);
    return true;
  }

  String getToken() {
    return this._token;
  }

  int getUserId() {
    return this._userId;
  }

  bool isLoggedIn() {
    return this._token != null && this._userId != null;
  }

  void _setUserId(int userId) {
    this._userId = userId;
    FirebaseCrashlytics.instance.setUserIdentifier(userId?.toString());
  }

  void _setToken(String token) {
    this._token = token;
    FirebaseCrashlytics.instance.setCustomKey("hasToken", token != null && token != "");
  }

  Future<void> logout() async {
    FirebaseCrashlytics.instance.log("logout() started");
    this._setUserId(null);
    this._setToken(null);

    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.remove(constants.key_userId);
    await prefs.remove(constants.key_token);
    await prefs.remove(constants.key_tokenTimestamp);
    FirebaseCrashlytics.instance.log("logout() completed");
  }

  Future<void> _onLoginSuccess(int userId, String token) async {
    this._setUserId(userId);
    this._setToken(token);

    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt(constants.key_userId, userId);

    if (token != null) {
      final timestamp = DateTime.now().millisecondsSinceEpoch;
      await prefs.setString(constants.key_token, token);
      await prefs.setInt(constants.key_tokenTimestamp, timestamp);<commit_msg>fix: crash with new FIrebase API<commit_after>import 'dart:convert';
import 'dart:io';

import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/widgets.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:fivekmrun_flutter/constants.dart' as constants;

class AuthenticationResource extends ChangeNotifier {
  String _token;
  int _userId;

  Future<bool> authenticate(String username, String password) async {
    FirebaseCrashlytics.instance.log("authenticate username - $username");

    HttpClient httpClient = new HttpClient();
    HttpClientRequest request =
        await httpClient.postUrl(Uri.parse("https://5kmrun.bg/api/auth"));
    request.headers.set('content-type', 'application/x-www-form-urlencoded');
    request.write("userEmail=${username}&userPassword=${password}");

    HttpClientResponse response = await request.close();
    String reply = await response.transform(utf8.decoder).join();
    print(reply);
    httpClient.close();
    Map<String, dynamic> map = json.decode(reply);
    List<String> errors = List.from(map["errors"]);

    if (errors.isEmpty) {
      final token = map["answer"]["tkn"];
      final userId = map["answer"]["id"];

      if (token != null && token != "" && userId != null) {
        await this._onLoginSuccess(userId, token);

        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }

  Future<bool> authenticateWithUserId(int userId) async {
    FirebaseCrashlytics.instance.log("authenticate userID - $userId");

    await this._onLoginSuccess(userId, null);
    return true;
  }

  String getToken() {
    return this._token;
  }

  int getUserId() {
    return this._userId;
  }

  bool isLoggedIn() {
    return this._token != null && this._userId != null;
  }

  void _setUserId(int userId) {
    this._userId = userId;
    FirebaseCrashlytics.instance.setUserIdentifier(userId?.toString() ?? "");
  }

  void _setToken(String token) {
    this._token = token;
    FirebaseCrashlytics.instance.setCustomKey("hasToken", token != null && token != "");
  }

  Future<void> logout() async {
    FirebaseCrashlytics.instance.log("logout() started");
    this._setUserId(null);
    this._setToken(null);

    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.remove(constants.key_userId);
    await prefs.remove(constants.key_token);
    await prefs.remove(constants.key_tokenTimestamp);
    FirebaseCrashlytics.instance.log("logout() completed");
  }

  Future<void> _onLoginSuccess(int userId, String token) async {
    this._setUserId(userId);
    this._setToken(token);

    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt(constants.key_userId, userId);

    if (token != null) {
      final timestamp = DateTime.now().millisecondsSinceEpoch;
      await prefs.setString(constants.key_token, token);
      await prefs.setInt(constants.key_tokenTimestamp, timestamp);
{'fraction_non_alphanumeric': 0.12827356130108425, 'fraction_numerical': 0.0006672226855713094, 'mean_word_length': 4.02177554438861, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7247865', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1841, 'n_words': 348}