I need you to help me know if what I am doing is correct.
Future _login(
BuildContext context, WidgetRef ref, web3auth.Provider provider) async {
setState(() {
_isLoading = true;
});
try {
final Web3AuthResponse response = await Web3AuthFlutter.login(
LoginParams(loginProvider: provider),
);
await _handleLoginResponse(context, ref, response);
} catch (e) {
_handleLoginError(context, e);
} finally {
setState(() {
_isLoading = false;
});
}
}
Future _handleLoginResponse(
BuildContext context, WidgetRef ref, Web3AuthResponse response) async {
try {
final userInfo = await Web3AuthFlutter.getUserInfo();
if (response.privKey != null) {
await ref.read(sessionProvider.notifier).setSession(
response.privKey!,
userInfo,
);
final session = ref.read(sessionProvider);
if (session != null && session.playerAddress.isNotEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Login successful. Address: ${session.playerAddress}')),
);
// Delay navigation to ensure the user sees the wallet address
await Future.delayed(Duration(seconds: 2));
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => HomePage(
privateKey: session.privateKey,
userInfo: session.userInfo!,
playerAddress: session.playerAddress,
),
),
);
} else {
throw Exception('Failed to obtain player address');
}
} else {
throw Exception('Private key is null');
}
} catch (e) {
print('Error in _handleLoginResponse: $e');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Login failed: $e')),
);
}
}
import ‘package:flutter_riverpod/flutter_riverpod.dart’;
import ‘package:shared_preferences/shared_preferences.dart’;
import ‘package:web3auth_flutter/output.dart’;
import ‘package:web3dart/web3dart.dart’;
import ‘dart:convert’;
class SessionModel {
final String privateKey;
final String playerAddress; // Changed from walletAddress to playerAddress
final TorusUserInfo? userInfo;
final String atonBalance;
final String ethBalance;
SessionModel({
required this.privateKey,
required this.playerAddress, // Changed from walletAddress to playerAddress
this.userInfo,
this.atonBalance = ‘0’,
this.ethBalance = ‘0’,
});
Map<String, dynamic> toJson() => {
‘privateKey’: privateKey,
‘playerAddress’:
playerAddress, // Changed from walletAddress to playerAddress
‘userInfo’: userInfo?.toJson(),
‘atonBalance’: atonBalance,
‘ethBalance’: ethBalance,
};
factory SessionModel.fromJson(Map<String, dynamic> json) => SessionModel(
privateKey: json[‘privateKey’],
playerAddress: json[
‘playerAddress’], // Changed from walletAddress to playerAddress
userInfo: json[‘userInfo’] != null
? TorusUserInfo.fromJson(json[‘userInfo’])
: null,
atonBalance: json[‘atonBalance’] ?? ‘0’,
ethBalance: json[‘ethBalance’] ?? ‘0’,
);
}
class SessionNotifier extends StateNotifier<SessionModel?> {
SessionNotifier() : super(null);
Future setSession(String privateKey, TorusUserInfo? userInfo) async {
final playerAddress = await _derivePlayerAddress(
privateKey); // Changed from _deriveWalletAddress to _derivePlayerAddress
final session = SessionModel(
privateKey: privateKey,
playerAddress:
playerAddress, // Changed from walletAddress to playerAddress
userInfo: userInfo,
);
state = session;
await _saveSession();
}
Future clearSession() async {
state = null;
await _clearSavedSession();
}
Future loadSession() async {
final prefs = await SharedPreferences.getInstance();
final sessionJson = prefs.getString(‘session’);
if (sessionJson != null) {
state = SessionModel.fromJson(Map<String, dynamic>.from(
json.decode(sessionJson) as Map,
));
}
}
Future updateBalances(String atonBalance, String ethBalance) async {
if (state != null) {
state = SessionModel(
privateKey: state!.privateKey,
playerAddress:
state!.playerAddress, // Changed from walletAddress to playerAddress
userInfo: state!.userInfo,
atonBalance: atonBalance,
ethBalance: ethBalance,
);
await _saveSession();
}
}
Future _derivePlayerAddress(String privateKey) async {
// Changed from _deriveWalletAddress to _derivePlayerAddress
final credentials = EthPrivateKey.fromHex(privateKey);
final address = await credentials.extractAddress();
return address.hexEip55;
}
Future _saveSession() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(‘session’, json.encode(state?.toJson()));
}
Future _clearSavedSession() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(‘session’);
}
String? get playerAddress =>
state?.playerAddress; // Changed from walletAddress to playerAddress
}