Skip to content

Commit

Permalink
added window for localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel2231 committed May 22, 2024
1 parent 83b1c95 commit a1c4929
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion axios/axiosInterceptorInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const axiosInterceptorInstance = axios.create({
axiosInterceptorInstance.interceptors.request.use(
(config) => {
// Modify the request config here (add headers, authentication tokens)
const accessToken = localStorage.getItem('key');
const accessToken = window.localStorage.getItem('key');

// If token is present, add it to request's Authorization Header
if (accessToken) {
Expand Down
7 changes: 5 additions & 2 deletions src/app/(main)/account/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ const Account = () => {
.get(`/club/my`)
.then((res) => {
setClubData(res.data);
localStorage.setItem('presidentContact', res.data.presidentContact);
localStorage.setItem('presidentName', res.data.presidentName);
window.localStorage.setItem(
'presidentContact',
res.data.presidentContact
);
window.localStorage.setItem('presidentName', res.data.presidentName);
})
.catch((err) => {
console.log(err);
Expand Down
4 changes: 2 additions & 2 deletions src/app/(main)/clubs/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const Clubs = () => {
setIsRefetching(false);
} else {
try {
switch (localStorage.getItem('role')) {
switch (window.localStorage.getItem('role')) {
case 'ROLE_MASTER':
break;
case 'ROLE_ADMIN_SUWON_CENTRAL':
Expand Down Expand Up @@ -233,7 +233,7 @@ const Clubs = () => {
} else {
return (
<TableContainer>
{localStorage.getItem('role') === 'ROLE_MASTER' ? (
{window.localStorage.getItem('role') === 'ROLE_MASTER' ? (
<div>
<ToggleButtonGroup
color="primary"
Expand Down
6 changes: 2 additions & 4 deletions src/app/(main)/settings/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const Settings = () => {
const [passwordMatchError, setPasswordMatchError] = useState(false);

const [inputValue, setInputValue] = useState({
presidentName: localStorage.getItem('presidentName'),
presidentContact: localStorage.getItem('presidentContact'),
presidentName: window.localStorage.getItem('presidentName'),
presidentContact: window.localStorage.getItem('presidentContact'),
password1: '',
password2: '',
});
Expand Down Expand Up @@ -60,8 +60,6 @@ const Settings = () => {
});
};



return (
<>
<Box
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/side-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const SideNav = (props) => {
<Stack component="nav" spacing={0.5}>
{items.map((item) => {
const active = item.path ? pathname === item.path : false;
const role = localStorage.getItem('role');
const role = window.localStorage.getItem('role');

if (
role === 'ROLE_MASTER' ||
Expand Down
19 changes: 11 additions & 8 deletions src/hooks/use-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ export const useUserLoginApi = () => {
.post(`/user/login?username=${id}&password=${pw}`)
.then((response) => {
console.log(response.data);
localStorage.setItem('key', response.headers['authorization']);
localStorage.setItem('refresh', response.headers['refresh-token']);
localStorage.setItem('userid', response.data.id);
localStorage.setItem('username', response.data.username);
localStorage.setItem('role', response.data.role);
window.localStorage.setItem('key', response.headers['authorization']);
window.localStorage.setItem(
'refresh',
response.headers['refresh-token']
);
window.localStorage.setItem('userid', response.data.id);
window.localStorage.setItem('username', response.data.username);
window.localStorage.setItem('role', response.data.role);
setUser({
name: response.data.username,
role: response.data.role,
Expand Down Expand Up @@ -68,7 +71,7 @@ export const useUserLogoutApi = () => {
withCredentials: true,
})
.then((response) => {
localStorage.clear();
window.localStorage.clear();
setUser({
name: '',
role: '',
Expand Down Expand Up @@ -104,8 +107,8 @@ export const useUserEditApi = () => {
export const useClubInfoApi = () => {
const [clubInfo, setClubInfo] = useState(null);
useEffect(() => {
const userid = localStorage.getItem('userid');
const auth = localStorage.getItem('key');
const userid = window.localStorage.getItem('userid');
const auth = window.localStorage.getItem('key');
if (userid) {
const getClubInfo = async () => {
await axiosInterceptorInstance
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/mainLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ export const MainLayout = (props) => {
axiosInterceptorInstance
.get('/refresh', {
headers: {
'refresh-token': localStorage.getItem('refresh'),
'refresh-token': window.localStorage.getItem('refresh'),
},
})
.then((res) => {
localStorage.setItem('key', res.headers['authorization']);
window.localStorage.setItem('key', res.headers['authorization']);
setSessionTime(1800000);
setOpenDialog(false);
})
Expand Down

0 comments on commit a1c4929

Please sign in to comment.