Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit f4b2da1

Browse files
committed
feat(user): add user entity
Add user entity for logged in user which tracks auth state and avoid dirty checking on auth service of aurelia-auth plugin. --- config.js | 3 +-- src/resources/entities/logged-in-user.js | 19 +++++++++++++++++++ .../view-elements/navigation-side.html | 4 ++-- src/resources/view-elements/navigation-side.js | 13 ++++--------- .../view-elements/navigation-top.html | 6 +++--- src/resources/view-elements/navigation-top.js | 13 +++++-------- src/view-models/news/view.js | 10 ++++------ src/views/news/view.html | 4 ++-- 8 files changed, 40 insertions(+), 32 deletions(-)
1 parent 30470d9 commit f4b2da1

File tree

8 files changed

+40
-32
lines changed

8 files changed

+40
-32
lines changed

config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ System.config({
1212
"github:*": "jspm_packages/github/*",
1313
"npm:*": "jspm_packages/npm/*"
1414
},
15-
1615
map: {
1716
"aurelia-animator-css": "npm:aurelia-animator-css@1.0.1",
1817
"aurelia-api": "npm:aurelia-api@3.0.0",
@@ -270,4 +269,4 @@ System.config({
270269
"indexof": "npm:indexof@0.0.1"
271270
}
272271
}
273-
});
272+
});
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {inject} from 'aurelia-framework';
2+
import {EventAggregator} from 'aurelia-event-aggregator';
3+
import {AuthService} from 'aurelia-authentication';
4+
5+
@inject(EventAggregator, AuthService)
6+
export class LoggedInUser {
7+
isLoggedIn = false;
8+
9+
constructor(eventAggregator, authService) {
10+
this.authService = authService;
11+
this.isLoggedIn = this.authService.isAuthenticated();
12+
this.eventAggregator = eventAggregator;
13+
this.eventAggregator.subscribe('authentication-change', this.authStateChanged.bind(this));
14+
}
15+
16+
authStateChanged(authenticated) {
17+
this.isLoggedIn = authenticated;
18+
}
19+
}

src/resources/view-elements/navigation-side.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
</a>
1717
</li>
1818
</ul>
19-
<hr class="nav-separator" if.bind="isAuthenticated">
20-
<ul class="nav-container" if.bind="isAuthenticated">
19+
<hr class="nav-separator" if.bind="loggedInUser.isLoggedIn">
20+
<ul class="nav-container" if.bind="loggedInUser.isLoggedIn">
2121
<li>
2222
<a href="#/news" class="nav-item-toggle">
2323
<i class="nav-item-icon fa fa-home fa-fw"></i> Settlement
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import {bindable, inject} from 'aurelia-framework';
2-
import {AuthService} from 'aurelia-authentication';
2+
import {LoggedInUser} from '../entities/logged-in-user';
33

4-
@inject(AuthService)
4+
@inject(LoggedInUser)
55
export class NavigationSide {
6-
_isAuthenticated = false;
76
@bindable router = null;
87

9-
constructor(auth) {
10-
this.auth = auth;
11-
}
12-
13-
get isAuthenticated() {
14-
return this.auth.isAuthenticated();
8+
constructor(loggedInUser) {
9+
this.loggedInUser = loggedInUser;
1510
}
1611
}

src/resources/view-elements/navigation-top.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
<nav-toggler></nav-toggler>
44
<a class="navbar-brand" href="#">${router.title}</a>
55
<ul class="nav navbar-nav pull-right">
6-
<li class="nav-item" if.bind="!isAuthenticated">
6+
<li class="nav-item" if.bind="!loggedInUser.isLoggedIn">
77
<a class="nav-link" href="#/users/signup">
88
<i class="fa fa-user fa-fw"></i> <span class="hidden-xs-down">Sign Up</span>
99
</a>
1010
</li>
11-
<li class="nav-item" if.bind="!isAuthenticated">
11+
<li class="nav-item" if.bind="!loggedInUser.isLoggedIn">
1212
<a class="nav-link" href="#/auth/signin">
1313
<i class="fa fa-sign-in fa-fw"></i> <span class="hidden-xs-down">Sign In</span>
1414
</a>
1515
</li>
16-
<li class="nav-item" if.bind="isAuthenticated">
16+
<li class="nav-item" if.bind="loggedInUser.isLoggedIn">
1717
<a class="nav-link" href="#/auth/signout">
1818
<i class="fa fa-sign-out fa-fw"></i> <span class="hidden-xs-down">Sign Out</span>
1919
</a>
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import {bindable, inject} from 'aurelia-framework';
2-
import {AuthService} from 'aurelia-authentication';
32

4-
@inject(AuthService)
3+
import {LoggedInUser} from '../entities/logged-in-user';
4+
5+
@inject(LoggedInUser)
56
export class NavigationTop {
67
@bindable router = null;
78

8-
constructor(auth) {
9-
this.auth = auth;
10-
}
11-
12-
get isAuthenticated() {
13-
return this.auth.isAuthenticated();
9+
constructor(loggedInUser) {
10+
this.loggedInUser = loggedInUser;
1411
}
1512
}

src/view-models/news/view.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ import {inject} from 'aurelia-framework';
22
import {AuthService} from 'aurelia-authentication';
33
import {NotificationService} from 'aurelia-notify';
44
import {DataListController} from 'resources/features/data-list/controller';
5+
import {LoggedInUser} from 'resources/entities/logged-in-user';
56
import {NewsService} from '../../services/news/news-service';
67
import {NewsCommentsService} from '../../services/news/news-comments-service';
78

89
const ENTER_KEY = 13;
910

10-
@inject(NewsService, NewsCommentsService, AuthService, NotificationService)
11+
@inject(NewsService, NewsCommentsService, AuthService, NotificationService, LoggedInUser)
1112
export class View {
1213
comments = [];
1314

14-
constructor(newsService, newsCommentsService, authService, notificationService) {
15+
constructor(newsService, newsCommentsService, authService, notificationService, loggedInUser) {
1516
this.newsService = newsService;
1617
this.newsCommentsService = newsCommentsService;
1718
this.authService = authService;
1819
this.notificationService = notificationService;
20+
this.loggedInUser = loggedInUser;
1921

2022
this.dataListController = new DataListController(options => this.loadMore(options));
2123
}
@@ -74,8 +76,4 @@ export class View {
7476
loadMore(page) {
7577
return this.newsCommentsService.getAll(this.newsId, page);
7678
}
77-
78-
get isAuthenticated() {
79-
return this.authService.isAuthenticated();
80-
}
8179
}

src/views/news/view.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h4><a href="#/news/view/${news.id}">${news.title}</a></h4>
2424
</div>
2525

2626
<div class="card-footer">
27-
<div class="news-card-comment mb-1" if.bind="isAuthenticated">
27+
<div class="news-card-comment mb-1" if.bind="loggedInUser.isLoggedIn">
2828
<a href="#/profiles/view/${user.id}" class="pull-left">
2929
<!-- Todo: image path -->
3030
<img alt="${user.username}" src="http://lr.local/img/avatar/${user.avatar}">
@@ -65,7 +65,7 @@ <h4><a href="#/news/view/${news.id}">${news.title}</a></h4>
6565
</div>
6666
</data-list>
6767

68-
<div class="news-card-comment" if.bind="isAuthenticated">
68+
<div class="news-card-comment" if.bind="loggedInUser.isLoggedIn">
6969
<a href="">Comment...</a>
7070
</div>
7171
</div>

0 commit comments

Comments
 (0)