Update the env proto and fix auth
This commit is contained in:
17
lib/api/grpc/creds.dart
Normal file
17
lib/api/grpc/creds.dart
Normal file
@ -0,0 +1,17 @@
|
||||
import 'dart:html';
|
||||
|
||||
class SoftplayerCreds {
|
||||
final String uuid;
|
||||
final String token;
|
||||
|
||||
SoftplayerCreds({
|
||||
required this.token,
|
||||
required this.uuid,
|
||||
});
|
||||
}
|
||||
|
||||
class SoftplayerCredsHelpers {
|
||||
String localStorageEntry(String key) => window.localStorage[key]!;
|
||||
SoftplayerCreds fromLocalStorage() => SoftplayerCreds(
|
||||
token: localStorageEntry("token"), uuid: localStorageEntry("uuid"));
|
||||
}
|
@ -1,37 +1,52 @@
|
||||
import 'dart:html';
|
||||
|
||||
import 'package:grpc/grpc_web.dart';
|
||||
import 'package:softplayer_dart_proto/main.dart';
|
||||
import 'package:softplayer_web/api/grpc/creds.dart';
|
||||
|
||||
class EnvironmentLocalData {
|
||||
EnvironmentLocalData({
|
||||
required this.uuid,
|
||||
required this.token,
|
||||
});
|
||||
|
||||
String uuid;
|
||||
String token;
|
||||
}
|
||||
|
||||
class EnvironmentsGrpc {
|
||||
final GrpcWebClientChannel channel;
|
||||
late EnvironmentsClient envStub;
|
||||
EnvironmentsGrpc({
|
||||
required this.channel,
|
||||
});
|
||||
// final GrpcWebClientChannel channel;
|
||||
final EnvironmentsClient envStub;
|
||||
|
||||
void init() {
|
||||
envStub = EnvironmentsClient(channel);
|
||||
// Init the grpc channel for environments
|
||||
EnvironmentsGrpc(channel) : envStub = EnvironmentsClient(channel);
|
||||
|
||||
// Get environments from the API
|
||||
Future<EnvironmentFull> get(String name, SoftplayerCreds creds) async {
|
||||
final request = GetOptions(
|
||||
name: EnvironmentName(name: name),
|
||||
ownerId: OwnerId(uuid: creds.uuid),
|
||||
token: Token(token: creds.token),
|
||||
);
|
||||
|
||||
try {
|
||||
final response = await envStub.get(request);
|
||||
return response;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<String>> list() async* {
|
||||
List<String> envs = [];
|
||||
Stream<List<EnvironmentFull>> list(SoftplayerCreds creds) async* {
|
||||
List<EnvironmentFull> envs = [];
|
||||
try {
|
||||
await for (var feature in envStub.list(Empty(),
|
||||
options: CallOptions(metadata: {
|
||||
"uuid": window.localStorage["uuid"]!,
|
||||
"token": window.localStorage["token"]!,
|
||||
}))) {
|
||||
envs.add(feature.data.name);
|
||||
await for (var feature in envStub.list(
|
||||
ListOptions(
|
||||
ownerId: OwnerId(uuid: creds.uuid),
|
||||
token: Token(token: creds.token),
|
||||
),
|
||||
)) {
|
||||
envs.add(EnvironmentFull(
|
||||
data: feature.data,
|
||||
name: feature.name,
|
||||
));
|
||||
}
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
|
24
lib/components/environment_card.dart
Normal file
24
lib/components/environment_card.dart
Normal file
@ -0,0 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class EnvirnomentCard extends StatelessWidget {
|
||||
final String name;
|
||||
|
||||
const EnvirnomentCard({
|
||||
super.key,
|
||||
required this.name,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: Column(
|
||||
children: [
|
||||
Text(name),
|
||||
Row(
|
||||
children: [Text(name)],
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,15 +1,14 @@
|
||||
import 'dart:js_interop';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:grpc/grpc_web.dart';
|
||||
import 'package:softplayer_web/api/grpc/creds.dart';
|
||||
import 'package:softplayer_web/api/grpc/environments.dart';
|
||||
import 'package:softplayer_web/components/environment_card.dart';
|
||||
|
||||
class EnvirnomentList extends StatefulWidget {
|
||||
EnvirnomentList({
|
||||
const EnvirnomentList({
|
||||
super.key,
|
||||
required this.channel,
|
||||
});
|
||||
|
||||
final GrpcWebClientChannel channel;
|
||||
@override
|
||||
State<StatefulWidget> createState() => _EnvirnomentListState();
|
||||
@ -21,8 +20,7 @@ class _EnvirnomentListState extends State<EnvirnomentList> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
envGrpc = EnvironmentsGrpc(channel: widget.channel);
|
||||
envGrpc.init();
|
||||
envGrpc = EnvironmentsGrpc(widget.channel);
|
||||
}
|
||||
|
||||
@override
|
||||
@ -30,20 +28,24 @@ class _EnvirnomentListState extends State<EnvirnomentList> {
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: StreamBuilder(
|
||||
stream: envGrpc.list(),
|
||||
stream:
|
||||
envGrpc.list(SoftplayerCredsHelpers().fromLocalStorage()),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return CircularProgressIndicator();
|
||||
return const CircularProgressIndicator();
|
||||
} else if (snapshot.connectionState == ConnectionState.done) {
|
||||
if (snapshot.hasError) {
|
||||
return Text('Error!');
|
||||
return const Text('Error!');
|
||||
} else {
|
||||
return Column(
|
||||
children: snapshot.data!.map((e) => Text(e)).toList(),
|
||||
return GridView.count(
|
||||
crossAxisCount: 4,
|
||||
children: snapshot.data!
|
||||
.map((e) => EnvirnomentCard(name: e.name.name))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
return Text("err");
|
||||
return const Text("err");
|
||||
})));
|
||||
}
|
||||
}
|
||||
|
@ -8,8 +8,9 @@ class LoginForm extends StatefulWidget {
|
||||
const LoginForm({
|
||||
super.key,
|
||||
required this.grpcChannel,
|
||||
required this.notifyParent,
|
||||
});
|
||||
|
||||
final Function() notifyParent;
|
||||
final GrpcWebClientChannel grpcChannel;
|
||||
@override
|
||||
State<StatefulWidget> createState() => _LoginFormState();
|
||||
@ -38,6 +39,7 @@ class _LoginFormState extends State<LoginForm> {
|
||||
accountsGrpc.signUp(username, email, password).then((rs) {
|
||||
window.localStorage["token"] = rs.token;
|
||||
window.localStorage["uuid"] = rs.uuid;
|
||||
widget.notifyParent();
|
||||
Navigator.of(context, rootNavigator: true).pop();
|
||||
}).catchError((e) {
|
||||
GrpcError error = e;
|
||||
@ -66,7 +68,8 @@ class _LoginFormState extends State<LoginForm> {
|
||||
accountsGrpc.signIn(username, "", password).then((rs) {
|
||||
window.localStorage["token"] = rs.token;
|
||||
window.localStorage["uuid"] = rs.uuid;
|
||||
Navigator.of(context, rootNavigator: true).pop();
|
||||
widget.notifyParent();
|
||||
// Navigator.of(context, rootNavigator: true).pop();
|
||||
}).catchError((e) {
|
||||
print(e);
|
||||
GrpcError error = e;
|
||||
|
@ -27,7 +27,7 @@ class MyApp extends StatelessWidget {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: 'Softplayer',
|
||||
home: TestAlert(channel: channel),
|
||||
home: RootWidget(channel: channel),
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
@ -36,10 +36,22 @@ class MyApp extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class TestAlert extends StatelessWidget {
|
||||
class RootWidget extends StatefulWidget {
|
||||
final GrpcWebClientChannel channel;
|
||||
TestAlert({super.key, required this.channel});
|
||||
|
||||
RootWidget({super.key, required this.channel});
|
||||
late final AccountsGrpc accountsGrpc = AccountsGrpc(channel: channel);
|
||||
|
||||
@override
|
||||
@override
|
||||
State<StatefulWidget> createState() => _StateRootWidget();
|
||||
}
|
||||
|
||||
class _StateRootWidget extends State<RootWidget> {
|
||||
refresh() {
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
bool isSignedIn() {
|
||||
return window.localStorage.containsKey("token");
|
||||
}
|
||||
@ -47,20 +59,34 @@ class TestAlert extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!isSignedIn()) {
|
||||
return Scaffold(
|
||||
body: Container(
|
||||
return Scaffold(
|
||||
body: Container(
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage("assets/login_background.jpg"),
|
||||
fit: BoxFit.fill,
|
||||
),
|
||||
),
|
||||
child: LoginForm(grpcChannel: channel),
|
||||
child: LoginForm(
|
||||
grpcChannel: widget.channel,
|
||||
notifyParent: refresh,
|
||||
),
|
||||
));
|
||||
} else {
|
||||
return Scaffold(
|
||||
body: EnvirnomentList(channel: channel),
|
||||
appBar: AppBar(),
|
||||
body: EnvirnomentList(channel: widget.channel),
|
||||
appBar: AppBar(
|
||||
title: const Text("Softplayer"),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
window.localStorage.remove("token");
|
||||
window.localStorage.remove("uuid");
|
||||
refresh();
|
||||
},
|
||||
child: const Text("sign out"))
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () => print("1"),
|
||||
),
|
||||
|
Reference in New Issue
Block a user