UNPKG

fractal-auth

Version:
122 lines (106 loc) 3.64 kB
import { createBrowserHistory } from 'history'; import { accountService } from './accountService'; import Cookies from 'universal-cookie'; const { FRACTAL_CORE_LOGIN_URL } = require('./config.json'); const product_id = process.env.REACT_APP_PRODUCT_ID ? process.env.REACT_APP_PRODUCT_ID : 'Main'; export const roles = { Admin: 'admin', Staff: 'staff', Partner: 'partner', User: 'user' } export const rolesArray = [ { value: 'admin', label: "Admin" }, { value: 'staff', label: "Staff" }, { value: 'partner', label: "Partner" }, { value: 'user', label: "User" }, ] export const history = createBrowserHistory(); export const fetchWrapper = { get, post, put, delete: _delete, upload } const cookies = new Cookies(); function get(url) { const rt = cookies.get('authtoken'); const requestOptions = { method: 'GET', headers: { ...authHeader(url) }, credentials: 'include', cookies: rt }; return fetch(url, requestOptions).then(handleResponse); } function post(url, body) { const authToken = cookies.get('authtoken'); const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', ...authHeader(url), 'authtoken': authToken }, credentials: 'include', body: JSON.stringify(body), cookies: authToken }; return fetch(url, requestOptions).then(handleResponse); } function upload(url, formData) { const requestOptions = { method: 'POST', headers: { ...authHeader(url) }, credentials: 'include', body: formData }; return fetch(url, requestOptions).then(handleResponse); } function put(url, body) { const requestOptions = { method: 'PUT', headers: { 'Content-Type': 'application/json', ...authHeader(url) }, credentials: 'include', body: JSON.stringify(body) }; return fetch(url, requestOptions).then(handleResponse); } // prefixed with underscored because delete is a reserved word in javascript function _delete(url) { const requestOptions = { method: 'DELETE', headers: authHeader(url) }; return fetch(url, requestOptions).then(handleResponse); } // helper functions function authHeader(url) { // return auth header with jwt if user is logged in and request is to the api url const user = accountService.userValue; let isLoggedIn = user && user.jwtToken; let jwtToken = user ? user.jwtToken : ''; if (!isLoggedIn) { jwtToken = cookies.get('jwtToken'); if (jwtToken) { isLoggedIn = true; } } const isApiUrl = url.startsWith(process.env.REACT_APP_API_URL) || url.startsWith(FRACTAL_CORE_LOGIN_URL || url.startsWith(FRACTAL_CORE_LOGIN_URL_DEV)); if (isLoggedIn && isApiUrl) { return { Authorization: `Bearer ${jwtToken}`, product: product_id }; } else { return { product: product_id }; } } function handleResponse(response) { return response.text().then(text => { const data = text && JSON.parse(text); if (!response.ok) { if ([401, 403].includes(response.status) && accountService.userValue) { // auto logout if 401 Unauthorized or 403 Forbidden response returned from api accountService.logout(); } const error = (data && data.message) || response.statusText; return Promise.reject(error); } return data; }); }