Skip to content

Commit

Permalink
Remember signin
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-xing committed Mar 24, 2022
1 parent 040e575 commit 66a6ed2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/contribute/contribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,21 @@ function Contribution() {
React.useEffect(() => {
switch (state.s) {
case 'signin': {
const currentToken = window.sessionStorage.getItem('oauthToken');
if (currentToken !== null) {
const tokenInfo = JSON.parse(currentToken);
if (tokenInfo.time > new Date().getTime()) {
setState({ s: 'load', token: tokenInfo.token });
}
}

/** @param {Record<string, unknown>} response */
const handleCredentialResponse = (response) => {
const token = /** @type {string} */(response.credential);
window.sessionStorage.setItem('oauthToken', JSON.stringify({
time: new Date().getTime() + 30 * 60 * 1000, // 30 min * 60 sec / min * 1000 ms / sec
token,
}));
setState({ s: 'load', token });
};
// @ts-ignore
Expand Down
13 changes: 13 additions & 0 deletions docs/contribute/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,23 @@ function Contribution() {
// eslint-disable-next-line no-undef
React.useEffect(() => {
if (!state) {
const currentToken = window.sessionStorage.getItem('oauthToken');
if (currentToken !== null) {
const tokenInfo = JSON.parse(currentToken);
if (tokenInfo.time > new Date().getTime()) {
setToken(tokenInfo.token);
setState(true);
}
}

/** @param {Record<string, unknown>} response */
const handleCredentialResponse = (response) => {
// @ts-ignore
setToken(response.credential);
window.sessionStorage.setItem('oauthToken', JSON.stringify({
time: new Date().getTime() + 30 * 60 * 1000, // 30 min * 60 sec / min * 1000 ms / sec
token: response.credential,
}));
setState(true);
};
// @ts-ignore
Expand Down

0 comments on commit 66a6ed2

Please sign in to comment.