Skip to content

Commit 54d1628

Browse files
committed
profiles wired up
1 parent 3b78693 commit 54d1628

18 files changed

+358
-18
lines changed

src/js/app.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import './profile';
1515
import './article';
1616
import './services';
1717
import './auth';
18+
import './settings';
1819

1920

2021
// Create and bootstrap application
@@ -27,7 +28,8 @@ const requires = [
2728
'app.profile',
2829
'app.article',
2930
'app.services',
30-
'app.auth'
31+
'app.auth',
32+
'app.settings'
3133
];
3234

3335
// Mount on window for testing

src/js/auth/auth.config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,24 @@ function AuthConfig($stateProvider, $httpProvider) {
77
url: '/login',
88
controller: 'AuthCtrl as $ctrl',
99
templateUrl: 'auth/auth.html',
10-
title: 'Sign in'
10+
title: 'Sign in',
11+
resolve: {
12+
auth: function(User) {
13+
return User.ensureAuthIs(false);
14+
}
15+
}
1116
})
1217

1318
.state('app.register', {
1419
url: '/register',
1520
controller: 'AuthCtrl as $ctrl',
1621
templateUrl: 'auth/auth.html',
17-
title: 'Sign up'
22+
title: 'Sign up',
23+
resolve: {
24+
auth: function(User) {
25+
return User.ensureAuthIs(false);
26+
}
27+
}
1828
});
1929

2030
};

src/js/auth/auth.controller.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class AuthCtrl {
33
'ngInject';
44

55
this._User = User;
6+
this._$state = $state;
67

78
this.title = $state.current.title;
89
this.authType = $state.current.name.replace('app.', '');
@@ -14,8 +15,7 @@ class AuthCtrl {
1415

1516
this._User.attemptAuth(this.authType, this.formData).then(
1617
(res) => {
17-
this.isSubmitting = false;
18-
console.log(res);
18+
this._$state.go('app.home');
1919
},
2020
(err) => {
2121
this.isSubmitting = false;

src/js/config/app.config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import authInterceptor from './auth.interceptor'
2+
13
function AppConfig($httpProvider, $stateProvider, $locationProvider, $urlRouterProvider) {
24
'ngInject';
35

6+
$httpProvider.interceptors.push(authInterceptor);
7+
48
/*
59
If you don't want hashbang routing, uncomment this line.
610
Our tutorial will be using hashbang routing though :)
@@ -10,7 +14,12 @@ function AppConfig($httpProvider, $stateProvider, $locationProvider, $urlRouterP
1014
$stateProvider
1115
.state('app', {
1216
abstract: true,
13-
templateUrl: 'layout/app-view.html'
17+
templateUrl: 'layout/app-view.html',
18+
resolve: {
19+
auth: function(User) {
20+
return User.verifyAuth();
21+
}
22+
}
1423
});
1524

1625
$urlRouterProvider.otherwise('/');

src/js/config/auth.interceptor.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function authInterceptor(JWT, AppConstants, $window, $q) {
2+
'ngInject'
3+
4+
return {
5+
// automatically attach Authorization header
6+
request: function(config) {
7+
if(config.url.indexOf(AppConstants.api) === 0 && JWT.get()) {
8+
config.headers.Authorization = 'Token ' + JWT.get();
9+
}
10+
return config;
11+
},
12+
13+
// Handle 401
14+
responseError: function(rejection) {
15+
if (rejection.status === 401) {
16+
// clear any JWT token being stored
17+
JWT.destroy();
18+
// do a hard page refresh
19+
$window.location.reload();
20+
}
21+
return $q.reject(rejection);
22+
}
23+
24+
}
25+
}
26+
27+
export default authInterceptor;

src/js/layout/header.component.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
class AppHeaderCtrl {
2-
constructor(AppConstants) {
2+
constructor(AppConstants, User, $scope) {
33
'ngInject';
44

55
this.appName = AppConstants.appName;
6+
this.currentUser = User.current;
7+
8+
$scope.$watch('User.current', (newUser) => {
9+
this.currentUser = newUser;
10+
})
611
}
712
}
813

src/js/layout/header.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,17 @@
5959
<li class="nav-item">
6060
<a class="nav-link"
6161
ui-sref-active="active"
62-
ui-sref="app.profile">
63-
My Profile
62+
ui-sref="app.settings">
63+
<i class="ion-gear-a"></i>&nbsp;Settings
64+
</a>
65+
</li>
66+
67+
<li class="nav-item">
68+
<a class="nav-link"
69+
ui-sref-active="active"
70+
ui-sref="app.profile({ username: $ctrl.currentUser.username})">
71+
<img ng-src="{{$ctrl.currentUser.image}}" class="user-pic" />
72+
{{ $ctrl.currentUser.username }}
6473
</a>
6574
</li>
6675

src/js/profile/profile.config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ function ProfileConfig($stateProvider) {
33

44
$stateProvider
55
.state('app.profile', {
6-
url: '/profile',
6+
url: '/@:username',
77
controller: 'ProfileCtrl',
88
controllerAs: '$ctrl',
9-
templateUrl: 'profile/profile.html'
9+
templateUrl: 'profile/profile.html',
10+
resolve: {
11+
profile: function(Profile, $state, $stateParams) {
12+
return Profile.get($stateParams.username).then(
13+
(profile) => profile,
14+
(err) => $state.go('app.home')
15+
)
16+
}
17+
}
18+
1019
});
1120

1221
};

src/js/profile/profile.controller.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
class ProfileCtrl {
2-
constructor() {
2+
constructor(profile, User) {
33
'ngInject';
44

5+
this.profile = profile;
6+
7+
if (User.current) {
8+
this.isUser = (User.current.username === this.profile.username);
9+
} else {
10+
this.isUser = false;
11+
}
12+
513
}
614
}
715

src/js/profile/profile.html

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66
<div class="row">
77
<div class="col-xs-12 col-md-10 offset-md-1">
88

9-
<img class="user-img" />
10-
<h4>BradGreen</h4>
11-
<p>I'm the dude running the Angular team at Google, yo.</p>
9+
<img ng-src="{{::$ctrl.profile.image}}" class="user-img" />
10+
<h4 ng-bind="::$ctrl.profile.username"></h4>
11+
<p ng-bind="::$ctrl.profile.bio"></p>
1212

13-
<button class="btn btn-sm btn-outline-secondary action-btn">
13+
<a ui-sref="app.settings"
14+
class="btn btn-sm btn-outline-secondary action-btn"
15+
ng-show="$ctrl.isUser">
16+
<i class="ion-gear-a"></i> Edit Profile Settings
17+
</a>
18+
19+
<button class="btn btn-sm btn-outline-secondary action-btn"
20+
ng-hide="$ctrl.isUser">
1421
<i class="ion-plus-round"></i>
1522
&nbsp;
16-
Follow BradGreen
23+
Follow
1724
</button>
1825

1926
</div>

0 commit comments

Comments
 (0)