Skip to content

Commit

Permalink
Merge pull request #412 from engaging-computing/dev
Browse files Browse the repository at this point in the history
v2.0.1 | Token Timeout Fix
  • Loading branch information
jasondkiesling authored Jul 20, 2020
2 parents dd76b99 + 75e654d commit 03241cc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/actions/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export function logout() {
return { type: types.LOGOUT };
}

export function refreshToken(token) {
return { type: types.REFRESH_TOKEN, token };
}

export default {
login,
logout
logout,
refreshToken
};
24 changes: 21 additions & 3 deletions src/components/structural/header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class Header extends Component {
updateCollection: false,
fetchCollection: false,
socket: sockets(),
savedSettings: []
savedSettings: [],
googleUser: undefined
};

this.state.socket.on("update", () => {
Expand Down Expand Up @@ -200,10 +201,27 @@ class Header extends Component {
//googleAuth.getAuthResponse().id_token;
googleAuth.profileObj["uid"] = googleAuth.getAuthResponse().id_token;
this.props.logging.login(googleAuth.profileObj);
this.setState({ logMenuOpen: false });
this.setState({ logMenuOpen: false, googleUser: googleAuth });

this.props.projectActions.asyncUserProj(this.props.user.uid);
this.props.collectionActions.asyncCollections(this.props.user.uid);
this.setRefreshTime(googleAuth.tokenObj.expires_at);
}

setRefreshTime = (time) => {
const oneMinute = 60*1000;
let expiryTime = Math.max(
oneMinute*5, //Default of 5 minutes
time - Date.now() - oneMinute*5 // give 5 mins of breathing room
);
setTimeout(this.refreshToken, expiryTime);
}

refreshToken = () => {
this.state.googleUser.reloadAuthResponse().then((authResponse) => {
this.props.logging.refreshToken(authResponse.id_token);
this.setRefreshTime(authResponse.expires_at);
});
}

/**
Expand Down Expand Up @@ -399,7 +417,7 @@ class Header extends Component {
if(!this.state.viewOnly) {
this.props.actions.refresh(text, this.props.user ? this.props.user.uid : "anon");
}
this.setState({spinnerOpen: false});
this.setState({spinnerOpen: false, saveOpen: false});
this.state.socket.emit("save");
return true;
});
Expand Down
1 change: 1 addition & 0 deletions src/constants/ActionTypes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const LOGIN = "LOGIN";
export const LOGOUT = "LOGOUT";
export const REFRESH_TOKEN = "REFRESH_TOKEN";

export const SYNC_COURSES = "SYNC_COURSES";
export const LOAD_COURSE = "LOAD_COURSE";
Expand Down
7 changes: 7 additions & 0 deletions src/reducers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export default function user(state = initial_state, action) {
return {
user: null
};
case types.REFRESH_TOKEN:
return {
user: {
...state,
uid: action.token
}
};
default:
return state;
}
Expand Down

0 comments on commit 03241cc

Please sign in to comment.