From 8e6e361eb0e1d4c92b99fa20a70f0fa7b296a466 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 29 Apr 2015 23:29:43 +1200 Subject: Check if user is logged in --- src/org/traccar/http/MainServlet.java | 6 ++++++ web/Application.js | 15 +++++++++++++-- web/Login.js | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java index 10920accb..4dc02e81f 100644 --- a/src/org/traccar/http/MainServlet.java +++ b/src/org/traccar/http/MainServlet.java @@ -53,6 +53,8 @@ public class MainServlet extends HttpServlet { resp.sendError(HttpServletResponse.SC_BAD_REQUEST); } else if (command.equals("/async")) { async(req.startAsync()); + } else if (command.equals("/session")) { + session(req, resp); } else if (command.equals("/login")) { login(req, resp); } else if (command.equals("/logout")) { @@ -211,6 +213,10 @@ public class MainServlet extends HttpServlet { } } + private void session(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.getWriter().println("{ success: true, session: " + (req.getSession().getAttribute(USER_ID) != null) + " }"); + } + private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { req.getSession().setAttribute(USER_ID, diff --git a/web/Application.js b/web/Application.js index c522d50f7..594ce0635 100644 --- a/web/Application.js +++ b/web/Application.js @@ -37,11 +37,22 @@ Ext.Loader.loadScript({ name: 'Traccar', requires: [ 'Styles', - 'Login' + 'Login', + 'MainView' ], launch: function() { - Ext.create('Login').show(); + Ext.Ajax.request({ + url: '/api/session', + success: function(response) { + var result = Ext.decode(response.responseText); + if (result.success && result.session) { + Ext.create('MainView', { renderTo: document.body }); + } else { + Ext.create('Login').show(); + } + } + }) } }); } diff --git a/web/Login.js b/web/Login.js index 6fef9ce9d..871bef17b 100644 --- a/web/Login.js +++ b/web/Login.js @@ -65,7 +65,7 @@ Ext.define('Login', { closable: false, items: [{ xtype: 'login-form' }], - + submit: function() { var win = this; var form = this.down('form').getForm(); -- cgit v1.2.3