Skip to content

Commit 6a01034

Browse files
committed
feat: example-with-cookie done
Signed-off-by: Arkadip Bhattacharya <[email protected]>
0 parents  commit 6a01034

18 files changed

+11461
-0
lines changed

example-with-cookie/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

example-with-cookie/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2+
3+
## Available Scripts
4+
5+
In the project directory, you can run:
6+
7+
### `yarn start`
8+
9+
Runs the app in the development mode.<br />
10+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11+
12+
The page will reload if you make edits.<br />
13+
You will also see any lint errors in the console.
14+
15+
### `yarn test`
16+
17+
Launches the test runner in the interactive watch mode.<br />
18+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19+
20+
### `yarn build`
21+
22+
Builds the app for production to the `build` folder.<br />
23+
It correctly bundles React in production mode and optimizes the build for the best performance.
24+
25+
The build is minified and the filenames include the hashes.<br />
26+
Your app is ready to be deployed!
27+
28+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29+
30+
### `yarn eject`
31+
32+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33+
34+
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35+
36+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37+
38+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39+
40+
## Learn More
41+
42+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43+
44+
To learn React, check out the [React documentation](https://reactjs.org/).
45+
46+
### Code Splitting
47+
48+
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49+
50+
### Analyzing the Bundle Size
51+
52+
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53+
54+
### Making a Progressive Web App
55+
56+
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57+
58+
### Advanced Configuration
59+
60+
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61+
62+
### Deployment
63+
64+
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65+
66+
### `yarn build` fails to minify
67+
68+
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify

example-with-cookie/package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "example-with-cookie",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^4.2.4",
7+
"@testing-library/react": "^9.3.2",
8+
"@testing-library/user-event": "^7.1.2",
9+
"react": "^16.13.1",
10+
"react-auth-kit": "^1.2.1",
11+
"react-dom": "^16.13.1",
12+
"react-router-dom": "^5.2.0",
13+
"react-scripts": "3.4.3"
14+
},
15+
"scripts": {
16+
"start": "react-scripts start",
17+
"build": "react-scripts build",
18+
"test": "react-scripts test",
19+
"eject": "react-scripts eject"
20+
},
21+
"eslintConfig": {
22+
"extends": "react-app"
23+
},
24+
"browserslist": {
25+
"production": [
26+
">0.2%",
27+
"not dead",
28+
"not op_mini all"
29+
],
30+
"development": [
31+
"last 1 chrome version",
32+
"last 1 firefox version",
33+
"last 1 safari version"
34+
]
35+
},
36+
"devDependencies": {
37+
"@types/react-auth-kit": "^1.2.0"
38+
}
39+
}
3.08 KB
Binary file not shown.

example-with-cookie/public/index.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>
5.22 KB
Loading
9.44 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}

example-with-cookie/public/robots.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *
3+
Disallow:

example-with-cookie/src/App.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import { AuthProvider } from 'react-auth-kit'
3+
import Routes from './Routes';
4+
5+
function App() {
6+
return (
7+
<AuthProvider
8+
authStorageType="cookie"
9+
authStorageName="_auth_t"
10+
authTimeStorageName="_auth_time"
11+
stateStorageName="_auth_state"
12+
cookieDomain={window.location.hostname}
13+
cookieSecure={window.location.protocol === "https:"}>
14+
<Routes/>
15+
</AuthProvider>
16+
);
17+
}
18+
19+
export default App;

example-with-cookie/src/Routes.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
import { PrivateRoute } from 'react-auth-kit'
3+
import { BrowserRouter, Route, Switch } from 'react-router-dom'
4+
import Home from './components/Home'
5+
import Login from './components/Login'
6+
import SecureComponent from './components/SecureComponent'
7+
8+
const Routes = () => {
9+
return (
10+
<BrowserRouter>
11+
<Switch>
12+
<Route path={'/'} component={Home} exact/>
13+
<Route path={'/login' } component={Login} exact/>
14+
<PrivateRoute path={'/secure'} component={SecureComponent} loginPath={'/login'} exact/>
15+
</Switch>
16+
</BrowserRouter>
17+
)
18+
}
19+
20+
export default Routes
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react'
2+
import { useHistory } from 'react-router-dom'
3+
4+
const Home = () => {
5+
const history = useHistory()
6+
7+
return (
8+
<div>
9+
<button onClick={()=> history.push('/login')}>Go to Login</button>
10+
<button onClick={()=> history.push('/secure')}>GO to Secure Dashboard</button>
11+
</div>
12+
)
13+
}
14+
15+
export default Home
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react'
2+
import { useIsAuthenticated, useSignIn } from 'react-auth-kit'
3+
import { Redirect, useHistory } from 'react-router-dom'
4+
5+
const Login = () => {
6+
const isAuthenticated = useIsAuthenticated()
7+
const signIn = useSignIn()
8+
const history = useHistory()
9+
10+
/**
11+
* Login Handle, the callback function onClick from the "Login" button
12+
*
13+
* This function demostrate a dummy authentication, using useSignIn function
14+
*/
15+
const loginHandler = () => {
16+
// Assuming that, all network Request is successfull, and the user is authenticated
17+
18+
if (signIn({
19+
token: '35v3443bn368367n306306wbn407qn420b436b4', //Just a random token
20+
tokenType: 'Bearer', // Token type set as Bearer
21+
authState: { name: 'React User', uid: 123456 }, // Dummy auth user state
22+
expiresIn: 120 // Token Expriration time, in minutes
23+
})) {
24+
// If Login Successfull, then Redirect the user to secure route
25+
history.push('/secure')
26+
} else {
27+
// Else, there must be some error. So, throw an error
28+
alert("Error Occoured. Try Again")
29+
}
30+
}
31+
32+
if (isAuthenticated()) {
33+
// If authenticated user, then redirect to secure dashboard
34+
return (
35+
<Redirect to={'/secure'} />
36+
)
37+
} else {
38+
// If not authenticated, use the login flow
39+
// For Demostration, I'm using just a button to login.
40+
// In reality, there should be a form, validation, nwetowrk request and other things
41+
return (
42+
<button onClick={loginHandler}>Log In!!</button>
43+
)
44+
}
45+
}
46+
47+
export default Login
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
import { useAuthUser, useSignOut } from 'react-auth-kit'
3+
4+
const SecureComponent = () => {
5+
const signOut = useSignOut()
6+
const authUser = useAuthUser()
7+
8+
return (
9+
<div>
10+
<p>{`Hello ${authUser().name}, your U-ID is: ${authUser().uid}`}</p>
11+
<button onClick={() => signOut()}>Sign Out!</button>
12+
</div>
13+
)
14+
}
15+
16+
export default SecureComponent

example-with-cookie/src/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
import * as serviceWorker from './serviceWorker';
5+
6+
ReactDOM.render(
7+
<React.StrictMode>
8+
<App />
9+
</React.StrictMode>,
10+
document.getElementById('root')
11+
);
12+
13+
// If you want your app to work offline and load faster, you can change
14+
// unregister() to register() below. Note this comes with some pitfalls.
15+
// Learn more about service workers: https://bit.ly/CRA-PWA
16+
serviceWorker.unregister();

0 commit comments

Comments
 (0)