1. class Token {
  2. constructor(tokenString, expiry) {
  3. this.tokenString = tokenString; // The actual token string
  4. this.expiry = expiry; // Expiration timestamp (in seconds since epoch)
  5. }
  6. isValid() {
  7. const now = Math.floor(Date.now() / 1000); // Current time in seconds
  8. return now < this.expiry; // Check if token has expired
  9. }
  10. getTokenString() {
  11. return this.tokenString;
  12. }
  13. getExpiry() {
  14. return this.expiry;
  15. }
  16. }
  17. // Example usage:
  18. // Create a simple token
  19. const token = new Token("simpletoken123", Math.floor(Date.now() / 1000) + 60); // Expires in 60 seconds
  20. // Check if the token is valid
  21. console.log("Token is valid:", token.isValid());
  22. // Get the token string
  23. console.log("Token string:", token.getTokenString());
  24. //Get the expiry time
  25. console.log("Expiry time:", token.getExpiry());

Add your comment