Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1776254

Browse files
authored
Add permissions example (kriasoft#64)
1 parent 5788874 commit 1776254

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Context.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ class Context {
122122
.select('comments.id', db.raw('count(comment_points.comment_id)'))
123123
.then(mapToValues(keys, x => x.id, x => x.count)),
124124
);
125+
126+
/*
127+
* Authenticatinon and permissions.
128+
*/
129+
130+
ensureIsAuthenticated() {
131+
if (!this.user) throw new Error('Anonymous access is denied.');
132+
}
125133
}
126134

127135
export default Context;

src/__tests__/Context.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Node.js API Starter Kit (https://reactstarter.com/nodejs)
3+
*
4+
* Copyright © 2016-present Kriasoft, LLC. All rights reserved.
5+
*
6+
* This source code is licensed under the MIT license found in the
7+
* LICENSE.txt file in the root directory of this source tree.
8+
*/
9+
10+
/* eslint-env jest */
11+
12+
import Context from '../Context';
13+
14+
describe('Context', () => {
15+
test('ensureIsAuthenticated()', () => {
16+
const ctx1 = new Context({ user: null });
17+
const ctx2 = new Context({ user: {} });
18+
expect(() => ctx1.ensureIsAuthenticated()).toThrow();
19+
expect(() => ctx2.ensureIsAuthenticated()).not.toThrow();
20+
});
21+
});

0 commit comments

Comments
 (0)