|
| 1 | +import React from 'react'; |
| 2 | +import { useHistory } from "react-router-dom"; |
| 3 | +import useToken from '../components/Login/useToken'; |
| 4 | +var jwt = require('jsonwebtoken'); |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +export default function Refresh() { |
| 9 | + |
| 10 | + const { access_token, setToken } = useToken(); |
| 11 | + |
| 12 | + const [processStatus, setProcessStatus] = React.useState('loading'); |
| 13 | + const [error, setError] = React.useState(''); |
| 14 | + const [data, setData] = React.useState(''); |
| 15 | + |
| 16 | + let history = useHistory() |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + // get the decoded payload and header |
| 23 | + var decoded = jwt.decode(access_token, { complete: true }); |
| 24 | + const userName = decoded?.payload.sub |
| 25 | + const userRole = decoded?.payload.role |
| 26 | + const expTime = decoded?.payload.exp |
| 27 | + // console.log('User: ' + userName + ' / Role:' + userRole + '->' + processStatus + ' @ ' + DateTime.local().toFormat('HH:mm:ss.SSS')) |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + React.useEffect(() => { |
| 32 | + |
| 33 | + function doRefresh() { |
| 34 | + // console.log('authCheck startfetch @ ' + DateTime.local().toFormat('HH:mm:ss.SSS')) |
| 35 | + fetch('http://localhost:5000/api/user/refresh', |
| 36 | + { |
| 37 | + method: 'GET', |
| 38 | + headers: { |
| 39 | + 'Content-Type': 'application/json', |
| 40 | + 'Authorization': 'Bearer ' + access_token |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + .then((response) => { |
| 45 | + // console.log('authCheck handle response @ ' + DateTime.local().toFormat('HH:mm:ss.SSS')) |
| 46 | + if (!response.ok) { |
| 47 | + //throw (String(response.status + ':' + response.statusText)) |
| 48 | + throw (response) |
| 49 | + } |
| 50 | + return response.json() |
| 51 | + } ) |
| 52 | + .then((data) => { |
| 53 | + // console.log('authCheck data @ ' + DateTime.local().toFormat('HH:mm:ss.SSS')) |
| 54 | + setProcessStatus('complete'); |
| 55 | + setData(data); |
| 56 | + }) |
| 57 | + .catch((e) => { |
| 58 | + let errCode = e.status |
| 59 | + let errText = e.statusText |
| 60 | + |
| 61 | + setToken(null) // Clear the token to force login again |
| 62 | + let errStr = String(e) |
| 63 | + setProcessStatus('error'); |
| 64 | + setError(errStr); |
| 65 | + console.log(errCode + ':' + errText) |
| 66 | + history.push('/') |
| 67 | + return e |
| 68 | + }); |
| 69 | + |
| 70 | + } // |
| 71 | + |
| 72 | + |
| 73 | + const handleRefresh = async e => { |
| 74 | + const dr = await doRefresh(); |
| 75 | + const at = dr.access_token; |
| 76 | + setToken(at); |
| 77 | + }; |
| 78 | + |
| 79 | + |
| 80 | + handleRefresh(); |
| 81 | + |
| 82 | + }, |
| 83 | + // eslint-disable-next-line |
| 84 | + [ processStatus, access_token, history ]); |
| 85 | + |
| 86 | + // if (processStatus === 'loading') { |
| 87 | + // console.log('Check: if pc=l loading...') |
| 88 | + // return <p>loading..</p>; |
| 89 | + // } |
| 90 | + |
| 91 | + if (processStatus === 'error') { |
| 92 | + console.log('error') |
| 93 | + return <p>ERROR: <i>{error}</i></p>; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + // console.log("About to return") |
| 99 | + return ( |
| 100 | + <div> |
| 101 | + |
| 102 | + <h2>Refresh</h2> |
| 103 | + <ul> |
| 104 | + |
| 105 | + <li>JWT expires: {-(Date.now()/1000 - expTime).toFixed(1)} secs</li> |
| 106 | + <li>{data}</li> |
| 107 | + |
| 108 | + </ul> |
| 109 | + {userRole === 'admin' && |
| 110 | + <h2>Welcome, admin!</h2> |
| 111 | + } |
| 112 | + </div> |
| 113 | + ); |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +}; |
| 118 | + |
0 commit comments