-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Muhammed Rameez
authored and
Muhammed Rameez
committed
Dec 27, 2023
1 parent
4dc1236
commit de47010
Showing
16 changed files
with
180 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './swr'; | ||
export * from './providers'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './user'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
createContext, | ||
ReactNode, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
|
||
import { useSupabaseClient } from '@supabase/auth-helpers-react'; | ||
import { useProfile } from '../swr'; | ||
import { ProfileExtended } from '../types'; | ||
|
||
type UserContextValue = { | ||
user: ProfileExtended; | ||
}; | ||
|
||
type UserProviderProps = { | ||
onUnauthorized?: () => void; | ||
children: ReactNode; | ||
loading?: ReactNode; | ||
}; | ||
|
||
const UserContext = createContext<UserContextValue | undefined>(undefined); | ||
|
||
UserContext.displayName = 'UserContext'; | ||
|
||
export const UserProvider = ({ children, ...props }: UserProviderProps) => { | ||
const [profileId, setProfileId] = useState<string | null>(null); | ||
const supabase = useSupabaseClient(); | ||
const { data: user } = useProfile({ id: profileId }); | ||
|
||
/** Get profileId from session */ | ||
useEffect(() => { | ||
const { | ||
data: { subscription }, | ||
} = supabase.auth.onAuthStateChange((event, session) => { | ||
if (!session) { | ||
setProfileId(null); | ||
props.onUnauthorized?.(); | ||
} else { | ||
setProfileId(session.user?.id); | ||
} | ||
}); | ||
|
||
return () => { | ||
subscription?.unsubscribe(); | ||
}; | ||
}, [supabase]); | ||
|
||
if (!user) { | ||
return props.loading ?? null; | ||
} | ||
|
||
return ( | ||
<UserContext.Provider | ||
value={{ | ||
user: user ?? null, | ||
}} | ||
> | ||
{children} | ||
</UserContext.Provider> | ||
); | ||
}; | ||
|
||
export const useActiveUser = () => { | ||
const context = useContext(UserContext); | ||
|
||
if (context === undefined) { | ||
throw new Error('useActiveUser was used outside of its Provider'); | ||
} | ||
|
||
return context; | ||
}; | ||
|
||
// export const useUserRole = () => { | ||
// const { user } = useActiveUser(); | ||
|
||
// return user?.profile?.role ?? null; | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './service'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { LearningAppSupabase } from '@learning-app/supabase'; | ||
import { ProfileGetRequest, ProfileGetResponse } from './types'; | ||
|
||
export async function getProfile( | ||
supabase: LearningAppSupabase, | ||
params: ProfileGetRequest | ||
): Promise<ProfileGetResponse> { | ||
const { data, error } = await supabase | ||
.from('Profile') | ||
.select('*') | ||
.match({ id: params.id }) | ||
.single(); | ||
|
||
if (error) { | ||
throw error; | ||
} | ||
|
||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ProfileExtended } from '../../../types'; | ||
export type ProfileGetRequest = { | ||
id: string | null; | ||
}; | ||
|
||
export type ProfileGetResponse = ProfileExtended; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { PostgrestError } from '@supabase/supabase-js'; | ||
import { useSupabaseClient } from '@supabase/auth-helpers-react'; | ||
|
||
import useSWR from 'swr'; | ||
import { AuthSWRKeys } from '../keys'; | ||
import { | ||
ProfileGetRequest, | ||
ProfileGetResponse, | ||
getProfile, | ||
} from '../../services/profile/get'; | ||
|
||
export function useProfile(params: ProfileGetRequest) { | ||
const supabase = useSupabaseClient(); | ||
const key = params.id | ||
? [AuthSWRKeys.PROFILE, AuthSWRKeys.GET, params.id] | ||
: null; | ||
return useSWR<ProfileGetResponse, PostgrestError, string[] | null>(key, () => | ||
getProfile(supabase, params) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useProfile'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Database } from '@learning-app/supabase'; | ||
|
||
export type Profile = Database['public']['Tables']['Profile']['Row']; | ||
|
||
export type ProfileExtended = Pick< | ||
Profile, | ||
'id' | 'firstName' | 'lastName' | 'mobile' | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters