-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor: makes session management a bit more readable #304
Changes from all commits
6e0b53a
a722623
021e5a7
9e58824
acb41ca
54a6880
5131771
a9225ee
01c4478
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ import * as endpoints from 'components/utils/Endpoints'; | |
|
||
const flashTimeout = 4000; | ||
|
||
// By default we load the mock messages when not in production. This is handy | ||
// By default, we load the mock messages when not in production. This is handy | ||
// because it removes the need to have a backend server. | ||
if (process.env.NODE_ENV !== 'production' && process.env.REACT_APP_NOMOCK !== 'on') { | ||
const { dvotingserver } = require('./mocks/dvotingserver'); | ||
|
@@ -22,8 +22,8 @@ if (process.env.NODE_ENV !== 'production' && process.env.REACT_APP_NOMOCK !== 'o | |
const arr = new Map<String, Array<String>>(); | ||
const defaultAuth = { | ||
isLogged: false, | ||
firstname: '', | ||
lastname: '', | ||
firstName: '', | ||
lastName: '', | ||
authorization: arr, | ||
isAllowed: (subject: string, action: string) => false, | ||
}; | ||
|
@@ -36,8 +36,8 @@ export const AuthContext = createContext<AuthState>(defaultAuth); | |
|
||
export interface AuthState { | ||
isLogged: boolean; | ||
firstname: string; | ||
lastname: string; | ||
firstName: string; | ||
lastName: string; | ||
authorization: Map<String, Array<String>>; | ||
isAllowed: (subject: string, action: string) => boolean; | ||
} | ||
|
@@ -224,39 +224,47 @@ const AppContainer = () => { | |
}; | ||
|
||
async function fetchData() { | ||
try { | ||
const res = await fetch(ENDPOINT_PERSONAL_INFO, req); | ||
|
||
if (res.status !== 200) { | ||
const txt = await res.text(); | ||
throw new Error(`unexpected status: ${res.status} - ${txt}`); | ||
const response = await fetch(ENDPOINT_PERSONAL_INFO, req); | ||
let result; | ||
switch (response.status) { | ||
case 200: { | ||
result = await response.json(); | ||
break; | ||
} | ||
case 401: { | ||
result = { | ||
isLoggedIn: false, | ||
firstName: '', | ||
lastName: '', | ||
authorization: {}, | ||
}; | ||
break; | ||
} | ||
default: { | ||
const txt = await response.text(); | ||
throw new Error(`Unexpected status: ${response.status} - ${txt}`); | ||
} | ||
|
||
const result = await res.json(); | ||
setAuth({ | ||
isLogged: result.islogged, | ||
firstname: result.firstname, | ||
lastname: result.lastname, | ||
authorization: result.islogged ? new Map(Object.entries(result.authorization)) : arr, | ||
isAllowed: function (subject: string, action: string) { | ||
return ( | ||
this.authorization.has(subject) && | ||
this.authorization.get(subject).indexOf(action) !== -1 | ||
); | ||
}, | ||
}); | ||
|
||
// wait for the default proxy to be set | ||
await setDefaultProxy(); | ||
|
||
setContent(<App />); | ||
} catch (e: any) { | ||
setContent(<Failed>{e.toString()}</Failed>); | ||
console.log('error:', e); | ||
} | ||
setAuth({ | ||
isLogged: result.isLoggedIn, | ||
firstName: result.firstName, | ||
lastName: result.lastName, | ||
authorization: result.isLoggedIn ? new Map(Object.entries(result.authorization)) : arr, | ||
isAllowed: function (subject: string, action: string) { | ||
return ( | ||
this.authorization.has(subject) && | ||
this.authorization.get(subject).indexOf(action) !== -1 | ||
); | ||
}, | ||
}); | ||
// wait for the default proxy to be set | ||
await setDefaultProxy(); | ||
setContent(<App />); | ||
} | ||
|
||
fetchData(); | ||
fetchData().catch((e) => { | ||
setContent(<Failed>{e.toString()}</Failed>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, be careful. Here you're changing the content of the page, I assume. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that is correct, but it already existed like this, so I wouldn't want to change it in this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But still I'm gonna take the advice for when I can apply am improvement confidently. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, I'm just reviewing some of this code for the first time - so I'm pointing out issues as I see them. That doesn't mean I expect all the fixes in the same PR :) |
||
console.log('error:', e); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the response did not return a 200, doesn't this always throw ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it's neither 200 nor 401
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what I meant is: isn't
response.text()
going to throw before yourthrow new Error()
, when there's no HTTP body ? (i.e. no error information other than the HTTP status sent from the backend)I'm not sure about how the
.text()
works in the Fetch API, but according to the documentation and the standard it seems it reads the body. Does it succeed on an empty body ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pierluca It succeeds. I tested it.
My test: I restarted the backend, and refreshed the UI causing the backend to return 504. the frontend then displayed the intended message.