Remove everything and add build
Signed-off-by: Nikolai Rodionov <allanger@badhouseplants.net>
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
import 'package:grpc/grpc_web.dart';
|
||||
import 'package:softplayer_dart_proto/src/accounts/v1/accounts_v1.pb.dart';
|
||||
import 'package:softplayer_dart_proto/src/accounts/v1/accounts_v1.pbgrpc.dart';
|
||||
|
||||
class AccountLocalData {
|
||||
AccountLocalData({
|
||||
required this.uuid,
|
||||
required this.token,
|
||||
});
|
||||
String uuid;
|
||||
String token;
|
||||
}
|
||||
|
||||
class AccountsGrpc {
|
||||
final GrpcWebClientChannel channel;
|
||||
late AccountsServiceClient accountsStub;
|
||||
AccountsGrpc({
|
||||
required this.channel,
|
||||
});
|
||||
|
||||
void init() {
|
||||
accountsStub = AccountsServiceClient(channel);
|
||||
}
|
||||
|
||||
Future<AccountLocalData> signUp(
|
||||
String username, String email, String password) async {
|
||||
final request = SignUpRequest(
|
||||
data: AccountData(
|
||||
name: username,
|
||||
email: email,
|
||||
),
|
||||
password: AccountPassword(
|
||||
password: password,
|
||||
));
|
||||
try {
|
||||
final response = await accountsStub.signUp(request);
|
||||
return AccountLocalData(uuid: "test", token: "test");
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
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"));
|
||||
void cleanupLocalStorate() {
|
||||
window.localStorage.remove("token");
|
||||
window.localStorage.remove("uuid");
|
||||
}
|
||||
}
|
||||
25
lib/api/grpc/test_auth.dart
Normal file
25
lib/api/grpc/test_auth.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:grpc/grpc_web.dart';
|
||||
import 'package:softplayer_dart_proto/src/test/v1/test_v1.pb.dart';
|
||||
import 'package:softplayer_dart_proto/src/test/v1/test_v1.pbgrpc.dart';
|
||||
|
||||
class TestAuthGrpc {
|
||||
final GrpcWebClientChannel channel;
|
||||
late TestAuthServiceClient serviceStub;
|
||||
TestAuthGrpc({required this.channel});
|
||||
|
||||
void init() {
|
||||
serviceStub = TestAuthServiceClient(channel);
|
||||
}
|
||||
|
||||
Future<void> pong() async {
|
||||
final request = PongRequest();
|
||||
try {
|
||||
final response = await serviceStub.pong(request);
|
||||
log(response.toString());
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
lib/api/grpc/test_no_auth.dart
Normal file
25
lib/api/grpc/test_no_auth.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:grpc/grpc_web.dart';
|
||||
import 'package:softplayer_dart_proto/src/test/v1/test_v1.pb.dart';
|
||||
import 'package:softplayer_dart_proto/src/test/v1/test_v1.pbgrpc.dart';
|
||||
|
||||
class TestNoAuthGrpc {
|
||||
final GrpcWebClientChannel channel;
|
||||
late TestNoAuthServiceClient serviceStub;
|
||||
TestNoAuthGrpc({required this.channel});
|
||||
|
||||
void init() {
|
||||
serviceStub = TestNoAuthServiceClient(channel);
|
||||
}
|
||||
|
||||
Future<void> ping() async {
|
||||
final request = PingRequest();
|
||||
try {
|
||||
final response = await serviceStub.ping(request);
|
||||
log(response.toString());
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
import 'package:web/web.dart' as web;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:grpc/grpc_web.dart';
|
||||
import 'package:softplayer_web/api/grpc/accounts.dart';
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
enum Action { singIn, signUp, resetPassword }
|
||||
|
||||
class _LoginFormState extends State<LoginForm> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
final usernameCtrl = TextEditingController();
|
||||
final passwordCtrl = TextEditingController();
|
||||
final passwordVerifyCtrl = TextEditingController();
|
||||
final emailCtrl = TextEditingController();
|
||||
final codeCtrl = TextEditingController();
|
||||
|
||||
late AccountsGrpc accountsGrpc;
|
||||
Action action = Action.singIn;
|
||||
bool codeEnabled = false;
|
||||
static const dialogName = "Login";
|
||||
|
||||
void submitSignUp() {
|
||||
// Validate returns true if the form is valid, or false otherwise.
|
||||
if (_formKey.currentState!.validate()) {
|
||||
final username = usernameCtrl.text;
|
||||
final password = passwordCtrl.text;
|
||||
final email = emailCtrl.text;
|
||||
accountsGrpc.signUp(username, email, password).then((rs) {
|
||||
web.window.localStorage.setItem("token", rs.token);
|
||||
web.window.localStorage.setItem("uuid", rs.uuid);
|
||||
widget.notifyParent();
|
||||
Navigator.of(context, rootNavigator: true).pop();
|
||||
}).catchError((e) {
|
||||
GrpcError error = e;
|
||||
String msg;
|
||||
if (error.message != null) {
|
||||
msg = error.message!;
|
||||
} else {
|
||||
msg = error.toString();
|
||||
}
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
content: Text(msg),
|
||||
backgroundColor: Colors.red,
|
||||
showCloseIcon: true,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
));
|
||||
passwordCtrl.clear();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
List<Widget> signUpActions() => [
|
||||
TextButton(
|
||||
onPressed: () => setState(() {
|
||||
action = Action.singIn;
|
||||
}),
|
||||
child: const Text('Sign In'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: submitSignUp,
|
||||
child: const Text('OK'),
|
||||
),
|
||||
];
|
||||
|
||||
List<Widget> resetPasswordActions() => [
|
||||
TextButton(
|
||||
onPressed: () => setState(() {
|
||||
action = Action.singIn;
|
||||
}),
|
||||
child: const Text('Sign In'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => setState(() {
|
||||
action = Action.signUp;
|
||||
}),
|
||||
child: const Text('Sing Up'),
|
||||
),
|
||||
];
|
||||
|
||||
Widget signUpForm() => SizedBox(
|
||||
width: 420,
|
||||
height: 280,
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Center(
|
||||
child: Column(children: [
|
||||
TextFormField(
|
||||
onFieldSubmitted: (value) => submitSignUp(),
|
||||
autofocus: true,
|
||||
controller: usernameCtrl,
|
||||
decoration: const InputDecoration(
|
||||
hintText: "Enter your username",
|
||||
icon: Icon(Icons.account_circle),
|
||||
label: Text("Username"),
|
||||
),
|
||||
cursorWidth: 1,
|
||||
cursorHeight: 18,
|
||||
cursorRadius: const Radius.circular(10),
|
||||
),
|
||||
TextFormField(
|
||||
onFieldSubmitted: (value) => submitSignUp(),
|
||||
controller: emailCtrl,
|
||||
autofocus: true,
|
||||
decoration: const InputDecoration(
|
||||
hintText: "Enter your email",
|
||||
icon: Icon(Icons.email),
|
||||
label: Text("Email"),
|
||||
),
|
||||
cursorWidth: 1,
|
||||
cursorHeight: 18,
|
||||
cursorRadius: const Radius.circular(10),
|
||||
),
|
||||
TextFormField(
|
||||
onFieldSubmitted: (value) => submitSignUp(),
|
||||
controller: passwordCtrl,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(
|
||||
hintText: "Enter your password",
|
||||
icon: Icon(Icons.password),
|
||||
label: Text("Password")),
|
||||
cursorWidth: 1,
|
||||
cursorHeight: 18,
|
||||
cursorRadius: const Radius.circular(10),
|
||||
),
|
||||
TextFormField(
|
||||
onFieldSubmitted: (value) => submitSignUp(),
|
||||
controller: passwordVerifyCtrl,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(
|
||||
hintText: "Verify your password",
|
||||
icon: Icon(Icons.password),
|
||||
label: Text("Confirm Password")),
|
||||
cursorWidth: 1,
|
||||
cursorHeight: 18,
|
||||
cursorRadius: const Radius.circular(10),
|
||||
),
|
||||
]))));
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
accountsGrpc = AccountsGrpc(channel: widget.grpcChannel);
|
||||
accountsGrpc.init();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => AlertDialog(
|
||||
title: const Text(dialogName),
|
||||
// content: action == Action.singIn ? signInForm() : signUpForm(),
|
||||
content: () {
|
||||
switch (action) {
|
||||
default:
|
||||
return signUpForm();
|
||||
}
|
||||
}(),
|
||||
actions: () {
|
||||
switch (action) {
|
||||
default:
|
||||
return signUpActions();
|
||||
}
|
||||
}());
|
||||
}
|
||||
@@ -1,75 +1,71 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:softplayer_web/api/grpc/test_auth.dart';
|
||||
import 'package:softplayer_web/api/grpc/test_no_auth.dart';
|
||||
import 'package:web/web.dart' as web;
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:grpc/grpc_web.dart';
|
||||
import 'package:softplayer_web/api/grpc/accounts.dart';
|
||||
import 'package:softplayer_web/components/login_form.dart';
|
||||
|
||||
void main() async {
|
||||
await dotenv.load(fileName: ".env");
|
||||
String backendURL = dotenv.env['SOFTPLAYER_BACKEND_URL']!;
|
||||
GrpcWebClientChannel grpcChannel =
|
||||
GrpcWebClientChannel.xhr(Uri.parse(backendURL));
|
||||
GrpcWebClientChannel grpcChannel = GrpcWebClientChannel.xhr(
|
||||
Uri.parse(backendURL),
|
||||
);
|
||||
|
||||
runApp(MyApp(channel: grpcChannel));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
MyApp({super.key, required this.channel});
|
||||
const MyApp({super.key, required this.channel});
|
||||
final GrpcWebClientChannel channel;
|
||||
late final AccountsGrpc accountsGrpc = AccountsGrpc(channel: channel);
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
accountsGrpc.init();
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
debugShowCheckedModeBanner: true,
|
||||
title: 'Softplayer',
|
||||
home: RootWidget(channel: channel),
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class RootWidget extends StatefulWidget {
|
||||
final GrpcWebClientChannel channel;
|
||||
const RootWidget({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 web.window.localStorage.getItem("token") != null;
|
||||
}
|
||||
|
||||
final GlobalKey<ScaffoldState> _key = GlobalKey(); // Create a key
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Container(
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage("assets/login_background.jpg"),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
body: Container(
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
child: Column(
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
TestAuthGrpc grpc = TestAuthGrpc(channel: widget.channel);
|
||||
grpc.init();
|
||||
await grpc.pong();
|
||||
},
|
||||
child: Text("pong"),
|
||||
),
|
||||
child: LoginForm(
|
||||
grpcChannel: widget.channel,
|
||||
notifyParent: refresh,
|
||||
)));
|
||||
ElevatedButton(
|
||||
onPressed: () async {
|
||||
TestNoAuthGrpc grpc = TestNoAuthGrpc(channel: widget.channel);
|
||||
grpc.init();
|
||||
await grpc.ping();
|
||||
},
|
||||
child: Text("ping"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user