Giter Site home page Giter Site logo

Comments (4)

diegohaz avatar diegohaz commented on May 1, 2024

Hi, @0xsven

I'm currently working on a project that implements it. It's still unfinished, but you can check that out to see how this works: https://github.com/tramaLabs/web

These are the steps to implement that:

1. First you need to install some isomorphic cookie handler. I'm using react-cookie;

2. Create methods to automatically set tokens to the API requests on your API handler:

// ...
export const setToken = (token) => {
  api.defaults.headers.common['Authorization'] = `Bearer ${token}`
}

export const unsetToken = () => {
  delete api.defaults.headers.common['Authorization']
}
// ...

See src/services/api/index.js

If you do not want to do that, you'll need to add the token for each request.

3. Create a store to handle authentication and save/remove cookies on sagas:

// ...
import cookie from 'react-cookie'
import { setToken, unsetToken } from 'services/api'

// ...

export function* loginFlow () {
  while (true) {
    const { token } = yield take(AUTH_SUCCESS)
    yield [
      call(cookie.save, 'token', token, { path: '/' }),
      call(setToken, token)
    ]
    yield take(AUTH_LOGOUT)
    yield [
      call(cookie.remove, 'token', { path: '/' }),
      call(unsetToken)
    ]
  }
}
// ...

See src/store/auth and src/store/auth/sagas.js

4. Get/set the token on the server side:

// ...
import cookie from 'react-cookie'
import { setToken } from 'services/api'
// ...
cookie.setRawCookie(req.headers.cookie)
const token = cookie.load('token')
const memoryHistory = createMemoryHistory(req.url)
const store = configureStore({ auth: { token } }, memoryHistory)
const history = syncHistoryWithStore(memoryHistory, store)

token && setToken(token)
// ...

See src/server.js#L28-L34

5. Get/set the token on the client side:

// ...
import { fromAuth } from 'store'
import { setToken } from 'services/api'
// ...
const initialState = window.__INITIAL_STATE__ // has { auth: { token: 'foo' } }
const store = configureStore(initialState, browserHistory)
// ...
const token = fromAuth.getToken(store.getState()) // getToken is a selector on the auth store

token && setToken(token)

See src/client.js#L14-L22

Tell me if it helps you. I'll add this to the boilerplate soon.

from arc.

diegohaz avatar diegohaz commented on May 1, 2024

Also, read this section on the README for running sagas on the server side.

from arc.

0xsven avatar 0xsven commented on May 1, 2024

Step 4/5 is where I was stuck! Thank you.

I guess the api call "to get user data" I would do in some componentWillMount function of the particular component that needs the data?

Btw. this thing I found quite helpful: https://github.com/mjrussell/redux-auth-wrapper

from arc.

diegohaz avatar diegohaz commented on May 1, 2024

You can do that and it will work on the client, but the server will not wait for the request to complete. There're many ways to solve this problem. The one I like, which is adopted by this boilerplate, is what's done on SamplePage.

The caveat of this approach is that you can't do that on any component, but only on those are used by the router on src/routes.js (essentially pages).

from arc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.