diff --git a/lib/auth.dart b/lib/auth.dart index f2fc2c6..1828bfb 100644 --- a/lib/auth.dart +++ b/lib/auth.dart @@ -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); } diff --git a/lib/screens/account.dart b/lib/screens/account.dart index f5cf372..2d60910 100644 --- a/lib/screens/account.dart +++ b/lib/screens/account.dart @@ -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 createState() => _AccountScreenState(); +} + +class _AccountScreenState extends State { + 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 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)); + } }, ), ],