@paralect/hive 0.1.32 → 0.1.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paralect/hive",
3
- "version": "0.1.32",
3
+ "version": "0.1.34",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -0,0 +1,39 @@
1
+ import db from 'db';
2
+ import config from 'app-config';
3
+
4
+ const userService = db.services.users;
5
+ const tokenService = db.services.tokens;
6
+
7
+ const storeTokenToState = async (ctx) => {
8
+ let accessToken = ctx.cookies.get('access_token');
9
+
10
+ const { authorization } = ctx.headers;
11
+
12
+ if (!accessToken && authorization) {
13
+ accessToken = authorization.replace('Bearer', '').trim();
14
+ }
15
+
16
+ if (!accessToken && config._hive.authHeaderName && ctx.headers[config._hive.authHeaderName]) {
17
+ accessToken = ctx.headers[config._hive.authHeaderName];
18
+ }
19
+
20
+ ctx.state.accessToken = accessToken;
21
+ };
22
+
23
+ export default async (ctx, next) => {
24
+ storeTokenToState(ctx);
25
+
26
+ let token;
27
+
28
+ if (ctx.state.accessToken) {
29
+ token = await tokenService.findOne({ token: ctx.state.accessToken });
30
+ }
31
+
32
+ if (token) {
33
+ ctx.state.user = await userService.findOne({ _id: token.user._id });
34
+
35
+ if (token.metadata) {
36
+ ctx.state.user.authMetadata = token.metadata;
37
+ }
38
+ }
39
+ };
@@ -1,52 +1,18 @@
1
- import db from 'db';
2
- import config from 'app-config';
3
-
4
- const userService = db.services.users;
5
- const tokenService = db.services.tokens;
6
-
7
- const storeTokenToState = async (ctx) => {
8
- let accessToken = ctx.cookies.get('access_token');
9
-
10
- const { authorization } = ctx.headers;
11
-
12
- if (!accessToken && authorization) {
13
- accessToken = authorization.replace('Bearer', '').trim();
14
- }
15
-
16
- if (!accessToken && config._hive.authHeaderName && ctx.headers[config._hive.authHeaderName]) {
17
- accessToken = ctx.headers[config._hive.authHeaderName];
18
- }
19
-
20
- ctx.state.accessToken = accessToken;
21
- };
1
+ import attachUser from './attachUser';
22
2
 
23
3
  export default async (ctx, next) => {
24
- storeTokenToState(ctx);
25
-
26
- let token;
27
-
28
4
  if (ctx.state.isSkipAuth) {
29
5
  ctx.state.user = null;
30
6
  return next();
31
7
  }
32
8
 
33
- if (ctx.state.accessToken) {
34
- token = await tokenService.findOne({ token: ctx.state.accessToken });
35
- }
36
-
37
- if (token) {
38
- ctx.state.user = await userService.findOne({ _id: token.user._id });
39
-
40
- if (token.metadata) {
41
- ctx.state.user.authMetadata = token.metadata;
9
+ await attachUser(ctx, async () => {
10
+ if (ctx.state.user) {
11
+ return next();
42
12
  }
43
- }
44
-
45
- if (ctx.state.user) {
46
- return next();
47
- }
48
13
 
49
- ctx.status = 401;
50
- ctx.body = {};
51
- return null;
14
+ ctx.status = 401;
15
+ ctx.body = {};
16
+ return null;
17
+ });
52
18
  };