Skip to content

Commit 35efbd5

Browse files
author
vikasrohit
committed
AS#160071805908634, Handle jwt token in api calls
-- Extracted common logic for adding auth token into base class
1 parent 3431b29 commit 35efbd5

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

src/projects/list/actions/loadProjects.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export function loadProjects(searchTerm) {
1515
api : projectService,
1616
method: 'getProjects',
1717
args: {searchTerm},
18-
success : () => { console.log('dispatch success action') },
18+
success : (resp) => {
19+
console.log('dispatch success action')
20+
console.log(resp)
21+
},
1922
failure : () => { console.log('dispatch failure action') }
2023
}
2124
}
@@ -53,4 +56,4 @@ export function projectSuggestions(searchTerm) {
5356
})
5457

5558
})
56-
}
59+
}

src/services/baseService.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class BaseService {
2+
constructor() {
3+
this.token = null
4+
}
5+
6+
setToken(token) {
7+
this.token = token
8+
}
9+
10+
getToken() {
11+
return this.token
12+
}
13+
14+
getOptions(requiresAuth) {
15+
const options = {}
16+
const myHeaders = new Headers()
17+
if (requiresAuth) {
18+
myHeaders.append('Authorization', 'Basic ' + this.token) //TODO use constants
19+
}
20+
options.headers = myHeaders
21+
return options
22+
}
23+
}
24+
25+
export default BaseService

src/services/projectService.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// import _ from 'lodash'
1+
import BaseService from './baseService'
22
import { fetchJSON } from '../helpers'
33

4-
class ProjectService {
4+
class ProjectService extends BaseService {
55
constructor() {
6-
this.token = null
6+
super()
77
this.getProjects = this.getProjects.bind(this)
88
this.getOptions = this.getOptions.bind(this)
99
}
@@ -15,29 +15,12 @@ class ProjectService {
1515
return false
1616
}
1717

18-
setToken(token) {
19-
this.token = token
20-
}
21-
22-
getToken() {
23-
return this.token
24-
}
25-
2618
getProjects(searchTerm) {
19+
console.log(searchTerm)
2720
const options = this.getOptions(true)
2821
options.method = 'GET' //TODO use constants
2922
return fetchJSON('https://api.topcoder-dev.com/v3/challenges/', options)
3023
}
31-
32-
getOptions(requiresAuth) {
33-
const options = {}
34-
const myHeaders = new Headers()
35-
if (requiresAuth) {
36-
myHeaders.append('Authorization', 'Basic ' + this.token) //TODO use constants
37-
}
38-
options.headers = myHeaders
39-
return options
40-
}
4124
}
4225

4326
export default new ProjectService()

0 commit comments

Comments
 (0)