Browse Source

[wip] connection to login api

Skylsmoi 6 years ago
parent
commit
81080790aa
4 changed files with 25 additions and 22 deletions
  1. 18 16
      src/action-creator.async.js
  2. 2 2
      src/container/Login.jsx
  3. 2 2
      src/container/Tracim.jsx
  4. 3 2
      src/helper.js

+ 18 - 16
src/action-creator.async.js View File

@@ -70,7 +70,7 @@ const fetchWrapper = async ({url, param, actionName, dispatch, debug = false}) =
70 70
 
71 71
 export const getLangList = () => async dispatch => {
72 72
   const fetchGetLangList = await fetchWrapper({
73
-    url: `${FETCH_CONFIG.apiUrl}/lang`,
73
+    url: `${FETCH_CONFIG.mockApiUrl}/lang`,
74 74
     param: {...FETCH_CONFIG.header, method: 'GET'},
75 75
     actionName: LANG,
76 76
     dispatch
@@ -80,7 +80,7 @@ export const getLangList = () => async dispatch => {
80 80
 
81 81
 export const getTimezone = () => async dispatch => {
82 82
   const fetchGetTimezone = await fetchWrapper({
83
-    url: `${FETCH_CONFIG.apiUrl}/timezone`,
83
+    url: `${FETCH_CONFIG.mockApiUrl}/timezone`,
84 84
     param: {...FETCH_CONFIG.header, method: 'GET'},
85 85
     actionName: TIMEZONE,
86 86
     dispatch
@@ -88,16 +88,16 @@ export const getTimezone = () => async dispatch => {
88 88
   if (fetchGetTimezone.status === 200) dispatch(setTimezone(fetchGetTimezone.json))
89 89
 }
90 90
 
91
-export const userLogin = (login, password, rememberMe) => async dispatch => {
91
+export const postUserLogin = (login, password, rememberMe) => async dispatch => {
92 92
   const fetchUserLogin = await fetchWrapper({
93
-    url: `${FETCH_CONFIG.apiUrl}/user/login`,
93
+    url: `${FETCH_CONFIG.apiUrl}/sessions/login`,
94 94
     param: {
95
-      ...FETCH_CONFIG.header,
95
+      headers: {...FETCH_CONFIG.headers},
96 96
       method: 'POST',
97 97
       body: JSON.stringify({
98
-        login,
99
-        password,
100
-        remember_me: rememberMe
98
+        email: login,
99
+        password: password
100
+        // remember_me: rememberMe
101 101
       })
102 102
     },
103 103
     actionName: USER_LOGIN,
@@ -106,19 +106,21 @@ export const userLogin = (login, password, rememberMe) => async dispatch => {
106 106
   if (fetchUserLogin.status === 200) dispatch(setUserConnected(fetchUserLogin.json))
107 107
 }
108 108
 
109
-export const getIsUserConnected = () => async dispatch => {
109
+export const getUserIsConnected = () => async dispatch => {
110 110
   const fetchUserLogged = await fetchWrapper({
111
-    url: `${FETCH_CONFIG.apiUrl}/user/is_logged_in`,
111
+    url: `${FETCH_CONFIG.apiUrl}/sessions/whoami`,
112 112
     param: {...FETCH_CONFIG.header, method: 'GET'},
113 113
     actionName: USER_CONNECTED,
114 114
     dispatch
115 115
   })
116 116
   if (fetchUserLogged.status === 200) dispatch(setUserConnected(fetchUserLogged.json))
117
+  else if (fetchUserLogged.status === 401) dispatch(setUserConnected({logged: false}))
118
+  else dispatch(setUserConnected({logged: undefined}))
117 119
 }
118 120
 
119 121
 export const getUserRole = user => async dispatch => {
120 122
   const fetchGetUserRole = await fetchWrapper({
121
-    url: `${FETCH_CONFIG.apiUrl}/user/${user.id}/roles`,
123
+    url: `${FETCH_CONFIG.mockApiUrl}/user/${user.id}/roles`,
122 124
     param: {...FETCH_CONFIG.header, method: 'GET'},
123 125
     actionName: USER_ROLE,
124 126
     dispatch
@@ -128,7 +130,7 @@ export const getUserRole = user => async dispatch => {
128 130
 
129 131
 export const updateUserLang = newLang => async dispatch => { // unused
130 132
   const fetchUpdateUserLang = await fetchWrapper({
131
-    url: `${FETCH_CONFIG.apiUrl}/user`,
133
+    url: `${FETCH_CONFIG.mockApiUrl}/user`,
132 134
     param: {...FETCH_CONFIG.header, method: 'PATCH', body: JSON.stringify({lang: newLang})},
133 135
     actionName: USER_DATA,
134 136
     dispatch
@@ -148,7 +150,7 @@ export const updateUserLang = newLang => async dispatch => { // unused
148 150
 
149 151
 export const getWorkspaceList = (userId, workspaceIdToOpen) => async dispatch => {
150 152
   const fetchGetWorkspaceList = await fetchWrapper({
151
-    url: `${FETCH_CONFIG.apiUrl}/user/${userId}/workspace`,
153
+    url: `${FETCH_CONFIG.mockApiUrl}/user/${userId}/workspace`,
152 154
     param: {...FETCH_CONFIG.header, method: 'GET'},
153 155
     actionName: WORKSPACE_LIST,
154 156
     dispatch
@@ -161,7 +163,7 @@ export const getWorkspaceList = (userId, workspaceIdToOpen) => async dispatch =>
161 163
 
162 164
 export const getWorkspaceContent = (workspaceId, filterStr) => async dispatch => {
163 165
   const fetchGetWorkspaceContent = await fetchWrapper({
164
-    url: `${FETCH_CONFIG.apiUrl}/workspace/${workspaceId}`,
166
+    url: `${FETCH_CONFIG.mockApiUrl}/workspace/${workspaceId}`,
165 167
     param: {...FETCH_CONFIG.header, method: 'GET'},
166 168
     actionName: WORKSPACE,
167 169
     dispatch
@@ -171,7 +173,7 @@ export const getWorkspaceContent = (workspaceId, filterStr) => async dispatch =>
171 173
 
172 174
 export const getFolderContent = (workspaceId, folderId) => async dispatch => {
173 175
   const fetchGetFolderContent = await fetchWrapper({
174
-    url: `${FETCH_CONFIG.apiUrl}/workspace/${workspaceId}/folder/${folderId}`,
176
+    url: `${FETCH_CONFIG.mockApiUrl}/workspace/${workspaceId}/folder/${folderId}`,
175 177
     param: {...FETCH_CONFIG.header, method: 'GET'},
176 178
     actionName: `${WORKSPACE}/${FOLDER}`,
177 179
     dispatch
@@ -181,7 +183,7 @@ export const getFolderContent = (workspaceId, folderId) => async dispatch => {
181 183
 
182 184
 export const getAppList = () => async dispatch => {
183 185
   const fetchGetAppList = await fetchWrapper({
184
-    url: `${FETCH_CONFIG.apiUrl}/app/config`,
186
+    url: `${FETCH_CONFIG.mockApiUrl}/app/config`,
185 187
     param: {...FETCH_CONFIG.header, method: 'GET'},
186 188
     actionName: APP_LIST,
187 189
     dispatch

+ 2 - 2
src/container/Login.jsx View File

@@ -3,7 +3,7 @@ import { connect } from 'react-redux'
3 3
 import { Redirect } from 'react-router'
4 4
 import LoginLogo from '../component/Login/LoginLogo.jsx'
5 5
 import LoginLogoImg from '../img/logoTracimWhite.svg'
6
-import { userLogin } from '../action-creator.async.js'
6
+import { postUserLogin } from '../action-creator.async.js'
7 7
 import Card from '../component/common/Card/Card.jsx'
8 8
 import CardHeader from '../component/common/Card/CardHeader.jsx'
9 9
 import CardBody from '../component/common/Card/CardBody.jsx'
@@ -30,7 +30,7 @@ class Login extends React.Component {
30 30
     const { history, dispatch } = this.props
31 31
     const { inputLogin, inputPassword, inputRememberMe } = this.state
32 32
 
33
-    await dispatch(userLogin(inputLogin, inputPassword, inputRememberMe))
33
+    await dispatch(postUserLogin(inputLogin, inputPassword, inputRememberMe))
34 34
     history.push('/')
35 35
   }
36 36
 

+ 2 - 2
src/container/Tracim.jsx View File

@@ -15,12 +15,12 @@ import PrivateRoute from './PrivateRoute.jsx'
15 15
 import { PAGE_NAME } from '../helper.js'
16 16
 import {
17 17
   getLangList,
18
-  getIsUserConnected
18
+  getUserIsConnected
19 19
 } from '../action-creator.async.js'
20 20
 
21 21
 class Tracim extends React.Component {
22 22
   componentDidMount () {
23
-    this.props.dispatch(getIsUserConnected())
23
+    this.props.dispatch(getUserIsConnected())
24 24
     this.props.dispatch(getLangList())
25 25
   }
26 26
 

+ 3 - 2
src/helper.js View File

@@ -1,9 +1,10 @@
1 1
 export const FETCH_CONFIG = {
2
-  header: {
2
+  headers: {
3 3
     'Accept': 'application/json',
4 4
     'Content-Type': 'application/json'
5 5
   },
6
-  apiUrl: 'http://localhost:3001'
6
+  apiUrl: 'http://localhost:6543/api/v2',
7
+  mockApiUrl: 'http://localhost:3001'
7 8
 }
8 9
 
9 10
 export const PAGE_NAME = {