aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/TokenManager.jsx
diff options
context:
space:
mode:
authorLibravatar Mo Tarbin <mhed.t91@gmail.com>2024-06-30 18:55:39 -0400
committerLibravatar Mo Tarbin <mhed.t91@gmail.com>2024-06-30 18:55:39 -0400
commit2657469964e24ffbeb905024532120395f6e797c (patch)
tree2fe9db8a4ecfa92d854ca94f7586d81163c8bd25 /src/utils/TokenManager.jsx
downloaddonetick-frontend-2657469964e24ffbeb905024532120395f6e797c.tar.gz
donetick-frontend-2657469964e24ffbeb905024532120395f6e797c.tar.bz2
donetick-frontend-2657469964e24ffbeb905024532120395f6e797c.zip
move to Donetick Org, First commit frontend
Diffstat (limited to 'src/utils/TokenManager.jsx')
-rw-r--r--src/utils/TokenManager.jsx65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/utils/TokenManager.jsx b/src/utils/TokenManager.jsx
new file mode 100644
index 0000000..35472e4
--- /dev/null
+++ b/src/utils/TokenManager.jsx
@@ -0,0 +1,65 @@
+import Cookies from 'js-cookie'
+import { API_URL } from '../Config'
+export function Fetch(url, options) {
+ if (!isTokenValid()) {
+ console.log('FETCH: Token is not valid')
+ console.log(localStorage.getItem('ca_token'))
+ // store current location in cookie
+ Cookies.set('ca_redirect', window.location.pathname)
+ // Assuming you have a function isTokenValid() that checks token validity
+ window.location.href = '/login' // Redirect to login page
+ // return Promise.reject("Token is not valid");
+ }
+ if (!options) {
+ options = {}
+ }
+ options.headers = { ...options.headers, ...HEADERS() }
+
+ return fetch(url, options)
+}
+
+export const HEADERS = () => {
+ return {
+ 'Content-Type': 'application/json',
+ Authorization: 'Bearer ' + localStorage.getItem('ca_token'),
+ }
+}
+
+export const isTokenValid = () => {
+ const expiration = localStorage.getItem('ca_expiration')
+ const token = localStorage.getItem('ca_token')
+
+ if (localStorage.getItem('ca_token')) {
+ const now = new Date()
+ const expire = new Date(expiration)
+ if (now < expire) {
+ if (now.getTime() + 24 * 60 * 60 * 1000 > expire.getTime()) {
+ refreshAccessToken()
+ }
+
+ return true
+ } else {
+ localStorage.removeItem('ca_token')
+ localStorage.removeItem('ca_expiration')
+ }
+ return false
+ }
+}
+
+export const refreshAccessToken = () => {
+ fetch(API_URL + '/auth/refresh', {
+ method: 'GET',
+ headers: HEADERS(),
+ }).then(res => {
+ if (res.status === 200) {
+ res.json().then(data => {
+ localStorage.setItem('ca_token', data.token)
+ localStorage.setItem('ca_expiration', data.expire)
+ })
+ } else {
+ return res.json().then(error => {
+ console.log(error)
+ })
+ }
+ })
+}