How to change password using Firebase in Flutter

前端 未结 4 1877
一向
一向 2021-01-24 06:46

I want to change current user password using Firebase in Flutter. Can any one help me on how to implement change password method?

4条回答
  •  借酒劲吻你
    2021-01-24 06:57

    As @Gunter mentioned that the feature is currently still unavailable, you can make use of the firebase REST API way of changing the password for the time being.

    import 'package:http/http.dart' as http;
    import 'dart:convert';
    import 'dart:async';
    
    Future changePassword(String newPassword) async {
      const String API_KEY = 'YOUR_API_KEY';
      final String changePasswordUrl =
          'https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=$API_KEY';
    
          final String idToken = await user.getIdToken(); // where user is FirebaseUser user
    
        final Map payload = {
          'email': idToken,
          'password': newPassword,
          'returnSecureToken': true
        };
    
      await http.post(changePasswordUrl, 
        body: json.encode(payload), 
        headers: {'Content-Type': 'application/json'},  
      )
    }
    

    You can get the idToken by using the getIdToken() method on the FirebaseUser object

    You can get the firebase api key under the project setting in your console

提交回复
热议问题