feat: Implement the /user/me endpoint
This commit is contained in:
@@ -36,15 +36,37 @@ interface IProps {
|
||||
// TODO: Replace the sessionStorage with localStorage?
|
||||
export default class Application extends React.Component<IProps> {
|
||||
componentDidMount() {
|
||||
// TODO: Ask the server if our session is still valid
|
||||
// TODO: When asking the server if our session is still valid, a spinner
|
||||
// should be shown
|
||||
if (getSessionToken(window) !== null) {
|
||||
// TODO: We still need to fetch the user data
|
||||
this.props.setAuthenticated(true);
|
||||
const token = getSessionToken(window);
|
||||
if (token !== null && !this.props.authenticated) {
|
||||
this.checkAuthStatus(token).then(user => {
|
||||
this.props.setUser(user);
|
||||
this.props.setAuthenticated(true);
|
||||
}).catch(err => {
|
||||
this.props.setAuthenticated(false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
checkAuthStatus = (token: string): Promise<IUser> => {
|
||||
return new Promise((res, rej) => {
|
||||
fetch(`${BACKEND_URL}/api/user/me`, {
|
||||
headers: new Headers({
|
||||
"Content-Type": "application/json",
|
||||
"Token": token,
|
||||
}),
|
||||
}).then(resp => resp.json(), err => rej(err))
|
||||
.then(data => {
|
||||
if (data.error === "0") {
|
||||
res(data.data);
|
||||
} else {
|
||||
rej(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getLevels = (): Promise<ILevel[]> => {
|
||||
return new Promise((res, rej) => {
|
||||
fetch(`${BACKEND_URL}/api/levels`, {
|
||||
|
||||
@@ -68,7 +68,6 @@ const initialState: IState = {
|
||||
didLogin: false,
|
||||
|
||||
// Is the user authenticated?
|
||||
// TODO: Set this to false
|
||||
authenticated: false,
|
||||
|
||||
user: {
|
||||
|
||||
Reference in New Issue
Block a user