Logout

This commit is contained in:
LittleSheep 2024-02-08 05:03:38 +08:00
parent de12616109
commit 7ed4045a8c
2 changed files with 65 additions and 8 deletions

View File

@ -85,8 +85,15 @@ class AuthGuard {
storage.write(key: profileKey, value: userinfo);
storage.write(key: storageKey, value: client!.credentials.toJson());
} catch (e) {
print(e);
}
}
print('Logged in: $userinfo');
void logout() {
try {
storage.delete(key: profileKey);
storage.delete(key: storageKey);
} catch (e) {
print(e);
}

View File

@ -1,16 +1,27 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:goatagent/auth.dart';
import 'package:goatagent/widgets/name_card.dart';
import 'package:oauth2/oauth2.dart' as oauth2;
import 'auth.dart';
class AccountScreen extends StatelessWidget {
class AccountScreen extends StatefulWidget {
const AccountScreen({super.key});
@override
State<AccountScreen> createState() => _AccountScreenState();
}
class _AccountScreenState extends State<AccountScreen> {
bool isAuthorized = false;
@override
void initState() {
super.initState();
AuthGuard().isAuthorized().then((value) {
setState(() {
isAuthorized = value;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -22,6 +33,45 @@ class AccountScreen extends StatelessWidget {
NameCard(
onLogin: () async {
await AuthGuard().login(context);
var authorized = await AuthGuard().isAuthorized();
setState(() {
isAuthorized = authorized;
});
},
),
FutureBuilder(
future: AuthGuard().isAuthorized(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData && snapshot.data == true) {
return Padding(
padding: const EdgeInsets.only(top: 5),
child: Wrap(
spacing: 5,
children: [
Card(
elevation: 0,
child: InkWell(
splashColor: Colors.indigo.withAlpha(30),
onTap: () async {
AuthGuard().logout();
var authorized =
await AuthGuard().isAuthorized();
setState(() {
isAuthorized = authorized;
});
},
child: const ListTile(
leading: Icon(Icons.logout),
title: Text('Logout'),
),
),
),
],
),
);
} else {
return const Padding(padding: EdgeInsets.only(top: 5));
}
},
),
],