@orsetra/shared-auth 1.1.4 → 1.1.6

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.
@@ -44,8 +44,9 @@ export function ZitadelProvider({ children, config, renderSessionExpiredAction }
44
44
  ZitadelAuthService.onSessionExpired = () => setSessionExpired(true)
45
45
  setIsConfigured(true)
46
46
 
47
- const currentUser = await ZitadelAuthService.getUser()
48
- setUser(currentUser?.expired ? null : currentUser)
47
+ const isAuthenticated = await ZitadelAuthService.isAuthenticated()
48
+ const currentUser = isAuthenticated ? await ZitadelAuthService.getUser() : null
49
+ setUser(currentUser)
49
50
  } catch (error) {
50
51
  console.error("Failed to configure Zitadel auth:", error)
51
52
  } finally {
package/index.ts CHANGED
@@ -2,7 +2,8 @@
2
2
  export { ZitadelProvider, useZitadel } from './ZitadelProvider'
3
3
  export type { ZitadelProviderProps } from './ZitadelProvider'
4
4
  export { ProtectedRoute } from './ProtectedRoute'
5
- export { ZitadelAuthService } from './services/zitadel.auth.service'
5
+ export { ZitadelAuthService, decodeJwt, isJwtExpired } from './services/zitadel.auth.service'
6
+ export type { JwtPayload } from './services/zitadel.auth.service'
6
7
  export { createAuthConfig } from './config/zitadel.config'
7
8
  export type { ZitadelConfig } from './config/zitadel.config'
8
9
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orsetra/shared-auth",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Shared authentication utilities for Orsetra platform using Zitadel",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",
@@ -5,6 +5,28 @@ import { ZitadelConfig, createAuthConfig } from '../config/zitadel.config';
5
5
 
6
6
  export type { ZitadelConfig };
7
7
 
8
+ export interface JwtPayload {
9
+ exp?: number
10
+ iat?: number
11
+ sub?: string
12
+ [key: string]: unknown
13
+ }
14
+
15
+ export function decodeJwt(token: string): JwtPayload {
16
+ const payload = token.split('.')[1]
17
+ if (!payload) throw new Error('Invalid JWT: missing payload segment')
18
+ return JSON.parse(atob(payload)) as JwtPayload
19
+ }
20
+
21
+ export function isJwtExpired(token: string): boolean {
22
+ try {
23
+ const { exp } = decodeJwt(token)
24
+ return typeof exp !== 'number' || exp <= Math.floor(Date.now() / 1000)
25
+ } catch {
26
+ return true
27
+ }
28
+ }
29
+
8
30
  export interface ZitadelAuth {
9
31
  authorize(): Promise<void>;
10
32
  signout(): Promise<void>;
@@ -148,7 +170,8 @@ export class ZitadelAuthService {
148
170
 
149
171
  static async isAuthenticated(): Promise<boolean> {
150
172
  const user = await this.getUser();
151
- return user !== null && !user.expired;
173
+ if (!user?.access_token) return false;
174
+ return !isJwtExpired(user.access_token);
152
175
  }
153
176
 
154
177
  static async getTokenRemainingTime(): Promise<number> {