Skip to content

Commit 70e0085

Browse files
author
vikasrohit
authored
Merge pull request #6 from appirio-tech/feature/jwt-token-middleware
Feature/jwt token middleware
2 parents 60803c4 + 35efbd5 commit 70e0085

File tree

6 files changed

+44
-31
lines changed

6 files changed

+44
-31
lines changed

src/components/AuthenticatedComponent.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export function requiresAuthentication(Component) {
2020
checkAuth() {
2121
getFreshToken().then(() => {
2222
this.setState({isLoggedIn: true})
23-
}).catch(() => {
23+
}).catch((error) => {
24+
console.log(error)
2425
// FIXME should we include hash, search etc
2526
const redirectBackToUrl = window.location.origin + '/' + this.props.location.pathname
2627
const newLocation = ACCOUNTS_APP_LOGIN_URL + '?retUrl=' + redirectBackToUrl

src/config/store.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import thunk from 'redux-thunk'
22
import { createStore, applyMiddleware, compose } from 'redux'
33
import reducers from '../reducers'
4-
import apiMiddleware from './apiMiddleware'
4+
import apiMiddleware from '../middleware/apiMiddleware'
55
// import jwt from '../middleware/jwt'
66

77

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
function apiMiddleware({ dispatch, getState }) {
22
return next => action => {
33
console.log(dispatch)
4-
console.log(next)
4+
// console.log(next)
55
const {
66
API_CALL
77
// payload = {}
88
} = action
99
if (!API_CALL) {
10-
return //next(action)
10+
return next(action)
1111
}
1212

1313
// const config = action[API_CALL]

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: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
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
7-
// this.getProjects = getProjects.bind(this)
6+
super()
7+
this.getProjects = this.getProjects.bind(this)
8+
this.getOptions = this.getOptions.bind(this)
89
}
910

1011
requiresAuth(methodName) {
@@ -14,29 +15,12 @@ class ProjectService {
1415
return false
1516
}
1617

17-
setToken(token) {
18-
this.token = token
19-
}
20-
21-
getToken() {
22-
return this.token
23-
}
24-
2518
getProjects(searchTerm) {
26-
const options = this.getOptions()
19+
console.log(searchTerm)
20+
const options = this.getOptions(true)
2721
options.method = 'GET' //TODO use constants
28-
return fetchJSON('http://api.topcoder-dev.com/v4/projects?q=' + searchTerm, options)
29-
}
30-
31-
getOptions(requiresAuth) {
32-
const options = {}
33-
const myHeaders = new Headers()
34-
if (requiresAuth) {
35-
myHeaders.append('Authorization', 'Basic ' + this.token) //TODO use constants
36-
}
37-
options.headers = myHeaders
38-
return options
22+
return fetchJSON('https://api.topcoder-dev.com/v3/challenges/', options)
3923
}
4024
}
4125

42-
export default new ProjectService()
26+
export default new ProjectService()

0 commit comments

Comments
 (0)