@orsetra/shared-auth 1.0.5 → 1.0.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/package.json
CHANGED
|
@@ -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(),
|
|
@@ -70,14 +87,7 @@ export class ZitadelAuthService {
|
|
|
70
87
|
static async handleCallback(): Promise<User> {
|
|
71
88
|
try {
|
|
72
89
|
const user = await this.getAuth().userManager.signinRedirectCallback();
|
|
73
|
-
|
|
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
|
-
|
|
90
|
+
// Le cookie est mis à jour automatiquement via l'event userLoaded
|
|
81
91
|
return user;
|
|
82
92
|
} catch (error) {
|
|
83
93
|
throw this.handleAuthError(error);
|