Skip to content

Commit

Permalink
OAuth: Implementing OAuth closing session button
Browse files Browse the repository at this point in the history
  • Loading branch information
0Nom4D committed Jul 14, 2024
1 parent 544a543 commit d264174
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
9 changes: 9 additions & 0 deletions lib/api/oauth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
import 'package:get_it/get_it.dart';
import 'package:guardian_dock/api/client_api.dart';
import 'package:guardian_dock/api/models/access_token.dart';

Expand Down Expand Up @@ -66,4 +68,11 @@ class OAuth {
_client.callbackJwt!(accessTokens);
}
}

Future<void> closeSession() async {
final storage = GetIt.I<FlutterSecureStorage>();

await storage.delete(key: 'access');
_client.authorizationTokens = null;
}
}
60 changes: 45 additions & 15 deletions lib/src/widgets/custom_appbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class _GuardianDockAppbarState extends State<GuardianDockAppbar> {

@override
Widget build(BuildContext context) {
debugPrint(client.authorizationTokens?.accessToken);
return AppBar(
elevation: 2.0,
backgroundColor: Colors.transparent,
Expand All @@ -37,24 +38,53 @@ class _GuardianDockAppbarState extends State<GuardianDockAppbar> {
actions: [
if (GoRouterState.of(context).path! == '/')
if (client.authorizationTokens == null) ...{
IconButton(
onPressed: () async {
await client.oauth.connectUserWithOAuth2();
setState(() {});
},
tooltip: "Link a Bungie account",
icon: Icon(
Icons.link,
color: Theme.of(context).colorScheme.onSurface
Padding(
padding: const EdgeInsets.only(right: 15),
child: IconButton(
onPressed: () async {
await client.oauth.connectUserWithOAuth2();
setState(() {});
},
tooltip: "Link a Bungie account",
icon: Icon(
Icons.link,
color: Theme.of(context).colorScheme.onSurface
)
)
)
} else ...{
// TODO Bien gérer l'affichage de la photo de profile du compte Bungie
CircleAvatar(
radius: 10,
child: Image.network(
Uri.https(ApiClient.baseUrl, '/img/profile/avatars/cc00009.jpg').toString()
),
Padding(
padding: const EdgeInsets.only(right: 15),
child: MenuAnchor(
builder: (BuildContext context, MenuController controller, Widget? child) {
return GestureDetector(
child: ClipOval(
child: SizedBox.fromSize(
size: const Size.fromRadius(17.5),
child: Image.network(
Uri.https(ApiClient.baseUrl, '/img/profile/avatars/cc00009.jpg').toString()
),
)
),
onTap: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
);
},
menuChildren: List<MenuItemButton>.generate(
1, (int index) => MenuItemButton(
onPressed: () async {
client.oauth.closeSession();
setState(() {});
},
child: const Text("Se déconnecter")
)
),
)
)
}
],
Expand Down

0 comments on commit d264174

Please sign in to comment.