forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSession.ts
More file actions
41 lines (34 loc) · 1.1 KB
/
Copy pathuseSession.ts
File metadata and controls
41 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { UseSessionReturn } from '@clerk/types';
import { useAssertWrappedByClerkProvider, useSessionContext } from '../contexts';
type UseSession = () => UseSessionReturn;
/**
* Returns the current auth state and if a session exists, the session object.
*
* Until Clerk loads and initializes, `isLoaded` will be set to `false`.
* Once Clerk loads, `isLoaded` will be set to `true`, and you can
* safely access `isSignedIn` state and `session`.
*
* @example
* A simple example:
*
* import { useSession } from '@clerk/clerk-react'
*
* function Hello() {
* const { isSignedIn, session } = useSession();
* if(!isSignedIn) {
* return null;
* }
* return <div>{session.updatedAt}</div>
* }
*/
export const useSession: UseSession = () => {
useAssertWrappedByClerkProvider('useSession');
const session = useSessionContext();
if (session === undefined) {
return { isLoaded: false, isSignedIn: undefined, session: undefined };
}
if (session === null) {
return { isLoaded: true, isSignedIn: false, session: null };
}
return { isLoaded: true, isSignedIn: true, session };
};