git.fiddlerwoaroof.com
Browse code

fine-tuning front controller

- User lists are now visible without logging in
- This doesn't matter privacy-wise because it's the way things have
always been

- Note: nginx conf changes necessary

fiddlerwoaroof authored on 15/10/2015 06:46:06
Showing 7 changed files
... ...
@@ -9,6 +9,9 @@ server {
9 9
   index /api;
10 10
 
11 11
 
12
+  location = / {
13
+    rewrite . /api/root;
14
+  }
12 15
   location / {
13 16
     # pass unrecognized urls for index.html
14 17
     # angularJS will handle the routing of most of these
... ...
@@ -35,14 +35,20 @@ user.get_users(app)
35 35
 app.register_blueprint(user.user_blueprint, url_prefix='/user')
36 36
 app.register_blueprint(bone.bone_blueprint, url_prefix='/bones')
37 37
 
38
+@app.route('/root')
39
+def get_index_page():
40
+    filebase = 'index'
41
+    if 'username' not in session:
42
+        filebase = 'login'
43
+    filename = os.path.join(config.static_root, '%s.html' % filebase)
44
+    with open(filename) as f:
45
+        return f.read()
46
+
38 47
 @app.route('/')
39 48
 def index():
40
-    filename = os.path.join(config.static_root, 'login.html')
41
-    if 'username' in session: 
42
-        filename = os.path.join(config.static_root, 'index.html')
49
+    filename = os.path.join(config.static_root, 'index.html')
43 50
     with open(filename) as f:
44 51
         dat = f.read()
45
-        print dat
46 52
         return dat
47 53
 
48 54
 if __name__ == '__main__':
... ...
@@ -148,12 +148,16 @@ def submit_link():
148 148
                 db.rollback()
149 149
     return json.dumps(result), 200, {'Content-Type':'application/json'}
150 150
 
151
-@bone_blueprint.route('',defaults={'username':None}, methods=['GET'])
152
-@bone_blueprint.route('/u/<username>', methods=['GET'])
151
+@bone_blueprint.route('', methods=['GET'])
153 152
 @login_required
153
+def default_data():
154
+    result = '', 401, {}
155
+    if 'username' in session:
156
+        result = data(current_user.id)
157
+    return result
158
+
159
+@bone_blueprint.route('/u/<username>', methods=['GET'])
154 160
 def data(username):
155
-    if username is None and 'username' in session:
156
-        username = current_user.id
157 161
     sectionTitle = username
158 162
 
159 163
     result = {'marrow':[], 'sectionTitle': sectionTitle}
... ...
@@ -200,6 +204,7 @@ def subscribe():
200 204
 @bone_blueprint.route('/subscriptions', defaults={'before':None, 'count': None})
201 205
 @bone_blueprint.route('/subscriptions/<before>', defaults={'count': None})
202 206
 @bone_blueprint.route('/subscriptions/count/<int:count>', defaults={'before': None})
207
+@login_required
203 208
 @cross_origin(allow_headers='Content-Type')
204 209
 def subscriptions(before, count):
205 210
     result = {'marrow':[], 'sectionTitle': 'Subscriptions'}
... ...
@@ -8,7 +8,7 @@ from flask.ext.login import LoginManager, UserMixin, login_user, logout_user, lo
8 8
 import psycopg2
9 9
 
10 10
 login_manager = LoginManager()
11
-login_manager.login_view = "/login.html"
11
+# login_manager.login_view = "/login.html"
12 12
 from . import database
13 13
 
14 14
 user_blueprint = Blueprint('user', __name__)
... ...
@@ -331,7 +331,7 @@ main form {
331 331
   padding: 10px 0;
332 332
 }
333 333
 main form input[type="text"] {
334
-  width: 100%;
334
+  width: 315px;
335 335
 }
336 336
 
337 337
 .more-link {
... ...
@@ -357,138 +357,28 @@ main form input[type="text"] {
357 357
 /* @end */
358 358
 
359 359
 /* @group Login Module */
360
-#login_form .tab {
361
-  text-align: center;
362
-  background: white;
363
-  position:absolute;
364
-  width: 39vw;
365
-  height: 39vh;
366
-  padding: 3vh 3vw;
367
-  margin-left: 13vw;
368
-  overflow: hidden;
369
-  z-index: 0;
370
-  border: 1px solid black;
371
-}
372
-
373
-.relCont::after {
374
-  content: " ";
375
-  clear: both;
376
-  overflow: hidden;
377
-}
378
-
379
-.relCont {
380
-  position: relative;
381
-}
382
-
383
-#login_form .tab.front {
384
-  z-index: 1;
385
-}
386 360
 
387
-#login_form .message{
388
-  /*position: absolute;*/
361
+#login_form div.message{
362
+  position: absolute;
363
+  background: red;
389 364
   width: 100%;
390
-  height: 2em;
391 365
   text-align: center;
392
-  visibility: hidden;
393 366
 }
394 367
 
395
-#login_form div.error {
396
-  background: red;
397
-  visibility: visible;
398
-}
399
-
400
-#login_form button {
401
-  bottom: 2em;
402
-  min-width: 33%;
403
-  margin-top: 1.5em;
404
-  padding: 1em;
405
-  font-size: 2rem;
406
-  color: white;
407
-  background: blue;
408
-  text-transform: uppercase;
409
-  background-image: linear-gradient(transparent, #003 90%);
410
-}
411
-
412
-#login_form ul, #login_form li {
413
-  list-style: none;
414
-  display: inline-block;
415
-  color: black;
416
-}
417
-
418
-#login_form ul {
419
-  z-index: 2;
420
-  margin-bottom: 0px;
421
-  width: 33vw;
422
-}
423
-
424
-#login_form #tab-choice {
425
-  margin-left: 13vw;
426
-  /*margin-bottom: 2em;*/
427
-}
428
-
429
-#login_form input[type=radio] {
430
-  display: none;
431
-}
432
-
433
-#login_form input[type=radio] + label {
434
-  font-size: 1.5rem;
435
-  cursor: pointer;
436
-  border: 1px solid black;
437
-  background: #aaa;
438
-  padding: 0.25em;
439
-  padding-bottom: 0em;
440
-  margin-right: 0.5em;
441
-  margin-bottom: -3px;
442
-  z-index: 5;
443
-}
444
-
445
-#login_form input[type=radio]:checked + label {
446
-  background: white;
447
-  border-bottom-color: white;
448
-  z-index: 3
449
-}
450
-
451
-/*
452 368
 #login_form {
453 369
   position: absolute;
454 370
   z-index: 100;
455 371
   height: 100%;
456 372
   background: white;
457 373
 }
458
-*/
459
-
460 374
 #login_form form {
461
-  text-align: center;
462
-}
463
-
464
-  
465
-#login_form div.message{
466
-}
467
-
468
-#login_form form {
469
-  margin-top: 100px;
470
-}
471
-
472
-#login_form {
473
-  width: 100%;
474
-  left: 0;
375
+  position: absolute;
475 376
   top: 0;
476
-}
477
-
478
-#login_form form {
479
-  margin-top: 20px;
480
-}
481
-
482
-#login_form div.message{
483
-  margin-top 10px;
484
-}
485
-
486
-form input[type="password"] {
377
+  left: 0;
487 378
   width: 100%;
379
+  text-align: center;
488 380
 }
489 381
 
490
-
491
-
492 382
 /* @end */
493 383
 
494 384
 /* @group Form Validation */
... ...
@@ -574,6 +464,18 @@ main > section {
574 464
     text-align: right;
575 465
     padding-right: 5px;
576 466
   }
467
+  
468
+  #login_form {
469
+    width: 125%;
470
+    left: -25%;
471
+    top: 66px;
472
+  }
473
+  #login_form div.message{
474
+    top: -33px;
475
+  }
476
+  #login_form form {
477
+    margin-top: 100px;
478
+  }
577 479
 
578 480
 }
579 481
 
... ...
@@ -682,6 +584,19 @@ a.user-settings:hover {
682 584
     border-top: 1px solid #ddd;
683 585
   }
684 586
   
587
+  #login_form {
588
+    width: 100%;
589
+    left: 0;
590
+    top: 0;
591
+  }
592
+  #login_form form {
593
+    margin-top: 20px;
594
+  }
595
+  #login_form div.message{
596
+    margin-top 10px;
597
+  }
598
+  form input[type="password"] {
599
+  }
685 600
 
686 601
   main::before {
687 602
     display: block;
... ...
@@ -1,3 +1,4 @@
1
+window.URL = window.URL || window.webkitURL;
1 2
 var marrowApp = angular.module('marrowApp', ['ngRoute', 'marrowApp.services', 'marrowApp.directives', 'marrowApp.utils',
2 3
                                              'marrowApp.directives.boneList', 'marrowApp.directives.userBadge',
3 4
                                              'angulartics', 'angulartics.google.analytics', 'angulartics.piwik']);
... ...
@@ -11,54 +12,75 @@ marrowApp.config(['$routeProvider',
11 12
       when('/', {templateUrl: 'partials/default.html', controller: 'MarrowCtrl'}).
12 13
       when('/user/:user', {template: '<div ng-include="templateUrl">Loading...</div>', controller: 'UserCtrl'});
13 14
   }
14
-]);
15
+])
16
+.factory('authHttpResponseInterceptor',['$q','$location', '$window',function($q,$location,$window){
17
+    return {
18
+        response: function(response){
19
+            if (response.status === 401) {
20
+                console.log("Response 401");
21
+            }
22
+            return response || $q.when(response);
23
+        },
24
+        responseError: function(rejection) {
25
+            if (rejection.status === 401) {
26
+                console.log("Response Error 401",rejection);
27
+                $window.location.href = '/login.html#' + encodeURIComponent($location.path());
28
+            }
29
+            return $q.reject(rejection);
30
+        }
31
+    };
32
+}])
33
+.config(['$httpProvider',function($httpProvider) {
34
+    //Http Intercpetor to check auth failures for xhr requests
35
+    $httpProvider.interceptors.push('authHttpResponseInterceptor');
36
+}]);
15 37
 
16 38
 marrowApp.config(['$locationProvider', function($locationProvider) { $locationProvider.html5Mode(true); }]);
17 39
 
18
-marrowApp.controller('LoginCtrl', function ($scope,$http,$route,$location) {
19
-  $scope.tab = 'login';
20
-
21
-  $scope.message = '';
22
-
23
-  var check_login = function () {
24
-    var injector = angular.injector(['ng']);
25
-    var $http = injector.get('$http');
26
-    return $http.get("/api/user/check").success(function(is_loggedon) {
27
-      if (is_loggedon.result === true) {
28
-        angular.element(document.body).addClass('is-logged-on');
29
-      }
30
-    });
31
-  };
32
-
33
-  check_login().success(
34
-    function(is_loggedon) {
35
-      if (is_loggedon.result) { $location.url('/');}
36
-  });
37
-
38
-  $scope.newuser = function () {
39
-    var username = $scope.username;
40
-    var password = $scope.password;
41
-    var postObj = {"username":username, "password": password};
42
-    $http.post("/api/user/add", postObj)
43
-    .success(function(added_user) {
44
-      if (added_user.status === true) {$location.url('/');}
45
-      else {$scope.message = added_user.message;}
46
-    });
47
-  };
48
-
49
-  $scope.login = function () {
50
-    var username = $scope.username;
51
-    var password = $scope.password;
52
-
53
-    $http.post("/api/user/login", {"username":username, "password":password})
54
-    .success(
55
-      function (login_succeeded) {
56
-        var el = angular.element(document.querySelector('#login_form'));
57
-        if (login_succeeded.status === true) {$location.url('/');}
58
-        else {$scope.message = login_succeeded.message;}
59
-    });
60
-  };
61
-});
40
+//marrowApp.controller('LoginCtrl', function ($scope,$http,$route,$location) {
41
+//  $scope.tab = 'login';
42
+
43
+//  $scope.message = '';
44
+
45
+//  var check_login = function () {
46
+//    var injector = angular.injector(['ng']);
47
+//    var $http = injector.get('$http');
48
+//    return $http.get("/api/user/check").success(function(is_loggedon) {
49
+//      if (is_loggedon.result === true) {
50
+//        angular.element(document.body).addClass('is-logged-on');
51
+//      }
52
+//    });
53
+//  };
54
+
55
+//  check_login().success(
56
+//    function(is_loggedon) {
57
+//      if (is_loggedon.result) { $location.url('/');}
58
+//  });
59
+
60
+//  $scope.newuser = function () {
61
+//    var username = $scope.username;
62
+//    var password = $scope.password;
63
+//    var postObj = {"username":username, "password": password};
64
+//    $http.post("/api/user/add", postObj)
65
+//    .success(function(added_user) {
66
+//      if (added_user.status === true) {$location.url('/');}
67
+//      else {$scope.message = added_user.message;}
68
+//    });
69
+//  };
70
+
71
+//  $scope.login = function () {
72
+//    var username = $scope.username;
73
+//    var password = $scope.password;
74
+
75
+//    $http.post("/api/user/login", {"username":username, "password":password})
76
+//    .success(
77
+//      function (login_succeeded) {
78
+//        var el = angular.element(document.querySelector('#login_form'));
79
+//        if (login_succeeded.status === true) {$location.url('/');}
80
+//        else {$scope.message = login_succeeded.message;}
81
+//    });
82
+//  };
83
+//});
62 84
 
63 85
 marrowApp.controller('RootCtrl', function ($scope,$http,$location,$route, SubscribedTo, BoneService, UserService) {
64 86
   $scope.url = "";
... ...
@@ -1,3 +1,4 @@
1
+window.URL = window.URL || window.webkitURL;
1 2
 var loginModule = angular.module('marrowLogin', ['ngResource','ngRoute','angulartics', 'angulartics.google.analytics']);
2 3
 
3 4
 loginModule.controller('LoginCtrl', function ($scope,$http,$route,$window) {