@orsetra/shared-auth 1.0.3 → 1.0.4

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/SessionProvider.tsx +29 -36
  2. package/package.json +1 -1
@@ -1,70 +1,63 @@
1
1
  "use client"
2
2
 
3
3
  import { createContext, useContext, useEffect, useState, ReactNode } from "react"
4
-
5
- interface SessionUser {
6
- access_token: string
7
- id_token?: string
8
- expires_at?: number
9
- profile: {
10
- sub: string
11
- email?: string
12
- name?: string
13
- preferred_username?: string
14
- [key: string]: any
15
- }
16
- }
4
+ import { ZitadelAuthService, ZitadelConfig } from "./services/zitadel.auth.service"
5
+ import type { User } from "oidc-client-ts"
17
6
 
18
7
  interface SessionContextType {
19
- user: SessionUser | null
8
+ user: User | null
20
9
  accessToken: string | null
21
10
  isLoading: boolean
22
11
  isAuthenticated: boolean
12
+ logout: () => Promise<void>
23
13
  }
24
14
 
25
15
  const SessionContext = createContext<SessionContextType | undefined>(undefined)
26
16
 
27
17
  interface SessionProviderProps {
28
18
  children: ReactNode
19
+ config: ZitadelConfig
29
20
  }
30
21
 
31
22
  /**
32
23
  * Provider de session pour les micro-apps
33
24
  * À utiliser après validation par le middleware
34
- * Lit directement depuis localStorage (pas de configuration requise)
25
+ * Utilise ZitadelAuthService pour récupérer l'utilisateur
35
26
  */
36
- export function SessionProvider({ children }: SessionProviderProps) {
37
- const [user, setUser] = useState<SessionUser | null>(null)
27
+ export function SessionProvider({ children, config }: SessionProviderProps) {
28
+ const [user, setUser] = useState<User | null>(null)
38
29
  const [isLoading, setIsLoading] = useState(true)
30
+ const [isConfigured, setIsConfigured] = useState(false)
39
31
 
40
32
  useEffect(() => {
41
- const authority = process.env.NEXT_PUBLIC_ZITADEL_AUTHORITY
42
- const clientId = process.env.NEXT_PUBLIC_ZITADEL_CLIENT_ID
43
- const storageKey = `oidc.user:${authority}:${clientId}`
44
-
45
- try {
46
- const userDataString = localStorage.getItem(storageKey)
47
-
48
- if (userDataString) {
49
- const userData = JSON.parse(userDataString) as SessionUser
50
- const isExpired = userData.expires_at && userData.expires_at * 1000 < Date.now()
51
-
52
- if (!isExpired && userData.access_token) {
53
- setUser(userData)
54
- }
33
+ const loadUser = async () => {
34
+ try {
35
+ ZitadelAuthService.configureAuth(config)
36
+ setIsConfigured(true)
37
+ const currentUser = await ZitadelAuthService.getUser()
38
+ setUser(currentUser)
39
+ } catch (error) {
40
+ console.error('Error loading user:', error)
41
+ } finally {
42
+ setIsLoading(false)
55
43
  }
56
- } catch (error) {
57
- console.error('Error reading session:', error)
58
- } finally {
59
- setIsLoading(false)
60
44
  }
61
- }, [])
45
+
46
+ loadUser()
47
+ }, [config])
48
+
49
+ const logout = async () => {
50
+ if (!isConfigured) return
51
+ await ZitadelAuthService.signOut()
52
+ setUser(null)
53
+ }
62
54
 
63
55
  const value: SessionContextType = {
64
56
  user,
65
57
  accessToken: user?.access_token ?? null,
66
58
  isLoading,
67
59
  isAuthenticated: !!user,
60
+ logout,
68
61
  }
69
62
 
70
63
  return (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orsetra/shared-auth",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Shared authentication utilities for Orsetra platform using Zitadel",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",