Comments (4)
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']
}
// ...
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)
// ...
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)
Tell me if it helps you. I'll add this to the boilerplate soon.
from arc.
Also, read this section on the README for running sagas on the server side.
from arc.
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.
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)
- Running from dist HOT 1
- A lot of packages is out of date HOT 2
- Arc ssr (server side) CSS can not loaded. HOT 1
- r
- Error with import components HOT 6
- Looking for a Typescript version of components/index.js HOT 1
- Question about how 'components' is exported in the example project HOT 1
- GraphQl Branch HOT 3
- Updating to webpack 4
- Updating the SSR version to webpack 4 : new webpack config HOT 1
- Implement React Portals HOT 4
- Non root path build
- crashes on heroku, but not locally.
- injectGlobal is not supported
- `process.env` Object in config.js empty on Browser
- change crlf to lf
- Molecule inside molecule
- Discussion: are people still using Atomic design in 2021? HOT 2
- eslint bug
- Mui
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arc.