softplayer-web/lib/api/grpc/accounts.dart

59 lines
1.4 KiB
Dart
Raw Normal View History

2024-04-05 15:14:56 +00:00
import 'package:grpc/grpc_web.dart';
import 'package:softplayer_dart_proto/accounts/accounts_v1.pbgrpc.dart';
import 'package:softplayer_dart_proto/main.dart';
2024-04-05 15:57:46 +00:00
class AccountLocalData {
AccountLocalData({
required this.uuid,
required this.token,
});
String uuid;
String token;
}
2024-04-05 15:14:56 +00:00
class AccountsGrpc {
final GrpcWebClientChannel channel;
late AccountsClient accountsStub;
AccountsGrpc({
required this.channel,
});
void init() {
accountsStub = AccountsClient(channel);
}
2024-04-05 15:57:46 +00:00
Future<AccountLocalData> signIn(String username, String email, String password) async {
2024-04-05 15:14:56 +00:00
final request = AccountWithPassword(
data: AccountData(
name: username,
email: email,
),
password: AccountPassword(
password: password,
));
try {
final response = await accountsStub.signIn(request);
2024-04-05 15:57:46 +00:00
return AccountLocalData(uuid: response.id.id, token: response.token);
2024-04-05 15:14:56 +00:00
} catch (e) {
rethrow;
}
}
2024-04-05 15:57:46 +00:00
Future<AccountLocalData> signUp(String username, String email, String password) async {
2024-04-05 15:14:56 +00:00
final request = AccountWithPassword(
data: AccountData(
name: username,
email: email,
),
password: AccountPassword(
password: password,
));
try {
final response = await accountsStub.signUp(request);
2024-04-05 15:57:46 +00:00
return AccountLocalData(uuid: response.id.id, token: response.token);
2024-04-05 15:14:56 +00:00
} catch (e) {
rethrow;
}
}
}