Skip to content

Commit

Permalink
Merge pull request #328 from ertush/hotfix
Browse files Browse the repository at this point in the history
Hotfix
  • Loading branch information
ertush authored Feb 27, 2024
2 parents ebcca3f + d6874ed commit 7174938
Show file tree
Hide file tree
Showing 17 changed files with 1,367 additions and 7,447 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
yarn.lock

# local env files
.env.local.dev
Expand Down
417 changes: 222 additions & 195 deletions components/HeaderLayout.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion components/MainLayout.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 44 additions & 44 deletions controllers/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ const cookieCutter = require('cookie-cutter')

const getToken = (req, res, refresh_token, creds) => {
const cookies = new Cookies(req, res)
console.log('running getToken')
// console.log('------------getToken: ', creds)
// console.log('running getToken')
// // console.log('------------getToken: ', creds)
const isServer = !!req
const isBrowser = !req
const bod = {} //new FormData();
let ct
if (isBrowser) {
console.log('running getToken in the BROWSER')
// console.log('running getToken in the BROWSER')
ct = cookieCutter.get('access_token')
console.log("B getToken ct == ", ct)
// console.log("B getToken ct == ", ct)
if (typeof ct == "string") {
ct = JSON.parse(ct)
}
if (ct && ct.expires && ct.expires > Date.now()) {
return ct
} else {
console.log('Refreshing the page...')
// console.log('Refreshing the page...')

}
} else if (isServer) {
console.log('running getToken in the SERVER')
// console.log('running getToken in the SERVER')
ct = cookies?.get('access_token')
console.log("S getToken ct == ", ct)
// console.log("S getToken ct == ", ct)
if (typeof ct == "string") {
ct = JSON.parse(ct)
}
Expand All @@ -36,19 +36,19 @@ const getToken = (req, res, refresh_token, creds) => {
}
}
if (refresh_token && refresh_token.length > 0 && refresh_token != null) {
console.log('Refreshing token...')
// console.log('Refreshing token...')
bod.grant_type = "refresh_token"
bod.refresh_token = refresh_token
} else {
console.log('Getting new token...')
// console.log('Getting new token...')
bod.grant_type = "password"
bod.username = creds?.username || process.env.USERNAME
bod.password = creds?.password || process.env.PASSWORD
}
bod.client_id = process.env.CLIENT_ID
bod.client_secret = process.env.CLIENT_SECRET

// console.log({ token_url: process.env.TOKEN_URL })
// // console.log({ token_url: process.env.TOKEN_URL })
return fetch(process.env.TOKEN_URL, {
'method': 'POST',
'headers': {
Expand All @@ -64,7 +64,7 @@ const getToken = (req, res, refresh_token, creds) => {
let tk = response;
////
if (tk && tk.access_token && tk.expires_in && tk.access_token.length > 0) {
console.log('Token refreshed.')
// console.log('Token refreshed.')
let expiry = new Date(new Date().getTime() + (parseInt(tk.expires_in) * 1000))
let tkn = {
'expires': expiry,
Expand All @@ -73,15 +73,15 @@ const getToken = (req, res, refresh_token, creds) => {
}
ct = tkn
if (isBrowser) {
console.log('Setting new BROWSER token')
// console.log('Setting new BROWSER token')
cookieCutter.set('access_token', JSON.stringify(tkn), { expires: expiry, httpOnly: false })
} else if (isServer) {
console.log('Setting new SERVER token')
// console.log('Setting new SERVER token')
cookies.set('access_token', JSON.stringify(tkn), { expires: expiry, maxAge: parseInt(tk.expires_in) * 1000, overwrite: true, httpOnly: false })
}
return tkn;
} else {
console.log('Error refreshing token: ', tk)
// console.log('Error refreshing token: ', tk)
if (isBrowser) {
cookieCutter.set('access_token', '', "{}", { expires: new Date(0), httpOnly: false })
} else if (isServer) {
Expand All @@ -91,50 +91,50 @@ const getToken = (req, res, refresh_token, creds) => {
return { error: true, ...tk };
}
}).then(json => {
// console.log('New token: ', json)
// // console.log('New token: ', json)
return json;
}).catch(err => {
console.log('Error in getToken: ', err)
// console.log('Error in getToken: ', err)
return { error: true, ...err };
})
}

const checkToken = async (req, res, isProtected, creds) => {
const cookies = new Cookies(req, res)
let crds = creds || null
// console.log('------------checkToken: ', creds)
// // console.log('------------checkToken: ', creds)
const isServer = !!req
const isBrowser = !req
let ct
if (isBrowser && typeof window != "undefined") {
console.log('running checkToken in the BROWSER')
// console.log('running checkToken in the BROWSER')
ct = cookieCutter.get('access_token')

if (ct && ct != null && ct != undefined && new Date(ct.expires) > Date.now()) {
console.log('B Token is valid')
// console.log('B Token is valid')
return ct
} else {
console.log('Refreshing entire page...')
// console.log('Refreshing entire page...')

}
} else if (isServer) {
console.log('running checkToken in the SERVER')
// console.log('running checkToken in the SERVER')
ct = cookies?.get('access_token')
if (typeof ct == "string") {
if (typeof ct == "string" && ct.length > 0) {
ct = JSON.parse(ct)
}
// console.log("S checkToken ct == ", ct)
// // console.log("S checkToken ct == ", ct)
if (ct && ct != null && ct != undefined && new Date(ct.expires) > Date.now()) {
console.log('S Token is valid')//: ', ct)
// console.log('S Token is valid')//: ', ct)
return ct
}
}
//check of cookie has expired
if (!ct || ct == null || ct == undefined || (ct && JSON.parse(ct).expires > Date.now())) {
console.log('Token expired. Refreshing...')
// console.log('Token expired. Refreshing...')
if (req && req.asPath != '/api/login' && req.asPath != '/auth/login') {//check if protected page too
// res.writeHead(301, { Location: '/auth/login?was=' + req.asPath + '&h=1' })
console.log('page not protected')
// console.log('page not protected')
res.writeHead(301, { Location: '/auth/login?was=' + encodeURIComponent(req.url) + '&h=1' })
res.end()
return { error: true, message: 'Token expired. Refreshing...' }
Expand All @@ -147,35 +147,35 @@ const checkToken = async (req, res, isProtected, creds) => {
}
return getToken(req, res, refresh_token, crds).then(tk => {
if (!tk.error) {
console.log('Token refreshed.')
// console.log('Token refreshed.')
return tkn;
} else {
console.log('Error refreshing token: ', tk)
// console.log('Error refreshing token: ', tk)
// res.redirect('/auth/login?was='+encodeURIComponent(req.url))
return { error: true, ...tk };
}
})
} else {
ct = JSON.parse(ct)
console.log('Token is valid.')
// console.log('Token is valid.')
return ct
}
}

const logUserIn = (req, res, creds, was) => {
// console.log({creds})
// console.log('------------logUserIn: ', creds)
// // console.log({creds})
// // console.log('------------logUserIn: ', creds)
return getToken(req, res, null, creds).then(tk => {
if (tk.error) {
console.log('Error in LogIn: ', tk)
// console.log('Error in LogIn: ', tk)
return { error: true, ...tk };
} else {
console.log('LogIn ok: ', tk)
// console.log('LogIn ok: ', tk)
return tk;
}
})
.catch(err => {
console.log('Error in LogIn: ', err)
// console.log('Error in LogIn: ', err)
return { error: true, ...err };
})
}
Expand All @@ -187,18 +187,18 @@ const getUserDetails = async (token, url) => {
// savedSession = JSON.parse(window.sessionStorage.getItem('user'))
// }
// if (savedSession && savedSession?.id && savedSession?.id.length > 0) {
// console.log('Saved session: ', savedSession)
// // console.log('Saved session: ', savedSession)
// return savedSession
// }
let savedSession = window.localStorage.getItem('user')
if (savedSession && savedSession.length > 0) {
savedSession = JSON.parse(window.localStorage.getItem('user'))
}
if (savedSession && savedSession?.id && savedSession?.id.length > 0) {
console.log('Saved session: ', savedSession)
// console.log('Saved session: ', savedSession)
return savedSession
}
// console.log('W getUserDetails URL: ',url)
// // console.log('W getUserDetails URL: ',url)
}

return fetch(url, {
Expand All @@ -211,21 +211,21 @@ const getUserDetails = async (token, url) => {
})
.then(j => j.json())
.then(response => {
// console.log('=================== getUserDetails returned: ', response)
// // console.log('=================== getUserDetails returned: ', response)
if (response.detail || response.error) {
console.log('Error in getUserDetails: ', response)
// console.log('Error in getUserDetails: ', response)
return {
error: true, message: response.detail || response.error
}
}
if (typeof window !== "undefined") {
// console.log('getUserDetails returning ', response)
// // console.log('getUserDetails returning ', response)
// window.sessionStorage.setItem('user', JSON.stringify(response))
window.localStorage.setItem('user', JSON.stringify(response))
}
return response
}).catch(err => {
console.log('Error in getUserDetails: ', err)
// console.log('Error in getUserDetails: ', err)
return {
error: true, message: err.message || err
}
Expand All @@ -244,16 +244,16 @@ const getUserContacts = async (token, url) => {
})
.then(j => j.json())
.then(response => {
// console.log('=================== getUserContacts returned: ', response)
// // console.log('=================== getUserContacts returned: ', response)
if (response.detail || response.error) {
console.log('Error in getUserContacts: ', response)
// console.log('Error in getUserContacts: ', response)
return {
error: true, message: response.detail || response.error
}
}
return response
}).catch(err => {
console.log('Error in getUserContacts: ', err)
// console.log('Error in getUserContacts: ', err)
return {
error: true, message: err.message || err
}
Expand Down
Loading

0 comments on commit 7174938

Please sign in to comment.