-
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.
Merge pull request #21 from DIMO-Network/moiz/auth-state
Persistent Auth State
- Loading branch information
Showing
11 changed files
with
215 additions
and
69 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,66 @@ | ||
// DimoAuthContext.tsx | ||
import React, { createContext, useContext, useEffect, useState } from "react"; | ||
import { getJWTFromCookies } from "../../storage/storageManager"; | ||
import { isTokenExpired } from "../../token/tokenManager"; | ||
|
||
// Define the type of the context | ||
type DimoAuthContextType = { | ||
isAuthenticated: boolean; // Read-only for app developers | ||
}; | ||
|
||
// Create the context | ||
const DimoAuthContext = createContext<DimoAuthContextType | undefined>( | ||
undefined | ||
); | ||
|
||
// Internal updater function type (hidden from the app) | ||
type DimoAuthUpdaterContextType = { | ||
setAuthenticated: React.Dispatch<React.SetStateAction<boolean>>; | ||
}; | ||
const DimoAuthUpdaterContext = createContext< | ||
DimoAuthUpdaterContextType | undefined | ||
>(undefined); | ||
|
||
// Provider to manage auth state | ||
export const DimoAuthProvider = ({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) => { | ||
const [isAuthenticated, setAuthenticated] = useState(false); | ||
|
||
useEffect(() => { | ||
const jwt = getJWTFromCookies(); | ||
if (jwt && !isTokenExpired(jwt)) { | ||
setAuthenticated(true); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<DimoAuthContext.Provider value={{ isAuthenticated }}> | ||
<DimoAuthUpdaterContext.Provider value={{ setAuthenticated }}> | ||
{children} | ||
</DimoAuthUpdaterContext.Provider> | ||
</DimoAuthContext.Provider> | ||
); | ||
}; | ||
|
||
// Hook to expose read-only auth state to developers | ||
export const useDimoAuthState = () => { | ||
const context = useContext(DimoAuthContext); | ||
if (!context) { | ||
throw new Error("useDimoAuthState must be used within a DimoAuthProvider"); | ||
} | ||
return context; // Only exposes isAuthenticated | ||
}; | ||
|
||
// Internal SDK hook to update the state | ||
export const useDimoAuthUpdater = () => { | ||
const context = useContext(DimoAuthUpdaterContext); | ||
if (!context) { | ||
throw new Error( | ||
"useDimoAuthUpdater must be used within a DimoAuthProvider" | ||
); | ||
} | ||
return context; // Exposes setAuthenticated only for SDK | ||
}; |
Oops, something went wrong.