Update the env proto and fix auth

This commit is contained in:
Nikolai Rodionov 2024-04-29 12:42:16 +02:00
parent ad259dc553
commit ddc70e5854
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
7 changed files with 138 additions and 75 deletions

17
lib/api/grpc/creds.dart Normal file
View 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"));
}

View File

@ -1,37 +1,52 @@
import 'dart:html';
import 'package:grpc/grpc_web.dart';
import 'package:softplayer_dart_proto/main.dart'; import 'package:softplayer_dart_proto/main.dart';
import 'package:softplayer_web/api/grpc/creds.dart';
class EnvironmentLocalData { class EnvironmentLocalData {
EnvironmentLocalData({ EnvironmentLocalData({
required this.uuid, required this.uuid,
required this.token, required this.token,
}); });
String uuid; String uuid;
String token; String token;
} }
class EnvironmentsGrpc { class EnvironmentsGrpc {
final GrpcWebClientChannel channel; // final GrpcWebClientChannel channel;
late EnvironmentsClient envStub; final EnvironmentsClient envStub;
EnvironmentsGrpc({
required this.channel,
});
void init() { // Init the grpc channel for environments
envStub = EnvironmentsClient(channel); 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* { Stream<List<EnvironmentFull>> list(SoftplayerCreds creds) async* {
List<String> envs = []; List<EnvironmentFull> envs = [];
try { try {
await for (var feature in envStub.list(Empty(), await for (var feature in envStub.list(
options: CallOptions(metadata: { ListOptions(
"uuid": window.localStorage["uuid"]!, ownerId: OwnerId(uuid: creds.uuid),
"token": window.localStorage["token"]!, token: Token(token: creds.token),
}))) { ),
envs.add(feature.data.name); )) {
envs.add(EnvironmentFull(
data: feature.data,
name: feature.name,
));
} }
} catch (e) { } catch (e) {
rethrow; rethrow;

View 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)],
)
],
),
);
}
}

View File

@ -1,15 +1,14 @@
import 'dart:js_interop';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:grpc/grpc_web.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/api/grpc/environments.dart';
import 'package:softplayer_web/components/environment_card.dart';
class EnvirnomentList extends StatefulWidget { class EnvirnomentList extends StatefulWidget {
EnvirnomentList({ const EnvirnomentList({
super.key, super.key,
required this.channel, required this.channel,
}); });
final GrpcWebClientChannel channel; final GrpcWebClientChannel channel;
@override @override
State<StatefulWidget> createState() => _EnvirnomentListState(); State<StatefulWidget> createState() => _EnvirnomentListState();
@ -21,8 +20,7 @@ class _EnvirnomentListState extends State<EnvirnomentList> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
envGrpc = EnvironmentsGrpc(channel: widget.channel); envGrpc = EnvironmentsGrpc(widget.channel);
envGrpc.init();
} }
@override @override
@ -30,20 +28,24 @@ class _EnvirnomentListState extends State<EnvirnomentList> {
return Scaffold( return Scaffold(
body: SafeArea( body: SafeArea(
child: StreamBuilder( child: StreamBuilder(
stream: envGrpc.list(), stream:
envGrpc.list(SoftplayerCredsHelpers().fromLocalStorage()),
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) { if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator(); return const CircularProgressIndicator();
} else if (snapshot.connectionState == ConnectionState.done) { } else if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) { if (snapshot.hasError) {
return Text('Error!'); return const Text('Error!');
} else { } else {
return Column( return GridView.count(
children: snapshot.data!.map((e) => Text(e)).toList(), crossAxisCount: 4,
children: snapshot.data!
.map((e) => EnvirnomentCard(name: e.name.name))
.toList(),
); );
} }
} }
return Text("err"); return const Text("err");
}))); })));
} }
} }

View File

@ -8,8 +8,9 @@ class LoginForm extends StatefulWidget {
const LoginForm({ const LoginForm({
super.key, super.key,
required this.grpcChannel, required this.grpcChannel,
required this.notifyParent,
}); });
final Function() notifyParent;
final GrpcWebClientChannel grpcChannel; final GrpcWebClientChannel grpcChannel;
@override @override
State<StatefulWidget> createState() => _LoginFormState(); State<StatefulWidget> createState() => _LoginFormState();
@ -38,6 +39,7 @@ class _LoginFormState extends State<LoginForm> {
accountsGrpc.signUp(username, email, password).then((rs) { accountsGrpc.signUp(username, email, password).then((rs) {
window.localStorage["token"] = rs.token; window.localStorage["token"] = rs.token;
window.localStorage["uuid"] = rs.uuid; window.localStorage["uuid"] = rs.uuid;
widget.notifyParent();
Navigator.of(context, rootNavigator: true).pop(); Navigator.of(context, rootNavigator: true).pop();
}).catchError((e) { }).catchError((e) {
GrpcError error = e; GrpcError error = e;
@ -66,7 +68,8 @@ class _LoginFormState extends State<LoginForm> {
accountsGrpc.signIn(username, "", password).then((rs) { accountsGrpc.signIn(username, "", password).then((rs) {
window.localStorage["token"] = rs.token; window.localStorage["token"] = rs.token;
window.localStorage["uuid"] = rs.uuid; window.localStorage["uuid"] = rs.uuid;
Navigator.of(context, rootNavigator: true).pop(); widget.notifyParent();
// Navigator.of(context, rootNavigator: true).pop();
}).catchError((e) { }).catchError((e) {
print(e); print(e);
GrpcError error = e; GrpcError error = e;

View File

@ -27,7 +27,7 @@ class MyApp extends StatelessWidget {
return MaterialApp( return MaterialApp(
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
title: 'Softplayer', title: 'Softplayer',
home: TestAlert(channel: channel), home: RootWidget(channel: channel),
theme: ThemeData( theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true, useMaterial3: true,
@ -36,10 +36,22 @@ class MyApp extends StatelessWidget {
} }
} }
class TestAlert extends StatelessWidget { class RootWidget extends StatefulWidget {
final GrpcWebClientChannel channel; final GrpcWebClientChannel channel;
TestAlert({super.key, required this.channel});
RootWidget({super.key, required this.channel});
late final AccountsGrpc accountsGrpc = AccountsGrpc(channel: channel); late final AccountsGrpc accountsGrpc = AccountsGrpc(channel: channel);
@override
@override
State<StatefulWidget> createState() => _StateRootWidget();
}
class _StateRootWidget extends State<RootWidget> {
refresh() {
setState(() {});
}
bool isSignedIn() { bool isSignedIn() {
return window.localStorage.containsKey("token"); return window.localStorage.containsKey("token");
} }
@ -48,19 +60,33 @@ class TestAlert extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (!isSignedIn()) { if (!isSignedIn()) {
return Scaffold( return Scaffold(
body: Container( body: Container(
decoration: const BoxDecoration( decoration: const BoxDecoration(
image: DecorationImage( image: DecorationImage(
image: AssetImage("assets/login_background.jpg"), image: AssetImage("assets/login_background.jpg"),
fit: BoxFit.fill, fit: BoxFit.fill,
), ),
), ),
child: LoginForm(grpcChannel: channel), child: LoginForm(
grpcChannel: widget.channel,
notifyParent: refresh,
),
)); ));
} else { } else {
return Scaffold( return Scaffold(
body: EnvirnomentList(channel: channel), body: EnvirnomentList(channel: widget.channel),
appBar: AppBar(), 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( floatingActionButton: FloatingActionButton(
onPressed: () => print("1"), onPressed: () => print("1"),
), ),

View File

@ -5,18 +5,18 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: archive name: archive
sha256: "22600aa1e926be775fa5fe7e6894e7fb3df9efda8891c73f70fb3262399a432d" sha256: "0763b45fa9294197a2885c8567927e2830ade852e5c896fd4ab7e0e348d0f373"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.4.10" version: "3.5.0"
args: args:
dependency: transitive dependency: transitive
description: description:
name: args name: args
sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596 sha256: "7cf60b9f0cc88203c5a190b4cd62a99feea42759a7fa695010eb5de1c0b2252a"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.4.2" version: "2.5.0"
async: async:
dependency: transitive dependency: transitive
description: description:
@ -57,14 +57,6 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.18.0" version: "1.18.0"
convert:
dependency: transitive
description:
name: convert
sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592"
url: "https://pub.dev"
source: hosted
version: "3.1.1"
crypto: crypto:
dependency: transitive dependency: transitive
description: description:
@ -77,18 +69,18 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
name: cupertino_icons name: cupertino_icons
sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.6" version: "1.0.8"
dio: dio:
dependency: "direct main" dependency: "direct main"
description: description:
name: dio name: dio
sha256: "0978e9a3e45305a80a7210dbeaf79d6ee8bee33f70c8e542dc654c952070217f" sha256: "11e40df547d418cc0c4900a9318b26304e665da6fa4755399a9ff9efd09034b5"
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "5.4.2+1" version: "5.4.3+1"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
@ -135,10 +127,10 @@ packages:
dependency: transitive dependency: transitive
description: description:
name: googleapis_auth name: googleapis_auth
sha256: "1401a9e55f9e0f565d3eebb18d990290f53a12d38a5f7f0230b112895778a85b" sha256: befd71383a955535060acde8792e7efc11d2fccd03dd1d3ec434e85b68775938
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.5.1" version: "1.6.0"
grpc: grpc:
dependency: "direct main" dependency: "direct main"
description: description:
@ -171,14 +163,6 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.0.2" version: "4.0.2"
js:
dependency: transitive
description:
name: js
sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf
url: "https://pub.dev"
source: hosted
version: "0.7.1"
leak_tracker: leak_tracker:
dependency: transitive dependency: transitive
description: description:
@ -243,14 +227,6 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.9.0" version: "1.9.0"
pointycastle:
dependency: transitive
description:
name: pointycastle
sha256: "43ac87de6e10afabc85c445745a7b799e04de84cebaa4fd7bf55a5e1e9604d29"
url: "https://pub.dev"
source: hosted
version: "3.7.4"
protobuf: protobuf:
dependency: transitive dependency: transitive
description: description:
@ -269,7 +245,7 @@ packages:
description: description:
path: "." path: "."
ref: main ref: main
resolved-ref: eb11f022be9c1fc5db6e87f3463a1ceb3e04501f resolved-ref: "6b6325ac4b6c469e41c994d1045af79225a49d38"
url: "https://git.badhouseplants.net/softplayer/softplayer-dart-proto.git" url: "https://git.badhouseplants.net/softplayer/softplayer-dart-proto.git"
source: git source: git
version: "1.0.0+1" version: "1.0.0+1"