From 2f98c95d215da1f93dc94e86745e6f6a80864dae Mon Sep 17 00:00:00 2001 From: yackob03 Date: Thu, 26 Sep 2013 19:59:58 -0400 Subject: [PATCH] Add a user service and load the user information dynamically from the backend. --- endpoints/api.py | 12 ++++++++++++ static/js/app.js | 29 ++++++++++++++++++++++++++++- static/js/controllers.js | 8 +++++++- templates/index.html | 4 ++-- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/endpoints/api.py b/endpoints/api.py index 7b587ce9b..9f033a958 100644 --- a/endpoints/api.py +++ b/endpoints/api.py @@ -19,6 +19,18 @@ def welcome(): return make_response('welcome', 200) +@app.route('/api/user/') +@login_required +def get_logged_in_user(): + user = current_user.db_user + return jsonify({ + 'verified': user.verified, + 'anonymous': False, + 'username': user.username, + 'email': user.email, + }) + + @app.route('/api/repository/', methods=['POST']) @login_required def create_repo_api(): diff --git a/static/js/app.js b/static/js/app.js index c13912532..cf4f438c1 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -1,4 +1,31 @@ -quayApp = angular.module('quay', ['restangular']). +quayApp = angular.module('quay', ['restangular'], function($provide) { + $provide.factory('UserService', ['Restangular', function(Restangular) { + var userResponse = { + verified: false, + anonymous: true, + username: null, + email: null + } + + var userService = {} + + userService.load = function() { + var userFetch = Restangular.one('user/'); + userFetch.get().then(function(loadedUser) { + userResponse = loadedUser; + }); + }; + + userService.currentUser = function() { + return userResponse; + } + + // Load the user the first time. + userService.load(); + + return userService; + }]) + }). config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { $routeProvider. when('/repository/:namespace/:name', {templateUrl: '/static/partials/view-repo.html', controller: RepoCtrl}). diff --git a/static/js/controllers.js b/static/js/controllers.js index a521be593..a8c773998 100644 --- a/static/js/controllers.js +++ b/static/js/controllers.js @@ -1,3 +1,9 @@ +function HeaderCtrl($scope, UserService) { + $scope.$watch( function () { return UserService.currentUser(); }, function (currentUser) { + $scope.user = currentUser; + }, true); +} + function RepoListCtrl($scope, Restangular) { var repositoryFetch = Restangular.all('repository/'); repositoryFetch.getList().then(function(resp) { @@ -37,4 +43,4 @@ function RepoCtrl($scope, Restangular, $routeParams, $rootScope) { $scope.repo = null; $rootScope.title = 'Unknown Repository'; }); -} \ No newline at end of file +} diff --git a/templates/index.html b/templates/index.html index f930d036b..e6d5f3e13 100644 --- a/templates/index.html +++ b/templates/index.html @@ -24,7 +24,7 @@ -