From 0010822a03f238a589508001f708d52e376b61fa Mon Sep 17 00:00:00 2001 From: Sebastien Date: Fri, 30 Nov 2018 16:53:03 +0200 Subject: [PATCH 1/3] Add check tocken --- .../RestApi/UserManagerAPIController.java | 13 +++ src/main/resources/static/js/navabar.js | 80 ++++++++++++++----- src/main/resources/static/manifest.json | 2 +- src/main/resources/templates/header.html | 7 +- 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/Broken/RestApi/UserManagerAPIController.java b/src/main/java/net/Broken/RestApi/UserManagerAPIController.java index 2617823..714e873 100644 --- a/src/main/java/net/Broken/RestApi/UserManagerAPIController.java +++ b/src/main/java/net/Broken/RestApi/UserManagerAPIController.java @@ -12,6 +12,7 @@ import net.Broken.Tools.UserManager.UserUtils; import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.User; +import org.apache.http.HttpResponse; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -137,6 +138,18 @@ public class UserManagerAPIController { } + @RequestMapping(value = "/checkToken", method = RequestMethod.GET) + public ResponseEntity checkToken(@CookieValue(value = "token") String token){ + try{ + userUtils.getUserWithApiToken(userRepository,token); + return new ResponseEntity(HttpStatus.OK); + } catch (UnknownTokenException e) { + logger.info("Token check fail"); + return new ResponseEntity(HttpStatus.UNAUTHORIZED); + } + } + + } diff --git a/src/main/resources/static/js/navabar.js b/src/main/resources/static/js/navabar.js index 434d67d..121b1c3 100644 --- a/src/main/resources/static/js/navabar.js +++ b/src/main/resources/static/js/navabar.js @@ -38,12 +38,7 @@ $(document).ready(function() { btn_disconnect = $(".nav-disconnect"); nav_name = $("#nav-name"); navListeners(); - if(Cookies.get('token') === undefined){ - disconnected() - } - else{ - connected(); - } + checkConnection(); @@ -69,20 +64,16 @@ function popInSubmit(){ function connected(){ console.log("Connected!"); - nav_bar_account_link.html(connected_link); - $('.dropdown-account').dropdown({ - constrainWidth: false, // Does not change width of dropdown to that of the activator - coverTrigger: false, // Displays dropdown below the button - alignment: 'left', // Displays dropdown with edge aligned to the left of button - stopPropagation: false // Stops event propagation - } - ); - nav_name.text(Cookies.get('name')); - if (typeof needLogin !== 'undefined') { - if (Cookies.get('guild') === undefined) { - getGuild() - } + console.log("Checking token..."); + console.log(window.location.href); + if(!window.location.href.includes("oauthCallback")){ + checkToken(); } + else{ + console.log("Oauth page skip check token"); + } + + } function disconnected() { @@ -112,7 +103,7 @@ function tryConnection() { console.log(data); Cookies.set('token',data.token, { expires: 31 }); Cookies.set('name', data.name, { expires: 31 }); - debugger; + window.location.reload(true); } @@ -223,6 +214,15 @@ function checkConnection() { url: "/api/isReady", success: function (data) { console.log("Connection Ok"); + console.log(Cookies.get('token')); + + if(Cookies.get('token') === undefined){ + disconnected() + } + else{ + connected(); + } + } }).fail(function (data) { @@ -230,4 +230,44 @@ function checkConnection() { $('#modal_internet').modal('open'); }); +} + + +function checkToken() { + $.ajax({ + type: "GET", + url: "/api/userManagement/checkToken", + success: function (data) { + console.debug("...token is valid."); + nav_bar_account_link.html(connected_link); + $('.dropdown-account').dropdown({ + constrainWidth: false, // Does not change width of dropdown to that of the activator + coverTrigger: false, // Displays dropdown below the button + alignment: 'left', // Displays dropdown with edge aligned to the left of button + stopPropagation: false // Stops event propagation + } + ); + nav_name.text(Cookies.get('name')); + if (typeof needLogin !== 'undefined') { + if (Cookies.get('guild') === undefined) { + getGuild() + } + } + + } + + }).fail(function (data) { + console.error("...token is invalid !"); + console.log(data); + + Cookies.remove('token'); + Cookies.remove('name'); + Cookies.remove('guild'); + window.location.reload(true); + + }); + + + + } \ No newline at end of file diff --git a/src/main/resources/static/manifest.json b/src/main/resources/static/manifest.json index 9f5c407..203bb7c 100644 --- a/src/main/resources/static/manifest.json +++ b/src/main/resources/static/manifest.json @@ -15,6 +15,6 @@ ], "start_url": "/", "display": "standalone", - "theme_color": "#263238", + "theme_color": "#3e2723", "background_color": "#263238" } \ No newline at end of file diff --git a/src/main/resources/templates/header.html b/src/main/resources/templates/header.html index 1660cd5..1af576b 100644 --- a/src/main/resources/templates/header.html +++ b/src/main/resources/templates/header.html @@ -11,11 +11,12 @@ - + +
-