{"size": 787, "ext": "js", "max_stars_repo_path": "src/auth/AuthService.js", "max_stars_repo_name": "webmasterdevlin/OfficeAppReactNative", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/auth/AuthService.js", "max_issues_repo_name": "webmasterdevlin/OfficeAppReactNative", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/auth/AuthService.js", "max_forks_repo_name": "webmasterdevlin/OfficeAppReactNative", "max_forks_repo_forks_event_min_datetime": "2019-02-16T13:30:30.000Z", "max_forks_repo_forks_event_max_datetime": "2019-02-16T13:30:30.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 20.7105263158, "max_line_length": 52, "alphanum_fraction": 0.7115628971}
import jwtDecode from "jwt-decode";
import { AsyncStorage } from "react-native";

export async function storeToken(val) {
  await AsyncStorage.setItem("token", val);
}

export async function logOut() {
  await AsyncStorage.removeItem("token");
}

export async function getJwt() {
  return await AsyncStorage.getItem("token");
}

export async function storedTokenIsValid() {
  const token = await AsyncStorage.getItem("token");
  if (!token) return false;

  const decoded = jwtDecode(token);
  const expiresAt = decoded.exp * 1000;
  const dateNow = Date.now();
  const isTokenExpired = dateNow > expiresAt;

  if (isTokenExpired) {
    return false;
  }
  if (!isTokenExpired) {
    return true;
  }
}

export default {
  storeToken,
  logOut,
  routeCanActivate: storedTokenIsValid
};
