Skip to content

Commit cf1999a

Browse files
authored
Merge pull request #71 from inblack67/pr/context-api
Authenticate User On Page Refresh In Context API
2 parents a932d41 + c57ab5e commit cf1999a

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ You can download the PDF and Epub version of this repository from the latest run
359359
|322| [What is the purpose of eslint plugin for hooks?](#what-is-the-purpose-of-eslint-plugin-for-hooks)|
360360
|323| [What is the difference between Imperative and Declarative in React?](#what-is-the-difference-between-imperative-and-declarative-in-react)|
361361
|324| [What are the benefits of using typescript with reactjs?](#what-are-the-benefits-of-using-typescript-with-reactjs)|
362+
|325| [How do you make sure that user remains authenticated on page refresh while using Context API State Management?](#how-do-you-make-sure-that-user-remains-authenticated-on-page-refresh-while-using-Context-API-State-Management?)|
362363

363364
## Core React
364365

@@ -6551,3 +6552,75 @@ You can download the PDF and Epub version of this repository from the latest run
65516552
2. Use of interfaces for complex type definitions
65526553
3. IDEs such as VS Code was made for TypeScript
65536554
4. Avoid bugs with the ease of readability and Validation
6555+
6556+
**[⬆ Back to Top](#table-of-contents)**
6557+
6558+
325. ### How do you make sure that user remains authenticated on page refresh while using Context API State Management?
6559+
When a user logs in and reload, to persist the state generally we add the load user action in the useEffect hooks in the main App.js. While using Redux, loadUser action can be easily accessed.
6560+
6561+
**App.js**
6562+
6563+
```js
6564+
import {lodUser} from '../actions/auth';
6565+
store.dispatch(loadUser());
6566+
```
6567+
6568+
* But while using **Context API**, to access context in App.js, wrap the AuthState in index.js so that App.js can access the auth context. Now whenever the page reloads, no matter what route you are on, the user will be authenticated as **loadUser** action will be triggered on each re-render.
6569+
6570+
**index.js**
6571+
6572+
```js
6573+
import React from 'react';
6574+
import ReactDOM from 'react-dom';
6575+
import App from './App';
6576+
import AuthState from './context/auth/AuthState'
6577+
6578+
ReactDOM.render(
6579+
<React.StrictMode>
6580+
<AuthState>
6581+
<App />
6582+
</AuthState>
6583+
</React.StrictMode>,
6584+
document.getElementById('root')
6585+
);
6586+
```
6587+
**App.js**
6588+
6589+
```js
6590+
const authContext = useContext(AuthContext);
6591+
6592+
const { loadUser } = authContext;
6593+
6594+
useEffect(() => {
6595+
loadUser();
6596+
},[])
6597+
```
6598+
6599+
**loadUser**
6600+
6601+
```js
6602+
const loadUser = async () => {
6603+
const token = sessionStorage.getItem('token');
6604+
6605+
if(!token){
6606+
dispatch({
6607+
type: ERROR
6608+
})
6609+
}
6610+
setAuthToken(token);
6611+
6612+
try {
6613+
const res = await axios('/api/auth');
6614+
6615+
dispatch({
6616+
type: USER_LOADED,
6617+
payload: res.data.data
6618+
})
6619+
6620+
} catch (err) {
6621+
console.error(err);
6622+
}
6623+
}
6624+
```
6625+
6626+
**[⬆ Back to Top](#table-of-contents)**

0 commit comments

Comments
 (0)