Skip to content

Commit

Permalink
Merge pull request #436 from ertush/hotfix
Browse files Browse the repository at this point in the history
Fixed issue of some users not being able logging in
  • Loading branch information
ertush authored Jul 31, 2024
2 parents 82c9067 + f69595b commit 2951aab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 14 additions & 5 deletions components/ProtectedRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { useRouter } from 'next/router'
import { getUserDetails } from "../controllers/auth/auth"
// import cookies from 'next-cookies'
// import cookieCutter from 'cookie-cutter'
import { useEffect, useState } from 'react'
import { useEffect, useState, useContext} from 'react'
import {UserContext} from '../providers/user'

export default function withAuth(Component) {

Expand All @@ -17,24 +18,32 @@ export default function withAuth(Component) {

const [token, setToken] = useState(null)

const userCtx = useContext(UserContext)

useEffect(() => {
if(window && window.document.cookie) {
setToken(
JSON.parse(window.document.cookie.split('=')[1])?.token
JSON.parse(window.document.cookie.split('=')[1])?.token ?? props?.token
)
}
},[])
/

if(userCtx && userCtx?.id == 6) {
router.push('/auth/login')
return null

getUserDetails(token ?? props?.token, `${process.env.NEXT_PUBLIC_API_URL}/rest-auth/user/`)
} else {
getUserDetails(token, `${process.env.NEXT_PUBLIC_API_URL}/rest-auth/user/`)
.then(async user => {

if(user && user?.id == 6) {

return router.push('/auth/login')
router.push('/auth/login')
return null

}
})
}

return <Component {...props}/>
}
Expand Down
6 changes: 3 additions & 3 deletions controllers/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,10 @@ const logUserIn = (req, res, creds, was) => {
}

function getUserDetails (token, url) {
let savedSession = null
if (typeof window != "undefined") {

let savedSession = window.localStorage.getItem('user')
savedSession = window.localStorage.getItem('user')
if (savedSession && savedSession.length > 0) {
savedSession = JSON.parse(window.localStorage.getItem('user'))
}
Expand All @@ -198,7 +199,6 @@ function getUserDetails (token, url) {
}



return fetch(url, {
'method': 'GET',
'headers': {
Expand Down Expand Up @@ -229,8 +229,8 @@ function getUserDetails (token, url) {
error: true, message: err.message || err
}
})
}

}

const getUserContacts = async (token, url) => {
return fetch(url, {
Expand Down

0 comments on commit 2951aab

Please sign in to comment.