| 219 | } |
| 220 | |
| 221 | export function doInit() { |
| 222 | return async (dispatch) => { |
| 223 | let currentUser = null; |
| 224 | if (!config.isBackend) { |
| 225 | currentUser = mockUser; |
| 226 | |
| 227 | dispatch({ |
| 228 | type: 'LOGIN_SUCCESS', |
| 229 | payload: { |
| 230 | currentUser, |
| 231 | }, |
| 232 | }); |
| 233 | } else { |
| 234 | try { |
| 235 | let token = localStorage.getItem('token'); |
| 236 | if (token) { |
| 237 | currentUser = await findMe(); |
| 238 | } |
| 239 | if (currentUser?.id) { |
| 240 | sessionStorage.setItem('user_id', currentUser.id); |
| 241 | } else { |
| 242 | sessionStorage.removeItem('user_id'); |
| 243 | } |
| 244 | dispatch({ |
| 245 | type: 'LOGIN_SUCCESS', |
| 246 | payload: { |
| 247 | currentUser, |
| 248 | }, |
| 249 | }); |
| 250 | } catch (error) { |
| 251 | console.log(error); |
| 252 | |
| 253 | dispatch({ |
| 254 | type: 'AUTH_INIT_ERROR', |
| 255 | payload: error, |
| 256 | }); |
| 257 | } |
| 258 | } |
| 259 | }; |
| 260 | } |
| 261 | |
| 262 | export function registerUser( |
| 263 | dispatch, |