├── .gitignore
├── README.md
├── client
├── .gitignore
├── README.md
├── package-lock.json
├── package.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── robots.txt
└── src
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── auth.js
│ ├── component
│ ├── home.js
│ ├── otpVerify.js
│ ├── phoneInput.js
│ ├── stepForm.js
│ └── styles
│ │ ├── home.module.css
│ │ └── style.module.css
│ ├── img
│ ├── abstract background.webp
│ └── home.svg
│ ├── index.css
│ ├── index.js
│ ├── serviceWorker.js
│ └── setupTests.js
├── package-lock.json
├── package.json
└── server.js
/.gitignore:
--------------------------------------------------------------------------------
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
17 | .env.local
18 | .env.development.local
19 | .env.test.local
20 | .env.production.local
21 |
22 | npm-debug.log*
23 | yarn-debug.log*
24 | yarn-error.log*
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # React-Node-Passwordless-Auth
2 | Passwordless Authentication of User in Node(express) and React using JWT and Twilio SMS API without and any database for validating OTP
3 |
--------------------------------------------------------------------------------
/client/.gitignore:
--------------------------------------------------------------------------------
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*
24 |
--------------------------------------------------------------------------------
/client/README.md:
--------------------------------------------------------------------------------
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 | ### `npm start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `npm test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `npm run build`
21 |
22 | Builds the app for production to the `build` folder.
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.
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 | ### `npm run 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 | ### `npm run 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
69 |
--------------------------------------------------------------------------------
/client/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "client",
3 | "version": "0.1.0",
4 | "type": "module",
5 | "private": true,
6 | "dependencies": {
7 | "@testing-library/jest-dom": "^4.2.4",
8 | "@testing-library/react": "^9.5.0",
9 | "@testing-library/user-event": "^7.2.1",
10 | "axios": "^0.20.0",
11 | "browser-cookies": "^1.2.0",
12 | "ms.macro": "^2.0.0",
13 | "react": "^16.13.1",
14 | "react-dom": "^16.13.1",
15 | "react-router-dom": "^5.2.0",
16 | "react-scripts": "3.4.3",
17 | "universal-cookie": "^4.0.4"
18 | },
19 | "scripts": {
20 | "start": "react-scripts start",
21 | "build": "react-scripts build",
22 | "test": "react-scripts test",
23 | "eject": "react-scripts eject"
24 | },
25 | "eslintConfig": {
26 | "extends": "react-app"
27 | },
28 | "browserslist": {
29 | "production": [
30 | ">0.2%",
31 | "not dead",
32 | "not op_mini all"
33 | ],
34 | "development": [
35 | "last 1 chrome version",
36 | "last 1 firefox version",
37 | "last 1 safari version"
38 | ]
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/client/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MeRahulAhire/React-Node-Passwordless-Auth/a251fcc21b76d4136ad62ac8430077240c5b3b7f/client/public/favicon.ico
--------------------------------------------------------------------------------
/client/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | PixCase App
28 |
29 |
30 | You need to enable JavaScript to run this app.
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/client/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MeRahulAhire/React-Node-Passwordless-Auth/a251fcc21b76d4136ad62ac8430077240c5b3b7f/client/public/logo192.png
--------------------------------------------------------------------------------
/client/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MeRahulAhire/React-Node-Passwordless-Auth/a251fcc21b76d4136ad62ac8430077240c5b3b7f/client/public/logo512.png
--------------------------------------------------------------------------------
/client/public/manifest.json:
--------------------------------------------------------------------------------
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 | }
26 |
--------------------------------------------------------------------------------
/client/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/client/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | height: 40vmin;
7 | pointer-events: none;
8 | }
9 |
10 | @media (prefers-reduced-motion: no-preference) {
11 | .App-logo {
12 | animation: App-logo-spin infinite 20s linear;
13 | }
14 | }
15 |
16 | .App-header {
17 | background-color: #282c34;
18 | min-height: 100vh;
19 | display: flex;
20 | flex-direction: column;
21 | align-items: center;
22 | justify-content: center;
23 | font-size: calc(10px + 2vmin);
24 | color: white;
25 | }
26 |
27 | .App-link {
28 | color: #61dafb;
29 | }
30 |
31 | @keyframes App-logo-spin {
32 | from {
33 | transform: rotate(0deg);
34 | }
35 | to {
36 | transform: rotate(360deg);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/client/src/App.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import StepForm from './component/stepForm';
3 | import auth from './auth';
4 | import Home from './component/home';
5 |
6 | function App() {
7 | if (auth.isAuthenticated()) return ;
8 | else return ;
9 | }
10 |
11 | export default App;
12 |
--------------------------------------------------------------------------------
/client/src/App.test.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { render } from '@testing-library/react';
3 | import App from './App';
4 |
5 | test('renders learn react link', () => {
6 | const { getByText } = render( );
7 | const linkElement = getByText(/learn react/i);
8 | expect(linkElement).toBeInTheDocument();
9 | });
10 |
--------------------------------------------------------------------------------
/client/src/auth.js:
--------------------------------------------------------------------------------
1 | import axios from 'axios';
2 | import Cookies from 'universal-cookie';
3 | const cookies = new Cookies();
4 |
5 | axios.defaults.withCredentials = true;
6 | class Auth {
7 | constructor() {
8 | this.authenticated = false;
9 | }
10 |
11 | isAuthenticated() {
12 | const accessToken = cookies.get('authSession');
13 | const refreshToken = cookies.get('refreshTokenID');
14 | if (!accessToken && !refreshToken) {
15 | return (this.authenticated = false);
16 | }
17 | if (accessToken && refreshToken) {
18 | return (this.authenticated = true);
19 | }
20 | if (!accessToken && refreshToken) {
21 | axios
22 | .post('http://localhost:8888/refresh', {
23 | withCredentials: true
24 | })
25 | .then(function(res) {
26 | console.log(res.data);
27 |
28 | window.location.reload();
29 | })
30 | .catch(function(error) {
31 | console.log(error.response);
32 | });
33 | }
34 | }
35 | }
36 |
37 | export default new Auth();
38 |
--------------------------------------------------------------------------------
/client/src/component/home.js:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import axios from 'axios';
3 | import styles from './styles/home.module.css';
4 |
5 | axios.defaults.withCredentials = true;
6 | function Home() {
7 | const [ state, setState ] = useState({
8 | value: 'Private Protected Route - Home'
9 | });
10 |
11 | /* The UseEffect below is used to verify the working of Protected Route.
12 | Do not use it aimlessly as it will cause performance issue and server timout due to infinite execution in loop
13 |
14 | useEffect(() => {
15 | console.log(state.value)
16 | axios
17 | .post('http://localhost:8888/home', {
18 | withCredentials: true
19 | })
20 | .then(function(res) {
21 | // console.log(res.data);
22 | setState({ ...state, value: res.data });
23 | })
24 | .catch(function(error) {
25 | console.log(error.response);
26 | });
27 | },[state]);
28 | */
29 |
30 | const logout = () => {
31 | axios
32 | .get('http://localhost:8888/logout')
33 | .then((res) => {
34 | console.log(res.data);
35 | })
36 | .catch((err) => {
37 | console.log(err.response);
38 | });
39 | window.location.reload();
40 | };
41 | return (
42 |
43 |
46 |
47 |
48 | Log out
49 |
50 |
51 |
52 |
{state.value}
53 |
54 |
55 | );
56 | }
57 |
58 | export default Home;
59 |
--------------------------------------------------------------------------------
/client/src/component/otpVerify.js:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import styles from './styles/style.module.css';
3 | import axios from 'axios';
4 |
5 | function OtpVerify(props) {
6 | axios.defaults.withCredentials = true;
7 |
8 | const [ error, setError ] = useState({
9 | error: '',
10 | success: ''
11 | });
12 | const { value, handleChange } = props;
13 | const back = (e) => {
14 | e.preventDefault();
15 | props.prevStep();
16 | };
17 |
18 | const confirmOtp = () => {
19 | axios
20 | .post('http://localhost:8888/verifyOTP', {
21 | phone: `${value.phone}`,
22 | hash: `${value.hash}`,
23 | otp: `${value.otp}`,
24 | withCredentials: true
25 | })
26 | .then(function(res) {
27 | console.log(res.data);
28 | window.location.reload();
29 | })
30 | .catch(function(error) {
31 | console.log(error.response.data);
32 | setError({ ...error, error: error.response.data.msg });
33 | });
34 | };
35 | return (
36 |
37 |
38 |
39 |
PixCase
40 |
{error.error}
41 |
{error.success}
42 |
Enter One Time Password:
43 |
44 |
51 |
52 |
53 | Back
54 |
55 |
56 | Confirm OTP
57 |
58 |
59 |
60 |
61 | );
62 | }
63 |
64 | export default OtpVerify;
65 |
--------------------------------------------------------------------------------
/client/src/component/phoneInput.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import styles from './styles/style.module.css';
3 | import axios from 'axios';
4 | function PhoneInput(props) {
5 | const { value, handleChange, hashHandleChange } = props;
6 |
7 | const Continue = (e) => {
8 | axios
9 | .post('http://localhost:8888/sendOTP', {
10 | phone: `${value.phone}`
11 | })
12 | .then(function(res) {
13 | console.log(res.data.otp);
14 | const hash = res.data.hash;
15 | hashHandleChange(hash);
16 | });
17 |
18 | e.preventDefault();
19 | props.nextStep();
20 | };
21 | return (
22 |
23 |
24 |
25 |
PixCase
26 |
27 |
Phone number:
28 |
29 |
36 |
37 |
38 | Send OTP
39 |
40 |
41 |
42 |
43 | );
44 | }
45 |
46 | export default PhoneInput;
47 |
--------------------------------------------------------------------------------
/client/src/component/stepForm.js:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react';
2 | import PhoneInput from './phoneInput';
3 | import Otpverify from './otpVerify';
4 | const StepForm = () => {
5 | const [ state, setState ] = useState({
6 | phone: '',
7 | hash: '',
8 | otp: ''
9 | });
10 |
11 | const [step, setStep] = useState(1)
12 |
13 | const handleChange = (input) => (e) => {
14 | setState({...state, [input]: e.target.value });
15 | };
16 | const hashHandleChange = (hash) => {
17 | setState({...state, hash : hash})
18 | }
19 | const nextStep = () => {
20 |
21 | setStep(prevStep => prevStep + 1)
22 | };
23 |
24 | const prevStep = () => {
25 |
26 | setStep(prevStep => prevStep - 1)
27 | };
28 |
29 | const { phone, hash, otp } = state;
30 | const value = { phone, hash, otp };
31 |
32 | switch (step) {
33 | case 1:
34 | return ;
35 | case 2:
36 | return ;
37 | default:
38 | return
39 |
40 | }
41 | };
42 |
43 | export default StepForm;
44 |
--------------------------------------------------------------------------------
/client/src/component/styles/home.module.css:
--------------------------------------------------------------------------------
1 | @import url('https://fonts.googleapis.com/css2?family=Staatliches&display=swap');
2 | @import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');
3 | @import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');
4 |
5 | *{
6 | margin: 0;
7 | padding: 0;
8 | }
9 | .top{
10 | height: 15vh;
11 | font-family: 'Staatliches', cursive;
12 | text-align: center;
13 | color: #009688;
14 | background-color: #1a1a1a;
15 | font-size: 3rem;
16 | display: grid;
17 | place-items: center;
18 |
19 | }
20 | .bottom{
21 | height: 85vh;
22 | background-color: #272c34;
23 | width: 100%;
24 |
25 | }
26 | .card{
27 | margin: auto;
28 | height: 400px;
29 | width: 500px;
30 | background-image: url('../../img/home.svg');
31 | background-size: contain;
32 | background-position: center;
33 | background-repeat: no-repeat;
34 | }
35 | .logout{
36 | width: 200px;
37 | height: 50px;
38 | font-size: 2rem;
39 | background-color: #ba000d;
40 | outline: none;
41 | border: none;
42 | color: white;
43 | float: right;
44 | cursor: pointer;
45 | }
46 | .logout:hover{
47 | background-color: red;
48 | }
49 | .words{
50 | font-family: 'Open Sans', sans-serif;
51 | text-align: center;
52 | color: white;
53 | font-size: 2rem;
54 | }
--------------------------------------------------------------------------------
/client/src/component/styles/style.module.css:
--------------------------------------------------------------------------------
1 | @import url('https://fonts.googleapis.com/css2?family=Staatliches&display=swap');
2 | @import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');
3 | @import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');
4 | *{
5 | padding: 0;
6 | margin: 0;
7 |
8 | }
9 |
10 | .background{
11 | height: 100vh;
12 | width: 100%;
13 | background-image: url('../../img/abstract background.webp');
14 | /* background-image: linear-gradient(135deg, #1e88e5, #0d47a1); */
15 | display: grid;
16 | place-items: center;
17 | }
18 |
19 | .container{
20 | height: 400px;
21 | width: 400px;
22 | /* background-image: linear-gradient(135deg, #1e88e5, #0d47a1); */
23 | background-color: #f7f7f7;
24 | border-radius: 15px;
25 |
26 | }
27 |
28 | .heading{
29 | font-family: 'Staatliches', cursive;
30 | text-align: center;
31 | color: #009688;
32 | font-size: 3rem;
33 | margin-top: 1rem;
34 | }
35 | .input_container{
36 | display: grid;
37 | place-items: center;
38 |
39 | }
40 | .input_text{
41 | font-family: 'Open Sans', sans-serif;
42 | font-size: 1.5rem;
43 | margin-top: 4rem;
44 | margin-left: 17px;
45 |
46 | }
47 | .input{
48 | width: 90%;
49 | height: 40px;
50 | font-size: 1.5rem;
51 | font-family: 'Open Sans', sans-serif;
52 | border: 2px solid black;
53 | margin-top: 0.5rem;
54 | border-radius: 5px;
55 | background-color: none;
56 | }
57 |
58 | .submit {
59 | margin-right: 17px;
60 | background-color: #000;
61 | height: 40px;
62 | width: 100px;
63 | margin-top: 1rem;
64 | border: 0px;
65 | color: white;
66 | font-family: Arial, Helvetica, sans-serif;
67 | border-radius: 20px;
68 | float: right;
69 | outline: none;
70 | cursor: pointer;
71 | }
72 | .back {
73 | margin-left: 17px;
74 | background-color: red;
75 | height: 40px;
76 | width: 100px;
77 | margin-top: 1rem;
78 | border: 0px;
79 | color: white;
80 | font-family: Arial, Helvetica, sans-serif;
81 | border-radius: 20px;
82 | float: left;
83 | outline: none;
84 | cursor: pointer;
85 | }
86 | .submit:hover{
87 | background-color: #1976d2;
88 | }
89 |
90 | .error{
91 | color: red;
92 | font-family: Arial, Helvetica, sans-serif;
93 | text-align: center;
94 | }
95 | .success{
96 | color: green;
97 | font-family: Arial, Helvetica, sans-serif;
98 | text-align: center;
99 | }
--------------------------------------------------------------------------------
/client/src/img/abstract background.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MeRahulAhire/React-Node-Passwordless-Auth/a251fcc21b76d4136ad62ac8430077240c5b3b7f/client/src/img/abstract background.webp
--------------------------------------------------------------------------------
/client/src/img/home.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/client/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/client/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import './index.css';
4 | import App from './App';
5 | import * as serviceWorker from './serviceWorker';
6 |
7 | ReactDOM.render(
8 |
9 |
10 | ,
11 | document.getElementById('root')
12 | );
13 |
14 | // If you want your app to work offline and load faster, you can change
15 | // unregister() to register() below. Note this comes with some pitfalls.
16 | // Learn more about service workers: https://bit.ly/CRA-PWA
17 | serviceWorker.unregister();
18 |
--------------------------------------------------------------------------------
/client/src/serviceWorker.js:
--------------------------------------------------------------------------------
1 | // This optional code is used to register a service worker.
2 | // register() is not called by default.
3 |
4 | // This lets the app load faster on subsequent visits in production, and gives
5 | // it offline capabilities. However, it also means that developers (and users)
6 | // will only see deployed updates on subsequent visits to a page, after all the
7 | // existing tabs open on the page have been closed, since previously cached
8 | // resources are updated in the background.
9 |
10 | // To learn more about the benefits of this model and instructions on how to
11 | // opt-in, read https://bit.ly/CRA-PWA
12 |
13 | const isLocalhost = Boolean(
14 | window.location.hostname === 'localhost' ||
15 | // [::1] is the IPv6 localhost address.
16 | window.location.hostname === '[::1]' ||
17 | // 127.0.0.0/8 are considered localhost for IPv4.
18 | window.location.hostname.match(
19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
20 | )
21 | );
22 |
23 | export function register(config) {
24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
25 | // The URL constructor is available in all browsers that support SW.
26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
27 | if (publicUrl.origin !== window.location.origin) {
28 | // Our service worker won't work if PUBLIC_URL is on a different origin
29 | // from what our page is served on. This might happen if a CDN is used to
30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374
31 | return;
32 | }
33 |
34 | window.addEventListener('load', () => {
35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
36 |
37 | if (isLocalhost) {
38 | // This is running on localhost. Let's check if a service worker still exists or not.
39 | checkValidServiceWorker(swUrl, config);
40 |
41 | // Add some additional logging to localhost, pointing developers to the
42 | // service worker/PWA documentation.
43 | navigator.serviceWorker.ready.then(() => {
44 | console.log(
45 | 'This web app is being served cache-first by a service ' +
46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA'
47 | );
48 | });
49 | } else {
50 | // Is not localhost. Just register service worker
51 | registerValidSW(swUrl, config);
52 | }
53 | });
54 | }
55 | }
56 |
57 | function registerValidSW(swUrl, config) {
58 | navigator.serviceWorker
59 | .register(swUrl)
60 | .then(registration => {
61 | registration.onupdatefound = () => {
62 | const installingWorker = registration.installing;
63 | if (installingWorker == null) {
64 | return;
65 | }
66 | installingWorker.onstatechange = () => {
67 | if (installingWorker.state === 'installed') {
68 | if (navigator.serviceWorker.controller) {
69 | // At this point, the updated precached content has been fetched,
70 | // but the previous service worker will still serve the older
71 | // content until all client tabs are closed.
72 | console.log(
73 | 'New content is available and will be used when all ' +
74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
75 | );
76 |
77 | // Execute callback
78 | if (config && config.onUpdate) {
79 | config.onUpdate(registration);
80 | }
81 | } else {
82 | // At this point, everything has been precached.
83 | // It's the perfect time to display a
84 | // "Content is cached for offline use." message.
85 | console.log('Content is cached for offline use.');
86 |
87 | // Execute callback
88 | if (config && config.onSuccess) {
89 | config.onSuccess(registration);
90 | }
91 | }
92 | }
93 | };
94 | };
95 | })
96 | .catch(error => {
97 | console.error('Error during service worker registration:', error);
98 | });
99 | }
100 |
101 | function checkValidServiceWorker(swUrl, config) {
102 | // Check if the service worker can be found. If it can't reload the page.
103 | fetch(swUrl, {
104 | headers: { 'Service-Worker': 'script' },
105 | })
106 | .then(response => {
107 | // Ensure service worker exists, and that we really are getting a JS file.
108 | const contentType = response.headers.get('content-type');
109 | if (
110 | response.status === 404 ||
111 | (contentType != null && contentType.indexOf('javascript') === -1)
112 | ) {
113 | // No service worker found. Probably a different app. Reload the page.
114 | navigator.serviceWorker.ready.then(registration => {
115 | registration.unregister().then(() => {
116 | window.location.reload();
117 | });
118 | });
119 | } else {
120 | // Service worker found. Proceed as normal.
121 | registerValidSW(swUrl, config);
122 | }
123 | })
124 | .catch(() => {
125 | console.log(
126 | 'No internet connection found. App is running in offline mode.'
127 | );
128 | });
129 | }
130 |
131 | export function unregister() {
132 | if ('serviceWorker' in navigator) {
133 | navigator.serviceWorker.ready
134 | .then(registration => {
135 | registration.unregister();
136 | })
137 | .catch(error => {
138 | console.error(error.message);
139 | });
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/client/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom/extend-expect';
6 |
--------------------------------------------------------------------------------
/package-lock.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "SMS-OTP-Verify",
3 | "version": "1.0.0",
4 | "lockfileVersion": 1,
5 | "requires": true,
6 | "dependencies": {
7 | "@types/body-parser": {
8 | "version": "1.19.0",
9 | "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.0.tgz",
10 | "integrity": "sha512-W98JrE0j2K78swW4ukqMleo8R7h/pFETjM2DQ90MF6XK2i4LO4W3gQ71Lt4w3bfm2EvVSyWHplECvB5sK22yFQ==",
11 | "requires": {
12 | "@types/connect": "*",
13 | "@types/node": "*"
14 | }
15 | },
16 | "@types/connect": {
17 | "version": "3.4.33",
18 | "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.33.tgz",
19 | "integrity": "sha512-2+FrkXY4zllzTNfJth7jOqEHC+enpLeGslEhpnTAkg21GkRrWV4SsAtqchtT4YS9/nODBU2/ZfsBY2X4J/dX7A==",
20 | "requires": {
21 | "@types/node": "*"
22 | }
23 | },
24 | "@types/express": {
25 | "version": "4.17.8",
26 | "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.8.tgz",
27 | "integrity": "sha512-wLhcKh3PMlyA2cNAB9sjM1BntnhPMiM0JOBwPBqttjHev2428MLEB4AYVN+d8s2iyCVZac+o41Pflm/ZH5vLXQ==",
28 | "requires": {
29 | "@types/body-parser": "*",
30 | "@types/express-serve-static-core": "*",
31 | "@types/qs": "*",
32 | "@types/serve-static": "*"
33 | }
34 | },
35 | "@types/express-serve-static-core": {
36 | "version": "4.17.13",
37 | "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.13.tgz",
38 | "integrity": "sha512-RgDi5a4nuzam073lRGKTUIaL3eF2+H7LJvJ8eUnCI0wA6SNjXc44DCmWNiTLs/AZ7QlsFWZiw/gTG3nSQGL0fA==",
39 | "requires": {
40 | "@types/node": "*",
41 | "@types/qs": "*",
42 | "@types/range-parser": "*"
43 | }
44 | },
45 | "@types/mime": {
46 | "version": "2.0.3",
47 | "resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.3.tgz",
48 | "integrity": "sha512-Jus9s4CDbqwocc5pOAnh8ShfrnMcPHuJYzVcSUU7lrh8Ni5HuIqX3oilL86p3dlTrk0LzHRCgA/GQ7uNCw6l2Q=="
49 | },
50 | "@types/node": {
51 | "version": "14.11.8",
52 | "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.8.tgz",
53 | "integrity": "sha512-KPcKqKm5UKDkaYPTuXSx8wEP7vE9GnuaXIZKijwRYcePpZFDVuy2a57LarFKiORbHOuTOOwYzxVxcUzsh2P2Pw=="
54 | },
55 | "@types/qs": {
56 | "version": "6.9.4",
57 | "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.4.tgz",
58 | "integrity": "sha512-+wYo+L6ZF6BMoEjtf8zB2esQsqdV6WsjRK/GP9WOgLPrq87PbNWgIxS76dS5uvl/QXtHGakZmwTznIfcPXcKlQ=="
59 | },
60 | "@types/range-parser": {
61 | "version": "1.2.3",
62 | "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz",
63 | "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA=="
64 | },
65 | "@types/serve-static": {
66 | "version": "1.13.5",
67 | "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.5.tgz",
68 | "integrity": "sha512-6M64P58N+OXjU432WoLLBQxbA0LRGBCRm7aAGQJ+SMC1IMl0dgRVi9EFfoDcS2a7Xogygk/eGN94CfwU9UF7UQ==",
69 | "requires": {
70 | "@types/express-serve-static-core": "*",
71 | "@types/mime": "*"
72 | }
73 | },
74 | "accepts": {
75 | "version": "1.3.7",
76 | "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
77 | "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==",
78 | "requires": {
79 | "mime-types": "~2.1.24",
80 | "negotiator": "0.6.2"
81 | }
82 | },
83 | "array-flatten": {
84 | "version": "1.1.1",
85 | "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
86 | "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
87 | },
88 | "asap": {
89 | "version": "2.0.6",
90 | "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz",
91 | "integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY="
92 | },
93 | "axios": {
94 | "version": "0.19.2",
95 | "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
96 | "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
97 | "requires": {
98 | "follow-redirects": "1.5.10"
99 | }
100 | },
101 | "body-parser": {
102 | "version": "1.19.0",
103 | "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
104 | "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==",
105 | "requires": {
106 | "bytes": "3.1.0",
107 | "content-type": "~1.0.4",
108 | "debug": "2.6.9",
109 | "depd": "~1.1.2",
110 | "http-errors": "1.7.2",
111 | "iconv-lite": "0.4.24",
112 | "on-finished": "~2.3.0",
113 | "qs": "6.7.0",
114 | "raw-body": "2.4.0",
115 | "type-is": "~1.6.17"
116 | }
117 | },
118 | "buffer-equal-constant-time": {
119 | "version": "1.0.1",
120 | "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
121 | "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk="
122 | },
123 | "bytes": {
124 | "version": "3.1.0",
125 | "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
126 | "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg=="
127 | },
128 | "content-disposition": {
129 | "version": "0.5.3",
130 | "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz",
131 | "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==",
132 | "requires": {
133 | "safe-buffer": "5.1.2"
134 | }
135 | },
136 | "content-type": {
137 | "version": "1.0.4",
138 | "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz",
139 | "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA=="
140 | },
141 | "cookie": {
142 | "version": "0.4.0",
143 | "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz",
144 | "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg=="
145 | },
146 | "cookie-parser": {
147 | "version": "1.4.5",
148 | "resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.5.tgz",
149 | "integrity": "sha512-f13bPUj/gG/5mDr+xLmSxxDsB9DQiTIfhJS/sqjrmfAWiAN+x2O4i/XguTL9yDZ+/IFDanJ+5x7hC4CXT9Tdzw==",
150 | "requires": {
151 | "cookie": "0.4.0",
152 | "cookie-signature": "1.0.6"
153 | }
154 | },
155 | "cookie-signature": {
156 | "version": "1.0.6",
157 | "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
158 | "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
159 | },
160 | "cors": {
161 | "version": "2.8.5",
162 | "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
163 | "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
164 | "requires": {
165 | "object-assign": "^4",
166 | "vary": "^1"
167 | }
168 | },
169 | "dayjs": {
170 | "version": "1.9.1",
171 | "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.9.1.tgz",
172 | "integrity": "sha512-01NCTBg8cuMJG1OQc6PR7T66+AFYiPwgDvdJmvJBn29NGzIG+DIFxPLNjHzwz3cpFIvG+NcwIjP9hSaPVoOaDg=="
173 | },
174 | "debug": {
175 | "version": "2.6.9",
176 | "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
177 | "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
178 | "requires": {
179 | "ms": "2.0.0"
180 | }
181 | },
182 | "depd": {
183 | "version": "1.1.2",
184 | "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
185 | "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak="
186 | },
187 | "destroy": {
188 | "version": "1.0.4",
189 | "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
190 | "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
191 | },
192 | "dotenv": {
193 | "version": "8.2.0",
194 | "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
195 | "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw=="
196 | },
197 | "ecdsa-sig-formatter": {
198 | "version": "1.0.11",
199 | "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
200 | "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
201 | "requires": {
202 | "safe-buffer": "^5.0.1"
203 | }
204 | },
205 | "ee-first": {
206 | "version": "1.1.1",
207 | "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
208 | "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
209 | },
210 | "encodeurl": {
211 | "version": "1.0.2",
212 | "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
213 | "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k="
214 | },
215 | "escape-html": {
216 | "version": "1.0.3",
217 | "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
218 | "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg="
219 | },
220 | "etag": {
221 | "version": "1.8.1",
222 | "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
223 | "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
224 | },
225 | "express": {
226 | "version": "4.17.1",
227 | "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
228 | "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
229 | "requires": {
230 | "accepts": "~1.3.7",
231 | "array-flatten": "1.1.1",
232 | "body-parser": "1.19.0",
233 | "content-disposition": "0.5.3",
234 | "content-type": "~1.0.4",
235 | "cookie": "0.4.0",
236 | "cookie-signature": "1.0.6",
237 | "debug": "2.6.9",
238 | "depd": "~1.1.2",
239 | "encodeurl": "~1.0.2",
240 | "escape-html": "~1.0.3",
241 | "etag": "~1.8.1",
242 | "finalhandler": "~1.1.2",
243 | "fresh": "0.5.2",
244 | "merge-descriptors": "1.0.1",
245 | "methods": "~1.1.2",
246 | "on-finished": "~2.3.0",
247 | "parseurl": "~1.3.3",
248 | "path-to-regexp": "0.1.7",
249 | "proxy-addr": "~2.0.5",
250 | "qs": "6.7.0",
251 | "range-parser": "~1.2.1",
252 | "safe-buffer": "5.1.2",
253 | "send": "0.17.1",
254 | "serve-static": "1.14.1",
255 | "setprototypeof": "1.1.1",
256 | "statuses": "~1.5.0",
257 | "type-is": "~1.6.18",
258 | "utils-merge": "1.0.1",
259 | "vary": "~1.1.2"
260 | }
261 | },
262 | "finalhandler": {
263 | "version": "1.1.2",
264 | "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz",
265 | "integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==",
266 | "requires": {
267 | "debug": "2.6.9",
268 | "encodeurl": "~1.0.2",
269 | "escape-html": "~1.0.3",
270 | "on-finished": "~2.3.0",
271 | "parseurl": "~1.3.3",
272 | "statuses": "~1.5.0",
273 | "unpipe": "~1.0.0"
274 | }
275 | },
276 | "follow-redirects": {
277 | "version": "1.5.10",
278 | "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
279 | "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
280 | "requires": {
281 | "debug": "=3.1.0"
282 | },
283 | "dependencies": {
284 | "debug": {
285 | "version": "3.1.0",
286 | "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
287 | "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
288 | "requires": {
289 | "ms": "2.0.0"
290 | }
291 | }
292 | }
293 | },
294 | "forwarded": {
295 | "version": "0.1.2",
296 | "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
297 | "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ="
298 | },
299 | "fresh": {
300 | "version": "0.5.2",
301 | "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
302 | "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac="
303 | },
304 | "http-errors": {
305 | "version": "1.7.2",
306 | "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz",
307 | "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==",
308 | "requires": {
309 | "depd": "~1.1.2",
310 | "inherits": "2.0.3",
311 | "setprototypeof": "1.1.1",
312 | "statuses": ">= 1.5.0 < 2",
313 | "toidentifier": "1.0.0"
314 | }
315 | },
316 | "iconv-lite": {
317 | "version": "0.4.24",
318 | "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
319 | "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
320 | "requires": {
321 | "safer-buffer": ">= 2.1.2 < 3"
322 | }
323 | },
324 | "inherits": {
325 | "version": "2.0.3",
326 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
327 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
328 | },
329 | "ipaddr.js": {
330 | "version": "1.9.1",
331 | "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
332 | "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g=="
333 | },
334 | "jsonwebtoken": {
335 | "version": "8.5.1",
336 | "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz",
337 | "integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==",
338 | "requires": {
339 | "jws": "^3.2.2",
340 | "lodash.includes": "^4.3.0",
341 | "lodash.isboolean": "^3.0.3",
342 | "lodash.isinteger": "^4.0.4",
343 | "lodash.isnumber": "^3.0.3",
344 | "lodash.isplainobject": "^4.0.6",
345 | "lodash.isstring": "^4.0.1",
346 | "lodash.once": "^4.0.0",
347 | "ms": "^2.1.1",
348 | "semver": "^5.6.0"
349 | },
350 | "dependencies": {
351 | "ms": {
352 | "version": "2.1.2",
353 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
354 | "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
355 | }
356 | }
357 | },
358 | "jwa": {
359 | "version": "1.4.1",
360 | "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
361 | "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
362 | "requires": {
363 | "buffer-equal-constant-time": "1.0.1",
364 | "ecdsa-sig-formatter": "1.0.11",
365 | "safe-buffer": "^5.0.1"
366 | }
367 | },
368 | "jws": {
369 | "version": "3.2.2",
370 | "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz",
371 | "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
372 | "requires": {
373 | "jwa": "^1.4.1",
374 | "safe-buffer": "^5.0.1"
375 | }
376 | },
377 | "lodash": {
378 | "version": "4.17.20",
379 | "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz",
380 | "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA=="
381 | },
382 | "lodash.includes": {
383 | "version": "4.3.0",
384 | "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
385 | "integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
386 | },
387 | "lodash.isboolean": {
388 | "version": "3.0.3",
389 | "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
390 | "integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
391 | },
392 | "lodash.isinteger": {
393 | "version": "4.0.4",
394 | "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
395 | "integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
396 | },
397 | "lodash.isnumber": {
398 | "version": "3.0.3",
399 | "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
400 | "integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
401 | },
402 | "lodash.isplainobject": {
403 | "version": "4.0.6",
404 | "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
405 | "integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
406 | },
407 | "lodash.isstring": {
408 | "version": "4.0.1",
409 | "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
410 | "integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
411 | },
412 | "lodash.once": {
413 | "version": "4.1.1",
414 | "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
415 | "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
416 | },
417 | "media-typer": {
418 | "version": "0.3.0",
419 | "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
420 | "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g="
421 | },
422 | "merge-descriptors": {
423 | "version": "1.0.1",
424 | "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
425 | "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E="
426 | },
427 | "methods": {
428 | "version": "1.1.2",
429 | "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
430 | "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4="
431 | },
432 | "mime": {
433 | "version": "1.6.0",
434 | "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
435 | "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
436 | },
437 | "mime-db": {
438 | "version": "1.44.0",
439 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz",
440 | "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg=="
441 | },
442 | "mime-types": {
443 | "version": "2.1.27",
444 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz",
445 | "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==",
446 | "requires": {
447 | "mime-db": "1.44.0"
448 | }
449 | },
450 | "ms": {
451 | "version": "2.0.0",
452 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
453 | "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
454 | },
455 | "negotiator": {
456 | "version": "0.6.2",
457 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
458 | "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
459 | },
460 | "object-assign": {
461 | "version": "4.1.1",
462 | "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
463 | "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
464 | },
465 | "on-finished": {
466 | "version": "2.3.0",
467 | "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
468 | "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=",
469 | "requires": {
470 | "ee-first": "1.1.1"
471 | }
472 | },
473 | "parseurl": {
474 | "version": "1.3.3",
475 | "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
476 | "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ=="
477 | },
478 | "path-to-regexp": {
479 | "version": "0.1.7",
480 | "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
481 | "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
482 | },
483 | "pop-iterate": {
484 | "version": "1.0.1",
485 | "resolved": "https://registry.npmjs.org/pop-iterate/-/pop-iterate-1.0.1.tgz",
486 | "integrity": "sha1-zqz9q0q/NT16DyqqLB/Hs/lBO6M="
487 | },
488 | "proxy-addr": {
489 | "version": "2.0.6",
490 | "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz",
491 | "integrity": "sha512-dh/frvCBVmSsDYzw6n926jv974gddhkFPfiN8hPOi30Wax25QZyZEGveluCgliBnqmuM+UJmBErbAUFIoDbjOw==",
492 | "requires": {
493 | "forwarded": "~0.1.2",
494 | "ipaddr.js": "1.9.1"
495 | }
496 | },
497 | "q": {
498 | "version": "2.0.3",
499 | "resolved": "https://registry.npmjs.org/q/-/q-2.0.3.tgz",
500 | "integrity": "sha1-dbjbAlWhpa+C9Yw/Oqoe/sfQ0TQ=",
501 | "requires": {
502 | "asap": "^2.0.0",
503 | "pop-iterate": "^1.0.1",
504 | "weak-map": "^1.0.5"
505 | }
506 | },
507 | "qs": {
508 | "version": "6.7.0",
509 | "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
510 | "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
511 | },
512 | "querystringify": {
513 | "version": "2.2.0",
514 | "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
515 | "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ=="
516 | },
517 | "range-parser": {
518 | "version": "1.2.1",
519 | "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
520 | "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg=="
521 | },
522 | "raw-body": {
523 | "version": "2.4.0",
524 | "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz",
525 | "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==",
526 | "requires": {
527 | "bytes": "3.1.0",
528 | "http-errors": "1.7.2",
529 | "iconv-lite": "0.4.24",
530 | "unpipe": "1.0.0"
531 | }
532 | },
533 | "requires-port": {
534 | "version": "1.0.0",
535 | "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
536 | "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8="
537 | },
538 | "rootpath": {
539 | "version": "0.1.2",
540 | "resolved": "https://registry.npmjs.org/rootpath/-/rootpath-0.1.2.tgz",
541 | "integrity": "sha1-Wzeah9ypBum5HWkKWZQ5vvJn6ms="
542 | },
543 | "safe-buffer": {
544 | "version": "5.1.2",
545 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
546 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
547 | },
548 | "safer-buffer": {
549 | "version": "2.1.2",
550 | "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
551 | "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
552 | },
553 | "scmp": {
554 | "version": "2.1.0",
555 | "resolved": "https://registry.npmjs.org/scmp/-/scmp-2.1.0.tgz",
556 | "integrity": "sha512-o/mRQGk9Rcer/jEEw/yw4mwo3EU/NvYvp577/Btqrym9Qy5/MdWGBqipbALgd2lrdWTJ5/gqDusxfnQBxOxT2Q=="
557 | },
558 | "semver": {
559 | "version": "5.7.1",
560 | "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
561 | "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ=="
562 | },
563 | "send": {
564 | "version": "0.17.1",
565 | "resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz",
566 | "integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==",
567 | "requires": {
568 | "debug": "2.6.9",
569 | "depd": "~1.1.2",
570 | "destroy": "~1.0.4",
571 | "encodeurl": "~1.0.2",
572 | "escape-html": "~1.0.3",
573 | "etag": "~1.8.1",
574 | "fresh": "0.5.2",
575 | "http-errors": "~1.7.2",
576 | "mime": "1.6.0",
577 | "ms": "2.1.1",
578 | "on-finished": "~2.3.0",
579 | "range-parser": "~1.2.1",
580 | "statuses": "~1.5.0"
581 | },
582 | "dependencies": {
583 | "ms": {
584 | "version": "2.1.1",
585 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
586 | "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg=="
587 | }
588 | }
589 | },
590 | "serve-static": {
591 | "version": "1.14.1",
592 | "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz",
593 | "integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==",
594 | "requires": {
595 | "encodeurl": "~1.0.2",
596 | "escape-html": "~1.0.3",
597 | "parseurl": "~1.3.3",
598 | "send": "0.17.1"
599 | }
600 | },
601 | "setprototypeof": {
602 | "version": "1.1.1",
603 | "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz",
604 | "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw=="
605 | },
606 | "statuses": {
607 | "version": "1.5.0",
608 | "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
609 | "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
610 | },
611 | "toidentifier": {
612 | "version": "1.0.0",
613 | "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",
614 | "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw=="
615 | },
616 | "twilio": {
617 | "version": "3.49.4",
618 | "resolved": "https://registry.npmjs.org/twilio/-/twilio-3.49.4.tgz",
619 | "integrity": "sha512-sC18gso9tgnIe8P99mv6KzHxrkUUCoxfNGbQfOmtakLLwuoehUMz4WTKbA3ubcCtqtjPSTGqV9GRCDJ4gloKQQ==",
620 | "requires": {
621 | "@types/express": "^4.17.7",
622 | "@types/qs": "6.9.4",
623 | "axios": "^0.19.2",
624 | "dayjs": "^1.8.29",
625 | "jsonwebtoken": "^8.5.1",
626 | "lodash": "^4.17.19",
627 | "q": "2.0.x",
628 | "qs": "^6.9.4",
629 | "rootpath": "^0.1.2",
630 | "scmp": "^2.1.0",
631 | "url-parse": "^1.4.7",
632 | "xmlbuilder": "^13.0.2"
633 | },
634 | "dependencies": {
635 | "qs": {
636 | "version": "6.9.4",
637 | "resolved": "https://registry.npmjs.org/qs/-/qs-6.9.4.tgz",
638 | "integrity": "sha512-A1kFqHekCTM7cz0udomYUoYNWjBebHm/5wzU/XqrBRBNWectVH0QIiN+NEcZ0Dte5hvzHwbr8+XQmguPhJ6WdQ=="
639 | }
640 | }
641 | },
642 | "type-is": {
643 | "version": "1.6.18",
644 | "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
645 | "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==",
646 | "requires": {
647 | "media-typer": "0.3.0",
648 | "mime-types": "~2.1.24"
649 | }
650 | },
651 | "unpipe": {
652 | "version": "1.0.0",
653 | "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
654 | "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw="
655 | },
656 | "url-parse": {
657 | "version": "1.4.7",
658 | "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.4.7.tgz",
659 | "integrity": "sha512-d3uaVyzDB9tQoSXFvuSUNFibTd9zxd2bkVrDRvF5TmvWWQwqE4lgYJ5m+x1DbecWkw+LK4RNl2CU1hHuOKPVlg==",
660 | "requires": {
661 | "querystringify": "^2.1.1",
662 | "requires-port": "^1.0.0"
663 | }
664 | },
665 | "utils-merge": {
666 | "version": "1.0.1",
667 | "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
668 | "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
669 | },
670 | "vary": {
671 | "version": "1.1.2",
672 | "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
673 | "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
674 | },
675 | "weak-map": {
676 | "version": "1.0.5",
677 | "resolved": "https://registry.npmjs.org/weak-map/-/weak-map-1.0.5.tgz",
678 | "integrity": "sha1-eWkVhNmGB/UHC9O3CkDmuyLkAes="
679 | },
680 | "xmlbuilder": {
681 | "version": "13.0.2",
682 | "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-13.0.2.tgz",
683 | "integrity": "sha512-Eux0i2QdDYKbdbA6AM6xE4m6ZTZr4G4xF9kahI2ukSEMCzwce2eX9WlTI5J3s+NU7hpasFsr8hWIONae7LluAQ=="
684 | }
685 | }
686 | }
687 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "SMS-OTP-Verify",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "index.js",
6 | "scripts": {
7 | "start": "node server.js",
8 | "devStart": "nodemon server.js",
9 | "test": "echo \"Error: no test specified\" && exit 1"
10 | },
11 | "keywords": [],
12 | "author": "",
13 | "license": "ISC",
14 | "dependencies": {
15 | "cookie-parser": "^1.4.5",
16 | "cors": "^2.8.5",
17 | "dotenv": "^8.2.0",
18 | "express": "^4.17.1",
19 | "jsonwebtoken": "^8.5.1",
20 | "twilio": "^3.49.4"
21 | },
22 | "devDependencies": {}
23 | }
24 |
--------------------------------------------------------------------------------
/server.js:
--------------------------------------------------------------------------------
1 | require('dotenv').config();
2 |
3 | const express = require('express');
4 | const cors = require('cors');
5 | const cookieParser = require('cookie-parser');
6 |
7 | const accountSid = process.env.ACCOUNT_SID;
8 | const authToken = process.env.AUTH_TOKEN;
9 | const client = require('twilio')(accountSid, authToken);
10 |
11 | const crypto = require('crypto');
12 | const smsKey = process.env.SMS_SECRET_KEY;
13 | const twilioNum = process.env.TWILIO_PHONE_NUMBER;
14 | const jwt = require('jsonwebtoken');
15 |
16 | const JWT_AUTH_TOKEN = process.env.JWT_AUTH_TOKEN;
17 | const JWT_REFRESH_TOKEN = process.env.JWT_REFRESH_TOKEN;
18 | let refreshTokens = [];
19 |
20 | const app = express();
21 | app.use(express.json());
22 |
23 | app.use(cors({ origin: 'http://localhost:3000', credentials: true }));
24 | app.use(cookieParser());
25 |
26 | app.post('/sendOTP', (req, res) => {
27 | const phone = req.body.phone;
28 | const otp = Math.floor(100000 + Math.random() * 900000);
29 | const ttl = 2 * 60 * 1000;
30 | const expires = Date.now() + ttl;
31 | const data = `${phone}.${otp}.${expires}`;
32 | const hash = crypto.createHmac('sha256', smsKey).update(data).digest('hex');
33 | const fullHash = `${hash}.${expires}`;
34 |
35 | client.messages
36 | .create({
37 | body: `Your One Time Login Password For CFM is ${otp}`,
38 | from: twilioNum,
39 | to: phone
40 | })
41 | .then((messages) => console.log(messages))
42 | .catch((err) => console.error(err));
43 |
44 | // res.status(200).send({ phone, hash: fullHash, otp }); // this bypass otp via api only for development instead hitting twilio api all the time
45 | res.status(200).send({ phone, hash: fullHash }); // Use this way in Production
46 | });
47 |
48 | app.post('/verifyOTP', (req, res) => {
49 | const phone = req.body.phone;
50 | const hash = req.body.hash;
51 | const otp = req.body.otp;
52 | let [ hashValue, expires ] = hash.split('.');
53 |
54 | let now = Date.now();
55 | if (now > parseInt(expires)) {
56 | return res.status(504).send({ msg: 'Timeout. Please try again' });
57 | }
58 | let data = `${phone}.${otp}.${expires}`;
59 | let newCalculatedHash = crypto.createHmac('sha256', smsKey).update(data).digest('hex');
60 | if (newCalculatedHash === hashValue) {
61 | console.log('user confirmed');
62 | const accessToken = jwt.sign({ data: phone }, JWT_AUTH_TOKEN, { expiresIn: '30s' });
63 | const refreshToken = jwt.sign({ data: phone }, JWT_REFRESH_TOKEN, { expiresIn: '1y' });
64 | refreshTokens.push(refreshToken);
65 | res
66 | .status(202)
67 | .cookie('accessToken', accessToken, {
68 | expires: new Date(new Date().getTime() + 30 * 1000),
69 | sameSite: 'strict',
70 | httpOnly: true
71 | })
72 | .cookie('refreshToken', refreshToken, {
73 | expires: new Date(new Date().getTime() + 31557600000),
74 | sameSite: 'strict',
75 | httpOnly: true
76 | })
77 | .cookie('authSession', true, { expires: new Date(new Date().getTime() + 30 * 1000), sameSite: 'strict' })
78 | .cookie('refreshTokenID', true, {
79 | expires: new Date(new Date().getTime() + 31557600000),
80 | sameSite: 'strict'
81 | })
82 | .send({ msg: 'Device verified' });
83 | } else {
84 | console.log('not authenticated');
85 | return res.status(400).send({ verification: false, msg: 'Incorrect OTP' });
86 | }
87 | });
88 |
89 | app.post('/home', authenticateUser, (req, res) => {
90 | console.log('home private route');
91 | res.status(202).send('Private Protected Route - Home');
92 | });
93 |
94 | async function authenticateUser(req, res, next) {
95 | const accessToken = req.cookies.accessToken;
96 |
97 | jwt.verify(accessToken, JWT_AUTH_TOKEN, async (err, phone) => {
98 | if (phone) {
99 | req.phone = phone;
100 | next();
101 | } else if (err.message === 'TokenExpiredError') {
102 | return res.status(403).send({
103 | success: false,
104 | msg: 'Access token expired'
105 | });
106 | } else {
107 | console.log(err);
108 | return res.status(403).send({ err, msg: 'User not authenticated' });
109 | }
110 | });
111 | }
112 |
113 | app.post('/refresh', (req, res) => {
114 | const refreshToken = req.cookies.refreshToken;
115 | if (!refreshToken) return res.status(403).send({ message: 'Refresh token not found, login again' });
116 | if (!refreshTokens.includes(refreshToken))
117 | return res.status(403).send({ message: 'Refresh token blocked, login again' });
118 |
119 | jwt.verify(refreshToken, JWT_REFRESH_TOKEN, (err, phone) => {
120 | if (!err) {
121 | const accessToken = jwt.sign({ data: phone }, JWT_AUTH_TOKEN, {
122 | expiresIn: '30s'
123 | });
124 | return res
125 | .status(200)
126 | .cookie('accessToken', accessToken, {
127 | expires: new Date(new Date().getTime() + 30 * 1000),
128 | sameSite: 'strict',
129 | httpOnly: true
130 | })
131 | .cookie('authSession', true, {
132 | expires: new Date(new Date().getTime() + 30 * 1000),
133 | sameSite: 'strict'
134 | })
135 | .send({ previousSessionExpired: true, success: true });
136 | } else {
137 | return res.status(403).send({
138 | success: false,
139 | msg: 'Invalid refresh token'
140 | });
141 | }
142 | });
143 | });
144 |
145 | app.get('/logout', (req, res) => {
146 | res
147 | .clearCookie('refreshToken')
148 | .clearCookie('accessToken')
149 | .clearCookie('authSession')
150 | .clearCookie('refreshTokenID')
151 | .send('logout');
152 | });
153 | app.listen(process.env.PORT || 8888);
154 |
--------------------------------------------------------------------------------