@@ -10,17 +10,15 @@ import {
10
10
UserIdInput ,
11
11
} from 'src/graphql.types' ;
12
12
import { ProfileService } from './profile.service' ;
13
+ import { getToken } from 'src/constants/decode' ;
13
14
14
15
@Resolver ( )
15
16
export class ProfileResolver {
16
17
constructor ( private readonly profileService : ProfileService ) { }
17
18
18
19
@Mutation ( 'authorizeGithub' )
19
20
async authorizeGithub ( @Context ( ) context ) {
20
- const authorization = context . req . headers . authorization ;
21
- const token = authorization ?. split ( ' ' ) [ 1 ] ;
22
- if ( token === undefined )
23
- throw new Error ( 'Invalid request, token not found' ) ;
21
+ const token = getToken ( context ) ;
24
22
return this . profileService . authorizeGithub ( token ) ;
25
23
}
26
24
@@ -29,19 +27,13 @@ export class ProfileResolver {
29
27
@Args ( 'input' ) input : AddUsernameInput ,
30
28
@Context ( ) context ,
31
29
) {
32
- const authorization = context . req . headers . authorization ;
33
- const token = authorization ?. split ( ' ' ) [ 1 ] ;
34
- if ( token === undefined )
35
- throw new Error ( 'Invalid request, token not found' ) ;
30
+ const token = getToken ( context ) ;
36
31
return this . profileService . addUsername ( input , token ) ;
37
32
}
38
33
39
34
@Query ( 'getUser' )
40
35
async getUser ( @Context ( ) context ) : Promise < RestrictedUserSelf > {
41
- const authorization = context . req . headers . authorization ;
42
- const token = authorization ?. split ( ' ' ) [ 1 ] ;
43
- if ( token === undefined )
44
- throw new Error ( 'Invalid request, token not found' ) ;
36
+ const token = getToken ( context ) ;
45
37
return await this . profileService . getUser ( token ) ;
46
38
}
47
39
@@ -50,9 +42,7 @@ export class ProfileResolver {
50
42
@Args ( 'input' ) input : UserIdInput ,
51
43
@Context ( ) context ,
52
44
) : Promise < RestrictedUserOther > {
53
- const token = context ?. req ?. headers ?. authorization ?. split ( ' ' ) [ 1 ] ;
54
- if ( token === undefined )
55
- throw new Error ( 'Invalid request, token not found' ) ;
45
+ const token = getToken ( context ) ;
56
46
return await this . profileService . getUserById ( input . userId , token ) ;
57
47
}
58
48
@@ -61,10 +51,7 @@ export class ProfileResolver {
61
51
@Args ( 'input' ) input : SearchInput ,
62
52
@Context ( ) context ,
63
53
) : Promise < RestrictedUserOther [ ] > {
64
- const authorization = context . req . headers . authorization ;
65
- const token = authorization ?. split ( ' ' ) [ 1 ] ;
66
- if ( token === undefined )
67
- throw new Error ( 'Invalid request, token not found' ) ;
54
+ const token = getToken ( context ) ;
68
55
return await this . profileService . search (
69
56
input . query ,
70
57
input ?. page || 1 ,
@@ -77,10 +64,7 @@ export class ProfileResolver {
77
64
@Args ( 'input' ) input : ToggleFollowInput ,
78
65
@Context ( ) context ,
79
66
) : Promise < string > {
80
- const authorization = context . req . headers . authorization ;
81
- const token = authorization ?. split ( ' ' ) [ 1 ] ;
82
- if ( token === undefined )
83
- throw new Error ( 'Invalid request, token not found' ) ;
67
+ const token = getToken ( context ) ;
84
68
return await this . profileService . toggleFollow ( input , token ) ;
85
69
}
86
70
@@ -89,10 +73,7 @@ export class ProfileResolver {
89
73
@Args ( 'input' ) input : PaginatedUserInput ,
90
74
@Context ( ) context ,
91
75
) : Promise < RestrictedUserOther [ ] > {
92
- const authorization = context . req . headers . authorization ;
93
- const token = authorization ?. split ( ' ' ) [ 1 ] ;
94
- if ( token === undefined )
95
- throw new Error ( 'Invalid request, token not found' ) ;
76
+ const token = getToken ( context ) ;
96
77
return await this . profileService . getFollowers (
97
78
input ?. page || 1 ,
98
79
input . userId ,
@@ -105,10 +86,7 @@ export class ProfileResolver {
105
86
@Args ( 'input' ) input : PaginatedUserInput ,
106
87
@Context ( ) context ,
107
88
) : Promise < RestrictedUserOther [ ] > {
108
- const authorization = context . req . headers . authorization ;
109
- const token = authorization ?. split ( ' ' ) [ 1 ] ;
110
- if ( token === undefined )
111
- throw new Error ( 'Invalid request, token not found' ) ;
89
+ const token = getToken ( context ) ;
112
90
return await this . profileService . getFollowing (
113
91
input ?. page || 1 ,
114
92
input . userId ,
@@ -121,9 +99,7 @@ export class ProfileResolver {
121
99
@Args ( 'input' ) input : DescriptionInput ,
122
100
@Context ( ) context ,
123
101
) : Promise < string > {
124
- const token = context ?. req ?. headers ?. authorization ?. split ( ' ' ) [ 1 ] ;
125
- if ( token === undefined )
126
- throw new Error ( 'Invalid request, token not found' ) ;
102
+ const token = getToken ( context ) ;
127
103
return await this . profileService . addDescription ( input . description , token ) ;
128
104
}
129
105
}
0 commit comments