@spidy092/auth-client 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/core.js +76 -0
  2. package/package.json +1 -1
package/core.js CHANGED
@@ -40,6 +40,16 @@ export function login(clientKeyArg, redirectUriArg) {
40
40
  sessionStorage.setItem('originalApp', clientKey);
41
41
  sessionStorage.setItem('returnUrl', redirectUri);
42
42
 
43
+ try {
44
+ const hasValidSession = await checkExistingTokens();
45
+ if (hasValidSession) {
46
+ console.log('✅ Valid session found, skipping login redirect');
47
+ return getToken();
48
+ }
49
+ } catch (err) {
50
+ console.log('⚠️ No valid session, proceeding with login flow');
51
+ }
52
+
43
53
  // ✅ Smart Router Logic
44
54
  if (isRouterMode()) {
45
55
  // Router mode: Direct backend authentication
@@ -64,6 +74,72 @@ function routerLogin(clientKey, redirectUri) {
64
74
  window.location.href = backendLoginUrl;
65
75
  }
66
76
 
77
+
78
+ async function checkExistingTokens() {
79
+ const token = getToken();
80
+ const refreshTokenValue = getRefreshToken();
81
+
82
+ console.log('🔍 Checking existing tokens:', {
83
+ hasAccessToken: !!token,
84
+ hasRefreshToken: !!refreshTokenValue
85
+ });
86
+
87
+ // No tokens at all
88
+ if (!token && !refreshTokenValue) {
89
+ console.log('❌ No tokens found');
90
+ return false;
91
+ }
92
+
93
+ // Have valid access token
94
+ if (token && !isTokenExpiredLocal(token)) {
95
+ console.log('✅ Valid access token exists');
96
+ return true;
97
+ }
98
+
99
+ // Have refresh token, try to get new access token
100
+ if (refreshTokenValue) {
101
+ try {
102
+ console.log('🔄 Access token expired, attempting refresh...');
103
+ const newToken = await refreshToken();
104
+ console.log('✅ Token refreshed successfully');
105
+ return !!newToken;
106
+ } catch (err) {
107
+ console.warn('❌ Token refresh failed:', err);
108
+ return false;
109
+ }
110
+ }
111
+
112
+ return false;
113
+ }
114
+
115
+ // ✅ NEW HELPER: Check if token is expired
116
+ function isTokenExpiredLocal(token, bufferSeconds = 60) {
117
+ if (!token) return true;
118
+
119
+ try {
120
+ const parts = token.split('.');
121
+ if (parts.length !== 3) return true;
122
+
123
+ const payload = JSON.parse(atob(parts[1]));
124
+
125
+ if (!payload.exp) return true;
126
+
127
+ const now = Date.now() / 1000;
128
+ const isExpired = payload.exp < (now + bufferSeconds);
129
+
130
+ console.log('🕐 Token expiry check:', {
131
+ expiresAt: new Date(payload.exp * 1000).toLocaleString(),
132
+ now: new Date(now * 1000).toLocaleString(),
133
+ isExpired
134
+ });
135
+
136
+ return isExpired;
137
+ } catch (err) {
138
+ console.error('❌ Failed to decode token:', err);
139
+ return true;
140
+ }
141
+ }
142
+
67
143
  // ✅ Client mode: Centralized login
68
144
  function clientLogin(clientKey, redirectUri) {
69
145
  const { accountUiUrl } = getConfig();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spidy092/auth-client",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Scalable frontend auth SDK for centralized login using Keycloak + Auth Service.",
5
5
  "main": "index.js",
6
6
  "module": "index.js",