11'use client' // Ensures this runs only on the client-side
22
3- import type { PropsWithChildren } from 'react'
4- import { createContext , useContext , useEffect , useState } from 'react'
5- import Cookies from 'js-cookie '
3+ import type { ComponentType , PropsWithChildren } from 'react'
4+ import { useEffect } from 'react'
5+ import { createContext , useContext , useState } from 'react '
66import { tw } from '@twind/core'
77import { LoadingAnimation } from '@helpwave/common/components/LoadingAnimation'
8- import type { LoginData } from '@/components/pages/login'
98import { LoginPage } from '@/components/pages/login'
10-
11- type Identity = {
12- token : string ,
13- name : string ,
14- }
9+ import { login , logout , restoreSession } from '@/api/auth/authService'
10+ import type { User } from 'oidc-client-ts'
11+ import { REDIRECT_URI } from '@/api/auth/config'
1512
1613type AuthContextType = {
17- identity : Identity ,
14+ identity : User ,
1815 logout : ( ) => void ,
1916}
2017
2118const AuthContext = createContext < AuthContextType | undefined > ( undefined )
2219
2320type AuthState = {
24- identity ?: Identity ,
21+ identity ?: User ,
2522 isLoading : boolean ,
2623}
2724
28- const cookieName = 'authToken'
29-
3025export const AuthProvider = ( { children } : PropsWithChildren ) => {
3126 const [ { isLoading, identity } , setAuthState ] = useState < AuthState > ( { isLoading : true } )
3227
33- const checkIdentity = ( ) => {
34- setAuthState ( { isLoading : true } )
35- const token = Cookies . get ( cookieName )
36- const newAuthState = ! ! token
37- if ( newAuthState ) {
38- const identity : Identity = { token : 'test-token' , name : 'Max Mustermann' }
39- setAuthState ( { identity, isLoading : false } )
40- } else {
41- setAuthState ( { isLoading : false } )
42- }
43- }
44-
45- // Check authentication state on first load
4628 useEffect ( ( ) => {
47- checkIdentity ( )
29+ restoreSession ( ) . then ( identity => {
30+ setAuthState ( {
31+ identity,
32+ isLoading : false ,
33+ } )
34+ } )
4835 } , [ ] )
4936
50- const login = async ( _ : LoginData ) => {
51- // TODO do real login
52- Cookies . set ( cookieName , 'testdata' , { expires : 1 } )
53- checkIdentity ( )
54- return true
55- }
56-
57- const logout = ( ) => {
58- Cookies . remove ( cookieName )
59- checkIdentity ( )
60- }
61-
6237 if ( ! identity && isLoading ) {
6338 return (
6439 < div className = { tw ( 'flex flex-col items-center justify-center w-screen h-screen' ) } >
@@ -68,7 +43,12 @@ export const AuthProvider = ({ children }: PropsWithChildren) => {
6843 }
6944
7045 if ( ! identity ) {
71- return ( < LoginPage login = { login } /> )
46+ return (
47+ < LoginPage login = { async ( ) => {
48+ await login ( REDIRECT_URI + `?redirect_uri=${ encodeURIComponent ( window . location . href ) } ` )
49+ return true
50+ } } />
51+ )
7252 }
7353
7454 return (
@@ -78,11 +58,27 @@ export const AuthProvider = ({ children }: PropsWithChildren) => {
7858 )
7959}
8060
61+ export const withAuth = < P extends object > ( Component : ComponentType < P > ) => {
62+ const WrappedComponent = ( props : P ) => (
63+ < AuthProvider >
64+ < Component { ...props } />
65+ </ AuthProvider >
66+ )
67+ WrappedComponent . displayName = `withAuth(${ Component . displayName || Component . name || 'Component' } )`
68+
69+ return WrappedComponent
70+ }
71+
72+
8173// Custom hook for using AuthContext
8274export const useAuth = ( ) => {
8375 const context = useContext ( AuthContext )
8476 if ( ! context ) {
8577 throw new Error ( 'useAuth must be used within an AuthProvider' )
8678 }
87- return context
79+ const authHeader = {
80+ 'Content-Type' : 'application/json' ,
81+ 'Authorization' : `Bearer ${ context . identity . access_token } ` ,
82+ }
83+ return { ...context , authHeader }
8884}
0 commit comments