import { RootState } from "app/store";
import { decodeToken } from "common/decodeToken";
import { DateTime } from "luxon";
/**
 * ログイン済みかどうかを判定する
 * @param state RootState
 */
export const isAuthenticatedSelector = (state: RootState): boolean =>
  state.auth.accessToken !== null;

// トークンが有効期限内かどうかを判定する
export const isTokenExpired = (state: RootState): boolean => {
  if (state.auth.accessToken) {
    const token = decodeToken(state.auth.accessToken);
    if (token) {
      const { exp } = token;
      const now = DateTime.local().toSeconds();
      if (now <= exp) {
        return false;
      }
    }
  }
  return true;
};
