autenticacion.js
1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { tap } from 'ramda'
import { SENATICS_URL } from '../../constants'
export const ACCESS_TOKEN_REQUESTED = 'ACCESS_TOKEN_REQUESTED'
export const accessTokenRequested = () => ({
type: ACCESS_TOKEN_REQUESTED
})
export const ACCESS_TOKEN_RECEIVED = 'ACCESS_TOKEN_RECEIVED'
export const accessTokenReceived = (accessToken) => ({
type: ACCESS_TOKEN_RECEIVED,
token: {
accessToken,
receivedAt: Date.now()
}
})
const ACCESS_TOKEN_URL = `${SENATICS_URL}/auth/token`
export const fetchToken = (token, secret) => (dispatch) => {
dispatch(accessTokenRequested())
const payload = {
clientSecret: secret
}
const options = {
method: 'POST',
body: JSON.stringify(payload),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Basic ${token}`
},
}
const internal = () => fetch(ACCESS_TOKEN_URL, options)
.then(response => response.text())
.then(tap(text => dispatch({ type: 'TEXT_RECEIVED', text })))
.then(JSON.parse)
.then(json => dispatch(accessTokenReceived(json.accessToken)))
.catch(error => dispatch(accessTokenFailed(error)))
return internal()
}
export const ACCESS_TOKEN_FAILED = 'ACCESS_TOKEN_FAILED'
export const accessTokenFailed = (error) => ({
type: ACCESS_TOKEN_FAILED,
error
})