Skip to content

Instantly share code, notes, and snippets.

@jarretmoses
Last active September 3, 2020 11:06
Show Gist options
  • Select an option

  • Save jarretmoses/6f73caebc50e30a2ab6fd964fe4f4ae5 to your computer and use it in GitHub Desktop.

Select an option

Save jarretmoses/6f73caebc50e30a2ab6fd964fe4f4ae5 to your computer and use it in GitHub Desktop.

Revisions

  1. jarretmoses revised this gist Mar 26, 2018. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions auth-token-header-set.js
    Original file line number Diff line number Diff line change
    @@ -22,12 +22,14 @@ export const basePost = ({ url, data }: BasePost) => api.post(url, data);

    // auth.js
    import { api } from '../services/api';
    ...code...
    /* ...code... */

    if (json.authorized) {
    // Set Axios instance to use a global header
    api.defaults.headers.common['Authorization-Header'] = json.token;

    await dispatch(setAuthorization(json));
    dispatch(push(dashboardRoute));
    }
    ...code...

    /* ...code... */
  2. jarretmoses created this gist Mar 26, 2018.
    33 changes: 33 additions & 0 deletions auth-token-header-set.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // api.js

    // @flow
    import axios from 'axios';
    import { BASE_URL } from '../constants/api';

    type BaseFetch = {
    url: string,
    }

    type BasePost = {
    url: string,
    data: Object
    }

    export const api = axios.create({
    baseURL: BASE_URL
    });

    export const baseFetch = ({ url }: BaseFetch) => api.get(url);
    export const basePost = ({ url, data }: BasePost) => api.post(url, data);

    // auth.js
    import { api } from '../services/api';
    ...code...
    if (json.authorized) {
    // Set Axios instance to use a global header
    api.defaults.headers.common['Authorization-Header'] = json.token;

    await dispatch(setAuthorization(json));
    dispatch(push(dashboardRoute));
    }
    ...code...