flutter firebase aggiorna automaticamente la sessione utente con refreshToken

Aug 23 2020

Voglio che l'utente nella mia app rimanga connesso. Sto usando l'autenticazione Firebase con IDToken che dura 1 ora fino alla scadenza. Voglio aggiornare automaticamente la sessione ogni volta che sta per scadere.

quello che ho letto finora qui https://firebase.google.com/docs/reference/rest/auth/#section-refresh-token dovrebbe essere in qualche modo possibile con https://securetoken.googleapis.com/v1/token?key=[API_KEY]

Questo è il mio codice completo per l'autenticazione in questo momento (flutter)

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../provider/http_exception.dart';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';

class Auth with ChangeNotifier {
  String _token;
  DateTime _expiryDate;
  String _userId;
  Timer _authTimer;
  bool wasLoggedOut = false;
  bool onBoarding = false;

  Future<void> createUser(String email, String firstName, String lastName) async {
    final url = 'https://test45.firebaseio.com/users/$userId.json?auth=$token';
    final response = await http.put(url, body: json.encode({
      'userEmail': email,
      'userIsArtist': false,
      'userFirstName': firstName,
      'userLastName': lastName,
    }));
    print('post ist done');
    print(json.decode(response.body));
  }

  bool get isAuth {
    return token != null;
  }

  String get userId {
    return _userId;
  }

  String get token {
    if (_expiryDate != null &&
        _expiryDate.isAfter(DateTime.now()) &&
        _token != null) {
      return _token;
    }
    return null;
  }

  Future<void> authenticate(
      String email, String password, String urlSegement) async {
    final url = 'https://identitytoolkit.googleapis.com/v1/accounts:$urlSegement?key=AIzaSyD8pb3M325252dfsDC-4535dfd';

    try {
      final response = await http.post(url,
          body: json.encode({
            'email': email,
            'password': password,
            'returnSecureToken': true,
          }));
      final responseData = json.decode(response.body);
      if (responseData['error'] != null) {
        throw HttpException(responseData['error']['message']);
      }
      _token = responseData['idToken'];
      _userId = responseData['localId'];
      _expiryDate = DateTime.now().add(Duration(seconds: int.parse(responseData['expiresIn'])));
      _autoLogout();
     
      notifyListeners();

      final prefs = await SharedPreferences.getInstance();
      final userData = json.encode({
        'token': _token,
        'userId': _userId,
        'expiryDate': _expiryDate.toIso8601String(),
      });
      prefs.setString('userData', userData);
    } catch (error) {
      throw error;
    }
  }

  Future<void> signup(String email, String password) async {
    return authenticate(email, password, 'signUp');
  }

  Future<void> signin(String email, String password) async {
    return authenticate(email, password, 'signInWithPassword');
  }

  Future<bool> tryAutoLogin() async {
    final prefs = await SharedPreferences.getInstance();
    if(!prefs.containsKey('userData')){
      return false;
    }
    final extractedUserData = json.decode(prefs.getString('userData')) as Map<String, Object>;
    final expiryDate = DateTime.parse(extractedUserData['expiryDate']);

    if(expiryDate.isBefore(DateTime.now())) {
      return false;
    }

    _token = extractedUserData['token'];
    _userId = extractedUserData['userId'];
    _expiryDate = expiryDate;

    notifyListeners();
    _autoLogout();
    return true;
  }


  Future<void> logout() async {
    _token = null;
    _userId = null;
    _expiryDate = null;
    if(_authTimer != null){
      _authTimer.cancel();
      _authTimer = null;
    }
    notifyListeners();
    final prefs = await SharedPreferences.getInstance();
    prefs.remove('userData');
  }

  void _autoLogout() {
    if(_authTimer != null) {
      _authTimer.cancel();
    }
  final timetoExpiry =  _expiryDate.difference(DateTime.now()).inSeconds;
    _authTimer = Timer(Duration(seconds: timetoExpiry), logout);
  }
}

come modificare il mio auth.dartper ottenere l'aggiornamento automatico?

MODIFICARE:

Come accennato nei commenti, sto lavorando con provider in cui ho le seguenti funzioni per recuperare il token:

update(String token, id, List<items> itemsList) {
    authToken = token;
    userId = id;
  }

anche in tutte le mie chiamate API sto già utilizzando il parametro auth:

var url = 'https://test45.firebaseio.com/folder/$inside/$ym.json?auth=$authToken';

Ho solo bisogno di qualcuno che mi mostri come modificare il mio codice con il token di aggiornamento.

Grazie in anticipo!

MODIFICARE:

Ho provato a implementarlo, ma sto ottenendo un ciclo infinito, per favore aiuto:

String get token {
    if (_expiryDate != null &&
        _expiryDate.isAfter(DateTime.now()) &&
        _token != null) {
      return _token;
    }
    refreshSession();
  }

  Future<void> refreshSession() async {
        final url = 'https://securetoken.googleapis.com/v1/token?key=5437fdjskfsdk38438?grant_type=refresh_token?auth=$token';
      
      try {
      final response = await http.post(url,
          body: json.encode({
  'token_type': 'Bearer',
          }));
      final responseData = json.decode(response.body);
      if (responseData['error'] != null) {
        throw HttpException(responseData['error']['message']);
      }
      _token = responseData['id_token'];
      _userId = responseData['user_id'];
      _expiryDate = DateTime.now().add(Duration(seconds: int.parse(responseData['expires_in'])));
      _autoLogout();
     
      notifyListeners();

      final prefs = await SharedPreferences.getInstance();
      final userData = json.encode({
        'token': _token,
        'userId': _userId,
        'expiryDate': _expiryDate.toIso8601String(),
      });
      prefs.setString('userData', userData);
    } catch (error) {
      throw error;
    }
      }

Risposte

2 FurkanKURT Sep 19 2020 at 15:58

Ho modificato la tua refresh_token()funzione.

In primo luogo, dovresti usare la tua chiave API web sul tuo progetto Firebase con il link. Dovresti anche salvare il token di aggiornamento. E se pubblichi in questo modo, funzionerà. Se non funziona, prova senza json.encode()funzione sul tuo corpo mentre mi impegno.

Future<void> refreshSession() async {
  final url =
      'https://securetoken.googleapis.com/v1/token?key=$WEB_API_KEY'; //$WEB_API_KEY=> You should write your web api key on your firebase project.
  
  try {
    final response = await http.post(
      url,
      headers: {
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded"
      },
      body: json.encode({
        'grant_type': 'refresh_token',
        'refresh_token': '[REFRESH_TOKEN]', // Your refresh token.
      }),
      // Or try without json.encode.
      // Like this:
      // body: {
      //   'grant_type': 'refresh_token',
      //   'refresh_token': '[REFRESH_TOKEN]',
      // },
    );
    final responseData = json.decode(response.body);
    if (responseData['error'] != null) {
      throw HttpException(responseData['error']['message']);
    }
    _token = responseData['id_token'];
    _refresh_token = responseData['refresh_token']; // Also save your refresh token
    _userId = responseData['user_id'];
    _expiryDate = DateTime.now()
        .add(Duration(seconds: int.parse(responseData['expires_in'])));
    _autoLogout();

    notifyListeners();

    final prefs = await SharedPreferences.getInstance();
    final userData = json.encode({
      'token': _token,
      'refresh_token': _refresh_token,
      'userId': _userId,
      'expiryDate': _expiryDate.toIso8601String(),
    });
    prefs.setString('userData', userData);
  } catch (error) {
    throw error;
  }
}

Questo è il tuo file auth.dart completo che ho modificato.

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../provider/http_exception.dart';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';

class Auth with ChangeNotifier {
  String _token;
  String _refresh_token;
  DateTime _expiryDate;
  String _userId;
  Timer _authTimer;
  bool wasLoggedOut = false;
  bool onBoarding = false;

  Future<void> createUser(String email, String firstName, String lastName) async {
    final url = 'https://test45.firebaseio.com/users/$userId.json?auth=$token';
    final response = await http.put(url, body: json.encode({
      'userEmail': email,
      'userIsArtist': false,
      'userFirstName': firstName,
      'userLastName': lastName,
    }));
    print('post ist done');
    print(json.decode(response.body));
  }

  bool get isAuth {
    return token != null;
  }

  String get userId {
    return _userId;
  }

  String get token {
    if (_expiryDate != null &&
        _expiryDate.isAfter(DateTime.now()) &&
        _token != null && _refresh_token!=null) {
      return _token;
    }
    refreshSession();
    return null;
  }

  Future<void> authenticate(
      String email, String password, String urlSegement) async {
    final url = 'https://identitytoolkit.googleapis.com/v1/accounts:$urlSegement?key=AIzaSyD8pb3M325252dfsDC-4535dfd'; try { final response = await http.post(url, body: json.encode({ 'email': email, 'password': password, 'returnSecureToken': true, })); final responseData = json.decode(response.body); if (responseData['error'] != null) { throw HttpException(responseData['error']['message']); } _token = responseData['idToken']; _refresh_token = responseData['refreshToken']; _userId = responseData['localId']; _expiryDate = DateTime.now().add(Duration(seconds: int.parse(responseData['expiresIn']))); _autoLogout(); notifyListeners(); final prefs = await SharedPreferences.getInstance(); final userData = json.encode({ 'token': _token, 'refresh_token': _refresh_token, 'userId': _userId, 'expiryDate': _expiryDate.toIso8601String(), }); prefs.setString('userData', userData); } catch (error) { throw error; } } Future<void> signup(String email, String password) async { return authenticate(email, password, 'signUp'); } Future<void> signin(String email, String password) async { return authenticate(email, password, 'signInWithPassword'); } Future<bool> tryAutoLogin() async { final prefs = await SharedPreferences.getInstance(); if(!prefs.containsKey('userData')){ return false; } final extractedUserData = json.decode(prefs.getString('userData')) as Map<String, Object>; final expiryDate = DateTime.parse(extractedUserData['expiryDate']); if(expiryDate.isBefore(DateTime.now())) { return false; } _token = extractedUserData['token']; _refresh_token = extractedUserData['refresh_token']; _userId = extractedUserData['userId']; _expiryDate = expiryDate; notifyListeners(); _autoLogout(); return true; } Future<void> logout() async { _token = null; _refresh_token = null; _userId = null; _expiryDate = null; if(_authTimer != null){ _authTimer.cancel(); _authTimer = null; } notifyListeners(); final prefs = await SharedPreferences.getInstance(); prefs.remove('userData'); } void _autoLogout() { if(_authTimer != null) { _authTimer.cancel(); } final timetoExpiry = _expiryDate.difference(DateTime.now()).inSeconds; _authTimer = Timer(Duration(seconds: timetoExpiry), logout); } Future<void> refreshSession() async { final url = 'https://securetoken.googleapis.com/v1/token?key=$WEB_API_KEY'; 
  //$WEB_API_KEY=> You should write your web api key on your firebase project.
  
  try {
    final response = await http.post(
      url,
      headers: {
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded"
      },
      body: json.encode({
        'grant_type': 'refresh_token',
        'refresh_token': '[REFRESH_TOKEN]', // Your refresh token.
      }),
      // Or try without json.encode.
      // Like this:
      // body: {
      //   'grant_type': 'refresh_token',
      //   'refresh_token': '[REFRESH_TOKEN]',
      // },
    );
    final responseData = json.decode(response.body);
    if (responseData['error'] != null) {
      throw HttpException(responseData['error']['message']);
    }
    _token = responseData['id_token'];
    _refresh_token = responseData['refresh_token']; // Also save your refresh token
    _userId = responseData['user_id'];
    _expiryDate = DateTime.now()
        .add(Duration(seconds: int.parse(responseData['expires_in'])));
    _autoLogout();

    notifyListeners();

    final prefs = await SharedPreferences.getInstance();
    final userData = json.encode({
      'token': _token,
      'refresh_token': _refresh_token,
      'userId': _userId,
      'expiryDate': _expiryDate.toIso8601String(),
    });
    prefs.setString('userData', userData);
  } catch (error) {
    throw error;
  }
}
}
2 Rod Aug 25 2020 at 21:35

È necessario salvare il token di aggiornamento.
Segui questo argomento per aggiornare il tuo IDToken utilizzando il token di aggiornamento:https://firebase.google.com/docs/reference/rest/auth#section-refresh-token

Quando si effettuano chiamate all'API, utilizzare una funzione per recuperare IDToken. Questa funzione deve verificare se l'IDToken corrente è ancora valido e, in caso contrario, richiederne uno nuovo (utilizzando il link fornito).

HưngTrịnh Sep 01 2020 at 11:20

Penso che la libreria Dio sia giusta per te

dio = Dio();
dio.options.baseUrl = URL_API_PROD;
dio.interceptors.add(InterceptorsWrapper(
  onRequest: (Options option) async{
    
    //getToken() : you can check token expires and renew in this function
    await getToken().then((result) {
      token = result;
    });
    option.headers = {
      "Authorization": "Bearer $token"
    };
  }
));

Response response = await dio.get('/api/users');