feat: Implement the /user/me endpoint

This commit is contained in:
Alexander Polynomdivision
2018-09-29 14:23:09 +02:00
parent 2e93fc954d
commit 65070b1f5b
14 changed files with 410 additions and 139 deletions

View File

@@ -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`, {

View File

@@ -68,7 +68,6 @@ const initialState: IState = {
didLogin: false,
// Is the user authenticated?
// TODO: Set this to false
authenticated: false,
user: {