From cb6b0760a0339a9a977e033519cd28b6a9f4ccc6 Mon Sep 17 00:00:00 2001 From: Szymon Uglis Date: Mon, 18 Nov 2024 23:49:31 +0100 Subject: [PATCH] Refactor session handling; Implement main page statistics --- lib/src/web_app/api_server.dart | 23 ++------- lib/src/web_app/utils.dart | 21 ++++++++ templates/index.html | 86 +++++++++++++++++++++++++++++++-- 3 files changed, 108 insertions(+), 22 deletions(-) diff --git a/lib/src/web_app/api_server.dart b/lib/src/web_app/api_server.dart index 41674a0..82a6c2f 100644 --- a/lib/src/web_app/api_server.dart +++ b/lib/src/web_app/api_server.dart @@ -27,7 +27,7 @@ class WebServer { ...data.toJson(), 'clientId': clientId, 'redirectUri': clientRedirectUri, - 'user_data': Session.getSession(request)?.data['user_data'] ?? false, + 'user_data': getUserDataFromSession(request) ?? false, }); } @@ -47,34 +47,19 @@ class WebServer { final tokenBodyJson = jsonDecode(tokenResponse.body); final token = tokenBodyJson['access_token']; - print(tokenResponse.body); - print(tokenResponse.statusCode); - final userData = await http.get(Uri.https('discord.com', '/api/oauth2/@me'), headers: { "Accept": "application/json", "Authorization": "Bearer $token", }); - final userDataJson = jsonDecode(userData.body); - print(userData.body); - print(userData.statusCode); - - var session = Session.getSession(request); - session ??= Session.createSession(request); - session.data['user_data'] = { - 'id': userDataJson['user']['id'], - 'name': userDataJson['user']['global_name'] ?? userDataJson['user']['username'], - 'avatar': userDataJson['user']['avatar'], - 'expires_at': userDataJson['expires'], - 'token': token, - }; - session.expires = DateTime.now().add(Duration(seconds: tokenBodyJson['expires_in'])); + + initSession(request, userDataJson, tokenBodyJson); return shelf.Response.seeOther("/"); } Future _handleLogOut(shelf.Request request) async { - Session.deleteSession(request); + deleteSession(request); return shelf.Response.seeOther("/"); } diff --git a/lib/src/web_app/utils.dart b/lib/src/web_app/utils.dart index bccb208..6fb0bb1 100644 --- a/lib/src/web_app/utils.dart +++ b/lib/src/web_app/utils.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:mustachex/mustachex.dart'; import 'package:shelf/shelf.dart' as shelf; +import 'package:shelf_session/session_middleware.dart'; shelf.Response createJsonErrorResponse(int errorCode, String errorMessage) { return shelf.Response(errorCode, @@ -23,3 +24,23 @@ Future createTwigResponse(String name, {Map? pa shelf.Response createUnauthorizedResponse(String errorMessage) => createJsonErrorResponse(400, errorMessage); shelf.Response createForbiddenResponse() => shelf.Response.forbidden(null); + +void initSession(shelf.Request request, Map userDataJson, Map tokenDataJson) { + var session = Session.getSession(request); + session ??= Session.createSession(request); + session.data['user_data'] = { + 'id': userDataJson['user']['id'], + 'name': userDataJson['user']['global_name'] ?? userDataJson['user']['username'], + 'avatar': userDataJson['user']['avatar'], + 'expires_at': userDataJson['expires'], + 'token': tokenDataJson['access_token'], + }; + session.expires = DateTime.now().add(Duration(seconds: tokenDataJson['expires_in'])); +} + +void deleteSession(shelf.Request request) { + Session.deleteSession(request); +} + +Map? getUserDataFromSession(shelf.Request request) => + Session.getSession(request)?.data['user_data'] as Map?; diff --git a/templates/index.html b/templates/index.html index b5f57df..ad29661 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,20 +6,23 @@ -