Push new session handling
This commit is contained in:
parent
75de51ec14
commit
b99f4c3140
8 changed files with 182 additions and 7 deletions
|
@ -3,7 +3,13 @@ import 'dart:convert';
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:footer/footer.dart';
|
||||
import 'package:footer/footer_view.dart';
|
||||
import 'package:libac_flutter/nbt/NbtIo.dart';
|
||||
import 'package:libac_flutter/nbt/NbtUtils.dart';
|
||||
import 'package:libac_flutter/nbt/impl/CompoundTag.dart';
|
||||
import 'package:libac_flutter/nbt/impl/IntTag.dart';
|
||||
import 'package:libac_flutter/nbt/impl/StringTag.dart';
|
||||
import 'package:libac_flutter/utils/uuid/UUID.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:zontreck/Constants.dart';
|
||||
import 'package:zontreck/Packets.dart';
|
||||
import 'package:zontreck/Settings.dart';
|
||||
|
@ -15,6 +21,7 @@ class User {
|
|||
int createdAt;
|
||||
String userTitle;
|
||||
bool active;
|
||||
UUID loginToken;
|
||||
|
||||
User(
|
||||
{required this.ID,
|
||||
|
@ -22,7 +29,8 @@ class User {
|
|||
required this.LastName,
|
||||
required this.createdAt,
|
||||
required this.userTitle,
|
||||
required this.active});
|
||||
required this.active,
|
||||
required this.loginToken});
|
||||
|
||||
static User parseJson(Map<String, dynamic> map) {
|
||||
return User(
|
||||
|
@ -31,7 +39,8 @@ class User {
|
|||
LastName: map['last'] as String,
|
||||
createdAt: map['rez'] as int,
|
||||
userTitle: map['title'] as String,
|
||||
active: map['active'] as bool);
|
||||
active: map['active'] as bool,
|
||||
loginToken: UUID.parse(map['token'] as String));
|
||||
}
|
||||
|
||||
String encode() {
|
||||
|
@ -41,9 +50,34 @@ class User {
|
|||
"last": LastName,
|
||||
"rez": createdAt,
|
||||
"title": userTitle,
|
||||
"active": active
|
||||
"active": active,
|
||||
"token": loginToken
|
||||
});
|
||||
}
|
||||
|
||||
CompoundTag save() {
|
||||
CompoundTag tag = CompoundTag();
|
||||
NbtUtils.writeUUID(tag, "id", ID);
|
||||
tag.put("first", StringTag.valueOf(FirstName));
|
||||
tag.put("last", StringTag.valueOf(LastName));
|
||||
tag.put("rez", IntTag.valueOf(createdAt));
|
||||
tag.put("title", StringTag.valueOf(userTitle));
|
||||
NbtUtils.writeBoolean(tag, "active", active);
|
||||
NbtUtils.writeUUID(tag, "token", loginToken);
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
static User load(CompoundTag tag) {
|
||||
return User(
|
||||
ID: NbtUtils.readUUID(tag, "id"),
|
||||
FirstName: tag.get("first")!.asString(),
|
||||
LastName: tag.get("last")!.asString(),
|
||||
createdAt: tag.get("rez")!.asInt(),
|
||||
userTitle: tag.get("title")!.asString(),
|
||||
active: NbtUtils.readBoolean(tag, "active"),
|
||||
loginToken: NbtUtils.readUUID(tag, "token"));
|
||||
}
|
||||
}
|
||||
|
||||
class OpenSimPage extends StatefulWidget {
|
||||
|
@ -77,6 +111,29 @@ class OpenSimPageState extends State<OpenSimPage> {
|
|||
settings.OpenSimSetupCompleted = false;
|
||||
}
|
||||
|
||||
if (!settings.loggedIn) {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
if (prefs.containsKey("settings")) {
|
||||
String encoded = prefs.getString("settings")!;
|
||||
CompoundTag tag = NbtIo.readBase64String(encoded) as CompoundTag;
|
||||
if (tag.contains("user"))
|
||||
settings.currentUser = User.load(tag.get("user") as CompoundTag);
|
||||
|
||||
// Validate current session
|
||||
var reply = await settings.sendPacketToEndpoint(
|
||||
APIEndpoint.ValidateSession,
|
||||
C2SSessionCheckPacket(
|
||||
sessionToken: settings.currentUser!.loginToken))
|
||||
as S2CSessionCheckPacket;
|
||||
if (reply.valid) {
|
||||
// We're good to continue loading this user
|
||||
} else {
|
||||
settings.currentUser = null;
|
||||
prefs.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var pong = await settings.sendPacketToEndpoint(
|
||||
APIEndpoint.Ping, NullPacket()) as S2CPongPacket;
|
||||
|
||||
|
@ -147,6 +204,11 @@ class OpenSimPageState extends State<OpenSimPage> {
|
|||
APIEndpoint.Logout,
|
||||
NullPacket());
|
||||
|
||||
SharedPreferences prefs =
|
||||
await SharedPreferences
|
||||
.getInstance();
|
||||
prefs.clear();
|
||||
|
||||
didChangeDependencies();
|
||||
},
|
||||
child: Text("LOGOUT"))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue