Skip to content

Commit

Permalink
Add token expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
campos20 committed Oct 1, 2020
1 parent b7349e0 commit 9deb482
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
42 changes: 36 additions & 6 deletions tnoodle-ui/src/main/api/wca.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import { getHashParameter, getQueryParameter } from "../util/query.param.util";

const TNOODLE_ACCESS_TOKEN_KEY = "TNoodle.accessToken";
const TNOODLE_LAST_LOGIN_ENV = "TNoodle.lastLoginEnv";
const TNOODLE_EXPIRATION = "TNoodle.expiration";
const STAGING = "staging";
const PRODUCTION = "production";

const ACCESS_TOKEN = "access_token";
const EXPIRES_IN = "expires_in";

// See https://github.com/thewca/worldcubeassociation.org/wiki/OAuth-documentation-notes#staging-oauth-application
let getWcaOrigin = () => {
if (isUsingStaging()) {
Expand All @@ -29,13 +33,21 @@ let getTnoodleAppId = () => {
);
};

let wcaAccessToken = getHashParameter("access_token");
if (wcaAccessToken) {
let wcaAccessToken = getHashParameter(ACCESS_TOKEN);
let expiresIn = getHashParameter(EXPIRES_IN);
if (!!wcaAccessToken) {
window.location.hash = "";

let now = new Date();
let expiration = now.setSeconds(now.getSeconds() + Number(expiresIn));

localStorage[TNOODLE_ACCESS_TOKEN_KEY] = wcaAccessToken;
localStorage[TNOODLE_EXPIRATION] = expiration;

gotoPreLoginPath();
} else {
wcaAccessToken = localStorage[TNOODLE_ACCESS_TOKEN_KEY];
expiresIn = localStorage[TNOODLE_EXPIRATION];
}

export function isUsingStaging() {
Expand All @@ -60,16 +72,34 @@ export function logIn() {
}

export function isLogged() {
return (
localStorage[TNOODLE_ACCESS_TOKEN_KEY] != null &&
getCurrentEnv() === getLastLoginEnv()
);
if (localStorage[TNOODLE_ACCESS_TOKEN_KEY] == null) {
return false;
}

if (getCurrentEnv() !== getLastLoginEnv()) {
return false;
}

let expiration = localStorage[TNOODLE_EXPIRATION];

if (expiration == null) {
return false;
}

if (new Date() < new Date(Number(expiration))) {
return true;
}

logOut();
return false;
}

export function logOut() {
delete localStorage[TNOODLE_LAST_LOGIN_ENV];
delete localStorage[TNOODLE_ACCESS_TOKEN_KEY];
delete localStorage[TNOODLE_EXPIRATION];
wcaAccessToken = null;
expiresIn = null;
window.location.reload();
}

Expand Down
3 changes: 3 additions & 0 deletions tnoodle-ui/src/main/components/FmcTranslationsDetail.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.fmc-label {
text-transform: capitalize;
}
11 changes: 6 additions & 5 deletions tnoodle-ui/src/main/components/FmcTranslationsDetail.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Component } from "react";
import { connect } from "react-redux";
import { capitalize } from "../util/string.util";
import _ from "lodash";
import {
updateFileZipBlob,
Expand All @@ -9,6 +8,7 @@ import {
resetTranslations,
setSuggestedFmcTranslations,
} from "../redux/ActionCreators";
import "./FmcTranslationsDetail.css";

const TRANSLATIONS_PER_LINE = 3;

Expand All @@ -30,8 +30,8 @@ const FmcTranslationsDetail = connect(
mapDispatchToProps
)(
class extends Component {
constructor(props) {
super(props);
constructor() {
super();
this.state = { showTranslations: false };
}

Expand Down Expand Up @@ -123,13 +123,14 @@ const FmcTranslationsDetail = connect(
>
<th>
<label
className="fmc-label"
htmlFor={
checkboxId
}
>
{capitalize(
{
translation.display
)}
}
</label>
</th>
<th>
Expand Down
1 change: 0 additions & 1 deletion tnoodle-ui/src/main/util/string.util.js

This file was deleted.

0 comments on commit 9deb482

Please sign in to comment.