I am working on a project in which I have stored user data in Fire store with name, email and user role, here is the screenshot of this and user role is defined as basic.check it out
Now here my auth service code in flutter
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:http/http.dart';
class GSignInhelp {
static FirebaseAuth _auth = FirebaseAuth.instance;
static signInWithGoogle() async {
GoogleSignIn googleSignIn = GoogleSignIn();
final account = await googleSignIn.signIn();
final auth = await account.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: auth.accessToken,
idToken: auth.idToken,
);
final res = await _auth.signInWithCredential(credential);
return res.user;
}
static logOut(){
GoogleSignIn().signOut();
return _auth.signOut();
}
}
String role = 'basic';
class Usertype {
static FirebaseFirestore _db = FirebaseFirestore.instance;
static saveUser(User user) async {
Map<String,dynamic> userData = {
'name': user.displayName,
'email': user.email,
'role' : role,
};
final userRef = _db.collection('users').doc(user.uid);
if((await userRef.get()).exists){
get('role');
userData.update('role', (value) => role);
}else{
await userRef.set(userData);
}
}
}
and I want to update user role when user make a successful payment here I am using Razor pay and code for it is here
@override
void initState() {
super.initState();
_razorpay =Razorpay();
_razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess);
_razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, _handlePaymentError);
_razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, _handleExternalWallet);
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_razorpay.clear();
}
void openCheckout() async{
var option= {
"key": "rzp_test_rkbMfLcwVZtwyk",
'amount': _paymentamount*100,
'name': 'ghori fashion designer',
'description': 'expert bane',
'prefill' : {
'contact' : '1234567890',
'email' : 'anything@gmail.com',
},
'external' : {
'wallet' : ['paytm']
}
};
try{
_razorpay.open(option);
}
catch(e){
debugPrint(e);
}
}
void _handlePaymentSuccess(PaymentSuccessResponse response){
Fluttertoast.showToast(msg: "SUCCESS: "+ response.paymentId);
setState(() {
role = 'fullexpert';
});
}
void _handlePaymentError(PaymentFailureResponse response){
Fluttertoast.showToast(msg: "Error: "+ response.code.toString() + ' - ' + response.message);
}
void _handleExternalWallet(ExternalWalletResponse response){
Fluttertoast.showToast(msg: "External Wallet: "+ response.walletName);
}
and this is main activity code
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:gfd_official/Login_data/Login.dart';
import 'package:gfd_official/Login_data/gSignin_data.dart';
import 'package:gfd_official/Mainhome.dart';
import 'package:gfd_official/paid_screens/Expert1year.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ghori fashion designer',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MySplashScreen(),
);
}
}
class MySplashScreen extends StatefulWidget {
@override
_MySplashScreenState createState() => _MySplashScreenState();
}
// ignore: camel_case_types
class _MySplashScreenState extends State<MySplashScreen> {
@override
void initState() {
// TODO: implement initState
super.initState();
Future.delayed(
Duration(
seconds: 3,
),
() {});
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data != null) {
Usertype.saveUser(snapshot.data);
return StreamBuilder(
stream: FirebaseFirestore.instance.collection('users').doc(
snapshot.data.uid).snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData && snapshot.data != null) {
final user = snapshot.data.data();
if(user['role'] == 'fullexpert'){
return Expert1year();
}else{
return Mainhome();
}
}
return Material(
child: Center(child: CircularProgressIndicator(),),);
});
}
return Loginpage();
},
);
}
}
when I run the app and make payment after success payment nothing happens and when I logout and login again role changes in Firestore but I have noticed some weird when I have made payment with an account and logged out and I just signed in with different account role for user who just logged in have changed but not for user who have made payment.
One more issue I got that the role changes for only onw time next time user login in the role again become basic instead of fullexpert
I have google searched it but due big change in firebase with flutter in 2020 all methods are not applicable.
Please if someone can help me to how can I change user role with user unique id or UID and this change remain until user upgrade role or downgrade role and it should be for specific user.