@orsetra/shared-auth 1.0.5 → 1.0.7

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.
@@ -28,12 +28,15 @@ export function SessionProvider({ children, config }: SessionProviderProps) {
28
28
  const [user, setUser] = useState<User | null>(null)
29
29
  const [isLoading, setIsLoading] = useState(true)
30
30
  const [isConfigured, setIsConfigured] = useState(false)
31
+ // Configure auth synchronously before any render
32
+ if (!ZitadelAuthService.isConfigured()) {
33
+ ZitadelAuthService.configureAuth(config)
34
+ setIsConfigured(true)
35
+ }
31
36
 
32
37
  useEffect(() => {
33
38
  const loadUser = async () => {
34
39
  try {
35
- ZitadelAuthService.configureAuth(config)
36
- setIsConfigured(true)
37
40
  const currentUser = await ZitadelAuthService.getUser()
38
41
  setUser(currentUser)
39
42
  } catch (error) {
package/package.json CHANGED
@@ -1,54 +1,54 @@
1
- {
2
- "name": "@orsetra/shared-auth",
3
- "version": "1.0.5",
4
- "description": "Shared authentication utilities for Orsetra platform using Zitadel",
5
- "main": "./index.ts",
6
- "types": "./index.ts",
7
- "exports": {
8
- ".": "./index.ts",
9
- "./config": "./config/zitadel.config.ts",
10
- "./services": "./services/zitadel.auth.service.ts",
11
- "./components": "./components/index.ts",
12
- "./utils": "./utils/index.ts"
13
- },
14
- "files": [
15
- "index.ts",
16
- "ZitadelProvider.tsx",
17
- "SessionProvider.tsx",
18
- "ProtectedRoute.tsx",
19
- "config",
20
- "services",
21
- "components",
22
- "hooks",
23
- "utils",
24
- "README.md"
25
- ],
26
- "publishConfig": {
27
- "access": "public"
28
- },
29
- "repository": {
30
- "type": "git",
31
- "url": "https://github.com/orsetra/console-ui.git",
32
- "directory": "packages/shared-auth"
33
- },
34
- "keywords": [
35
- "authentication",
36
- "zitadel",
37
- "oidc",
38
- "oauth2",
39
- "orsetra"
40
- ],
41
- "peerDependencies": {
42
- "react": "^18.0.0 || ^19.0.0",
43
- "react-dom": "^18.0.0 || ^19.0.0",
44
- "next": "^14.0.0 || ^15.0.0"
45
- },
46
- "dependencies": {
47
- "oidc-client-ts": "^3.2.1",
48
- "jose": "^5.9.6"
49
- },
50
- "devDependencies": {
51
- "@types/react": "^19",
52
- "typescript": "^5"
53
- }
54
- }
1
+ {
2
+ "name": "@orsetra/shared-auth",
3
+ "version": "1.0.7",
4
+ "description": "Shared authentication utilities for Orsetra platform using Zitadel",
5
+ "main": "./index.ts",
6
+ "types": "./index.ts",
7
+ "exports": {
8
+ ".": "./index.ts",
9
+ "./config": "./config/zitadel.config.ts",
10
+ "./services": "./services/zitadel.auth.service.ts",
11
+ "./components": "./components/index.ts",
12
+ "./utils": "./utils/index.ts"
13
+ },
14
+ "files": [
15
+ "index.ts",
16
+ "ZitadelProvider.tsx",
17
+ "SessionProvider.tsx",
18
+ "ProtectedRoute.tsx",
19
+ "config",
20
+ "services",
21
+ "components",
22
+ "hooks",
23
+ "utils",
24
+ "README.md"
25
+ ],
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/orsetra/console-ui.git",
32
+ "directory": "packages/shared-auth"
33
+ },
34
+ "keywords": [
35
+ "authentication",
36
+ "zitadel",
37
+ "oidc",
38
+ "oauth2",
39
+ "orsetra"
40
+ ],
41
+ "peerDependencies": {
42
+ "react": "^18.0.0 || ^19.0.0",
43
+ "react-dom": "^18.0.0 || ^19.0.0",
44
+ "next": "^14.0.0 || ^15.0.0"
45
+ },
46
+ "dependencies": {
47
+ "oidc-client-ts": "^3.2.1",
48
+ "jose": "^5.9.6"
49
+ },
50
+ "devDependencies": {
51
+ "@types/react": "^19",
52
+ "typescript": "^5"
53
+ }
54
+ }
@@ -11,6 +11,15 @@ export interface ZitadelAuth {
11
11
  userManager: UserManager;
12
12
  }
13
13
 
14
+ function updateAuthCookie(user: User | null) {
15
+ if (user?.access_token) {
16
+ const maxAge = user.expires_at
17
+ ? user.expires_at - Math.floor(Date.now() / 1000)
18
+ : 3600;
19
+ document.cookie = `auth_session=1; path=/; max-age=${maxAge}; SameSite=Lax`;
20
+ }
21
+ }
22
+
14
23
  function createZitadelAuth(zitadelConfig: ZitadelConfig): ZitadelAuth {
15
24
  const authConfig = createAuthConfig(zitadelConfig);
16
25
 
@@ -20,6 +29,14 @@ function createZitadelAuth(zitadelConfig: ZitadelConfig): ZitadelAuth {
20
29
  ...authConfig,
21
30
  });
22
31
 
32
+ userManager.events.addUserLoaded((user) => {
33
+ updateAuthCookie(user);
34
+ });
35
+
36
+ userManager.events.addUserUnloaded(() => {
37
+ document.cookie = 'auth_session=; path=/; max-age=0';
38
+ });
39
+
23
40
  return {
24
41
  authorize: () => userManager.signinRedirect(),
25
42
  signout: () => userManager.signoutRedirect(),
@@ -58,9 +75,13 @@ export class ZitadelAuthService {
58
75
  }
59
76
  }
60
77
 
78
+ static isConfigured(): boolean {
79
+ return this.zitadelAuth !== null;
80
+ }
81
+
61
82
  static async getUser(): Promise<User | null> {
62
83
  try {
63
- return await this.getAuth().userManager.getUser();
84
+ return await this.zitadelAuth!.userManager.getUser();
64
85
  } catch (error) {
65
86
  console.error('Error getting user:', error);
66
87
  return null;
@@ -70,14 +91,7 @@ export class ZitadelAuthService {
70
91
  static async handleCallback(): Promise<User> {
71
92
  try {
72
93
  const user = await this.getAuth().userManager.signinRedirectCallback();
73
- console.log(user)
74
- if (user?.access_token) {
75
- const maxAge = user.expires_at
76
- ? user.expires_at - Math.floor(Date.now() / 1000)
77
- : 3600;
78
- document.cookie = `auth_session=1; path=/; max-age=${maxAge}; SameSite=Lax`;
79
- }
80
-
94
+ // Le cookie est mis à jour automatiquement via l'event userLoaded
81
95
  return user;
82
96
  } catch (error) {
83
97
  throw this.handleAuthError(error);