@orsetra/shared-auth 1.1.5 → 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.
- package/ZitadelProvider.tsx +1 -6
- package/index.ts +2 -1
- package/package.json +1 -1
- package/services/zitadel.auth.service.ts +24 -1
package/ZitadelProvider.tsx
CHANGED
|
@@ -45,12 +45,7 @@ export function ZitadelProvider({ children, config, renderSessionExpiredAction }
|
|
|
45
45
|
setIsConfigured(true)
|
|
46
46
|
|
|
47
47
|
const isAuthenticated = await ZitadelAuthService.isAuthenticated()
|
|
48
|
-
|
|
49
|
-
await ZitadelAuthService.signIn()
|
|
50
|
-
return
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const currentUser = await ZitadelAuthService.getUser()
|
|
48
|
+
const currentUser = isAuthenticated ? await ZitadelAuthService.getUser() : null
|
|
54
49
|
setUser(currentUser)
|
|
55
50
|
} catch (error) {
|
|
56
51
|
console.error("Failed to configure Zitadel auth:", error)
|
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
|
@@ -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
|
-
|
|
173
|
+
if (!user?.access_token) return false;
|
|
174
|
+
return !isJwtExpired(user.access_token);
|
|
152
175
|
}
|
|
153
176
|
|
|
154
177
|
static async getTokenRemainingTime(): Promise<number> {
|