Skip to content

Commit 5934a16

Browse files
jacksonhooseAndrewSouthpaw
authored andcommitted
added nav controller, added Authenicated to , added properties to resolve, etc
1 parent 4718327 commit 5934a16

File tree

15 files changed

+89
-41
lines changed

15 files changed

+89
-41
lines changed

client/app/src/js/app.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ function config($urlRouterProvider, $stateProvider, $locationProvider, $httpProv
3232
});
3333
}
3434
},
35-
views: {
36-
35+
views: {
3736

3837
// main view
3938
'': {
@@ -78,7 +77,17 @@ function config($urlRouterProvider, $stateProvider, $locationProvider, $httpProv
7877
url: '/screenshot/:screenshotId',
7978
templateUrl: '/views/screenshot.html',
8079
controller: 'ScreenshotController',
81-
controllerAs: 'screenshotCtrl'
80+
controllerAs: 'screenshotCtrl',
81+
resolve: {
82+
screenshot: function($stateParams, $window, Screenshot) {
83+
return Screenshot.getScreenshot($stateParams.username, $stateParams.screenshotId)
84+
.then(function(response){
85+
return response;
86+
}).catch(function(err){
87+
return err;
88+
});
89+
}
90+
}
8291
})
8392

8493
/**
@@ -134,6 +143,16 @@ function AttachTokens($window) {
134143
};
135144
}
136145

146+
function run($rootScope, $location, Auth) {
147+
$rootScope.Authenticated = Auth.isAuth();
148+
149+
$rootScope.$on('$stateChangeStart', function(event) {
150+
$rootScope.Authenticated = Auth.isAuth();
151+
}.bind(this));
152+
153+
}
154+
run.$inject = ['$rootScope', '$location', 'Auth'];
155+
137156
angular
138157
.module('Archivr', [
139158
'Archivr.auth',
@@ -142,10 +161,12 @@ angular
142161
'Archivr.screenshots',
143162
'Archivr.screenshot',
144163
'Archivr.services.Auth',
164+
'Archivr.nav',
145165
'Archivr.services.User',
146166
'Archivr.services.Screenshot',
147167
'Archivr.userPage',
148168
'ui.router'
149169
])
150170
.config(config)
171+
.run(run)
151172
.factory('AttachTokens', AttachTokens);

client/app/src/js/controllers/auth.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,15 @@ function AuthController($window, $location, Auth, User) {
2323
Auth.signup({
2424
username: user,
2525
password: pass
26-
})
27-
.then(function(response) {
26+
}).then(function(response) {
2827
console.log(response);
2928
// $window.localStorage.setItem('com.archivr', token);
3029
// $location.path('/screenshots');
31-
})
32-
.catch(function(error) {
30+
}).catch(function(error) {
3331
console.error(error);
3432
});
3533
};
3634

37-
3835
this.logout = function(){
3936
console.log('Signed out');
4037
Auth.signout();

client/app/src/js/controllers/nav.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* NavController
3+
* ====================
4+
* Handles control across the navigation bar
5+
*/
6+
7+
function NavController($rootScope, Auth) {
8+
9+
this.loggedIn = $rootScope.Authenticated;
10+
11+
$rootScope.$watch('Authenticated', function(authenticated) {
12+
this.loggedIn = authenticated;
13+
}.bind(this));
14+
15+
// ng-click function for header logout
16+
this.logout = function() {
17+
Auth.signout();
18+
};
19+
20+
}
21+
NavController.$inject = ['$rootScope', 'Auth'];
22+
23+
angular.module('Archivr.nav', [
24+
'Archivr.services.User',
25+
'Archivr.services.Auth'
26+
]).controller('NavController', NavController);
27+

client/app/src/js/controllers/profileBar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
function ProfileBarController(User) {
88

9-
this.user = User.user;
9+
this.user = User.getUser();
1010

1111
}
1212
ProfileBarController.$inject = ['User'];

client/app/src/js/controllers/screenshot.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Handles display of a single screenshot.
55
*/
66

7-
function ScreenshotController(Screenshot, User) {
7+
function ScreenshotController(Screenshot, User, screenshot) {
88

99
this.screenshot = {};
1010

@@ -15,9 +15,11 @@ function ScreenshotController(Screenshot, User) {
1515
this.screenshot = data;
1616
});
1717
};
18+
1819
this.getScreenshot();
20+
1921
}
20-
ScreenshotController.$inject = ['Screenshot', 'User'];
22+
ScreenshotController.$inject = ['Screenshot', 'User', 'screenshot'];
2123

2224
angular.module('Archivr.screenshot', [
2325
'Archivr.services.Screenshot',

client/app/src/js/controllers/screenshots.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
* Handles display of a user screenshots.
55
*/
66

7-
function ScreenshotsController($location, screenshots, Screenshot) {
7+
function ScreenshotsController($stateParams, $location, screenshots, Screenshot, User) {
8+
89
this.screenshots = screenshots;
910
this.url = '';
11+
this.isUser = User.getUser() !== null && User.getUser().username === $stateParams.username;
12+
1013
this.addScreenshot = function(url){
1114
Screenshot.addScreenshot(url)
1215
.success(function(data){
@@ -26,9 +29,11 @@ function ScreenshotsController($location, screenshots, Screenshot) {
2629
}
2730

2831
ScreenshotsController.$inject = [
32+
'$stateParams',
2933
'$location',
3034
'screenshots',
31-
'Screenshot'
35+
'Screenshot',
36+
'User'
3237
];
3338

3439
angular.module('Archivr.screenshots', [

client/app/src/js/controllers/userPage.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
function UserPageController($stateParams, User) {
8-
98
// Get User object for reference
109
// this.user = new User($stateParams.username);
1110

client/app/src/js/services/Auth.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Responsible for authenticating user. Handles JWT from server which contains
55
* user model data. JWT is stored in localStorage as 'com.archivr'.
66
*/
7-
function Auth($http, $location, $window) {
7+
function Auth($http, $location, $window, User) {
88
// sign a user in
99
var signin = function(user) {
1010
return $http({
@@ -32,23 +32,21 @@ function Auth($http, $location, $window) {
3232
};
3333

3434
var signout = function() {
35-
console.log('in services... signout!!!');
35+
console.log('Logging user out, destroying JWT');
3636
$window.localStorage.removeItem('com.archivr');
37+
User.setUser(null);
3738
$location.path('/login');
38-
console.log('hello');
3939
};
4040

41-
4241
return {
4342
signin: signin,
4443
signup: signup,
4544
isAuth: isAuth,
4645
signout: signout
4746
};
4847
}
49-
Auth.$inject = ['$http', '$location', '$window'];
50-
48+
Auth.$inject = ['$http', '$location', '$window', 'User'];
5149

5250
angular.module('Archivr.services.Auth', [
51+
'Archivr.services.User'
5352
]).factory('Auth', Auth);
54-

client/app/src/js/services/Screenshot.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ function Screenshot($http, User) {
1717

1818
var addScreenshot = function (url) {
1919
var user = User.getUser();
20-
console.log(user);
2120
return $http.post('/api/user/' + user.username + '/screenshot', {
2221
url: url
2322
});
2423
};
2524

26-
var getScreenshot = function(id){
27-
var user = User.getUser();
28-
return $http.get('/api/user/' + user.username + '/screenshot/' + id)
25+
var getScreenshot = function(user, id){
26+
return $http.get('/api/user/' + user + '/screenshot/' + id)
2927
.then(function(response) {
3028
return response;
3129
});

client/app/src/js/services/User.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
function User() {
2-
3-
var user = {};
2+
var user = null;
43

54
var getUser = function () {
65
return user;
76
};
87

9-
var setUser = function(user){
10-
user = user;
8+
var setUser = function(newUser){
9+
user = newUser;
1110
};
1211

1312
return {
@@ -16,5 +15,5 @@ function User() {
1615
};
1716
}
1817

19-
angular.module('Archivr.services.User', [
20-
]).factory('User', User);
18+
angular.module('Archivr.services.User', [])
19+
.factory('User', User);

client/app/src/views/login.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<!-- inside signup -->
22

33
<div id='login'>
4-
54
<h1>Login</h1>
65
<form name="signupForm" ng-submit='authCtrl.login(username, password)'>
76
<label for="username">Enter username:</label>

client/app/src/views/profileBar.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
</div>
55
<div class="row">
66
<h1>{{ userPageCtrl.user.username }}</h1>
7+
<a href="/edit">Edit profile</a>
78
<p>Screenshots: {{ userPageCtrl.user.images.length }}</p>
89
<p>Galleries: {{ userPageCtrl.user.galleries.length }}</p>
910
</div>

client/app/src/views/screenshot.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="col-xs-11 col-md-11" ng-controller='ScreenshotController'>
1+
<div class="col-xs-12 col-md-12" ng-controller='ScreenshotController'>
22
<a href="#">
33
<img src="{{ screenshot.url }}" alt="{{ screenshot.description }}">
44
</a>
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<!-- Template for UserScreenshots -->
22

3-
<form name="newScreenshot" class="form-inline" id="newScreenshot" novalidate ng-submit="newScreenshot.$valid && screenshotsCtrl.addScreenshot(url)">
3+
<form ng-if="screenshotsCtrl.isUser" name="newScreenshot" class="form-inline" id="newScreenshot" novalidate ng-submit="newScreenshot.$valid && screenshotsCtrl.addScreenshot(url)">
44
<!-- <label for="newUrl">Enter URL:</label> -->
55
<input placeholder="Enter URL here..." id="newUrl" type="url" name="url" ng-model="url" class="form-control">
66
<button class="btn" type="submit">Get Screenshot</button>
77
</form>
88

9-
<div class="container">
10-
<ul class="row" >
9+
<div class="row">
10+
<div class="col-md-12">
11+
<ul class="list-unstyled">
1112
<li class="col-xs-6 col-md-3" ng-repeat="screenshot in screenshotsCtrl.screenshots">
1213
<a href="#" class="thumbnail">
1314
<img ng-click="screenshotCtrl.changeView(screenshot._id)" src="{{ screenshot.sizes.small.url }}" alt="{{ screenshot.sizes.small.description }}">
1415
</a>
1516
</li>
1617
</ul>
18+
</div>
1719
</div>

server/views/_partials/header.jade

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//- partial for the header bar
2-
header(role="header" id="header")
2+
header(role="header" id="header" ng-controller="NavController as NavCtrl")
33
nav.navbar.navbar-inverse.navbar-fixed-top
44
.container-fluid
55
.navbar-header
@@ -9,11 +9,11 @@ header(role="header" id="header")
99
span.icon-bar
1010
a(class="navbar-brand" href="/") Archivr
1111
ul(class="nav navbar-nav navbar-right")
12-
li
12+
li(ng-hide="NavCtrl.loggedIn")
1313
a(href="/login") Login
14-
li
14+
li(ng-hide="NavCtrl.loggedIn")
1515
a(href="/signup") Signup
16-
li
17-
a(href="/logout") Logout
16+
li(ng-show="NavCtrl.loggedIn")
17+
a(href="#" ng-click="NavCtrl.logout()") Logout
1818

1919

0 commit comments

Comments
 (0)