-
-
Save saito-sv/a32f02754735f693936b927e3f37fcdb to your computer and use it in GitHub Desktop.
React + Redux + localStorage Login example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ApolloClient, { createNetworkInterface } from 'apollo-client'; | |
import { API_TOKEN } from './constants/LocalStorage'; | |
import { URL } from './constants/URLs'; | |
const networkInterface = createNetworkInterface({uri: URL}); | |
networkInterface.use([{ | |
applyMiddleware(request, next) { | |
const currentApiToken = localStorage.getItem(API_TOKEN); | |
if (!currentApiToken) { | |
next(); | |
return; | |
} | |
if (!request.options.headers) { | |
request.options.headers = new Headers(); | |
} | |
delete request.options.headers.map; | |
request.options.headers.Authorization = `Bearer ${currentApiToken}`; | |
next(); | |
} | |
}]); | |
const client = new ApolloClient({ | |
networkInterface, | |
// other stuff... | |
}); | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// reducers/currentApiToken.js | |
import { browserHistory } from 'react-router'; | |
import { SET_CURRENT_API_TOKEN, UNSET_CURRENT_API_TOKEN } from '../constants/EventTypes'; | |
import { API_TOKEN } from '../constants/LocalStorage'; | |
const initialState = localStorage.getItem(API_TOKEN); | |
export default function(state = initialState, event) { | |
switch (event.type) { | |
case SET_CURRENT_API_TOKEN: | |
localStorage.setItem(API_TOKEN, event.value); | |
window.location = "/"; | |
return event.value; | |
case UNSET_CURRENT_API_TOKEN: | |
localStorage.removeItem(API_TOKEN); | |
window.location = "/"; | |
return initialState; | |
default: | |
return state; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// actions/currentApiToken.js | |
import { SET_CURRENT_API_TOKEN, UNSET_CURRENT_API_TOKEN } from '../constants/EventTypes'; | |
export function setCurrentApiToken(value) { | |
let action = { | |
type: SET_CURRENT_API_TOKEN, | |
value: value | |
}; | |
return action; | |
} | |
export function unsetCurrentApiToken(value) { | |
let action = { | |
type: UNSET_CURRENT_API_TOKEN, | |
value: value | |
}; | |
return action; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// components/Login/Login.js | |
class Login extends Component { | |
// ... | |
handleSubmit(evt) { | |
evt.preventDefault(); | |
this.props.mutate(this.state) | |
.then(({ data }) => { | |
this.handleLogin(data); | |
}).catch((error) => { | |
this.setState({password: null, error: true}); | |
console.warn('there was an error sending the query', error); | |
}); | |
} | |
handleLogin(data){ | |
const { result } = data; | |
if (result && result.token) { | |
this.setState({error: false}); | |
this.props.setCurrentApiToken(result.token); | |
} | |
if (this.state.error) { | |
this.refs.password.focus(); | |
} | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment