Add check tocken

This commit is contained in:
Sebastien 2018-11-30 16:53:03 +02:00
parent 23d098780b
commit 0010822a03
4 changed files with 78 additions and 24 deletions

View File

@ -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);
}
}
}

View File

@ -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) {
@ -231,3 +231,43 @@ function checkConnection() {
});
}
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);
});
}

View File

@ -15,6 +15,6 @@
],
"start_url": "/",
"display": "standalone",
"theme_color": "#263238",
"theme_color": "#3e2723",
"background_color": "#263238"
}

View File

@ -11,11 +11,12 @@
<!-- AND -->
<!-- LOGIN -->
<!--__________________________________________________________-->
<link href="css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<link href="../static/css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection"/>
<div th:fragment="header (page, guild_name, redirect_url, isAdmin)">
<nav class="blue-grey darken-4 z-depth-3" role="navigation" >
<nav class="brown darken-4 z-depth-3" role="navigation" >
<div class="nav-wrapper container">
<a id="logo-container" href="/" class="brand-logo" style="white-space: nowrap">Claptrap Bot</a>
<b><a id="logo-container" href="/" class="brand-logo" style="white-space: nowrap">Claptrap Bot</a></b>
<ul class="right hide-on-med-and-down">
<li>
<a class="nav-change-guild truncate waves-effect waves-light btn-flat grey-text text-darken-1" th:text="${guild_name}" style="margin-top: 2px;margin-right: 0px;"></a>