style: format
This commit is contained in:
parent
185611f09a
commit
6c17ea89d9
|
@ -1,40 +1,45 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
export type Session = string;
|
export type Session = string;
|
||||||
export const LumeIdentityContext = React.createContext<{
|
export const LumeIdentityContext = React.createContext<
|
||||||
|
| {
|
||||||
session: Session | undefined;
|
session: Session | undefined;
|
||||||
setSession: React.Dispatch<React.SetStateAction<Session | undefined>>;
|
setSession: React.Dispatch<React.SetStateAction<Session | undefined>>;
|
||||||
} | undefined>(undefined);
|
}
|
||||||
|
| undefined
|
||||||
|
>(undefined);
|
||||||
export function useLumeIndentity() {
|
export function useLumeIndentity() {
|
||||||
const contextValue = React.useContext(LumeIdentityContext);
|
const contextValue = React.useContext(LumeIdentityContext);
|
||||||
|
|
||||||
// When the `session` changes we want to update the `session` in the local storage?
|
// When the `session` changes we want to update the `session` in the local storage?
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (contextValue?.session) {
|
if (contextValue?.session) {
|
||||||
localStorage.setItem('lume-session', contextValue.session);
|
localStorage.setItem("lume-session", contextValue.session);
|
||||||
} else {
|
} else {
|
||||||
localStorage.removeItem('lume-session');
|
localStorage.removeItem("lume-session");
|
||||||
}
|
}
|
||||||
}, [contextValue?.session]);
|
}, [contextValue?.session]);
|
||||||
|
|
||||||
// Get the session from the local storage
|
// Get the session from the local storage
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const session = localStorage.getItem('lume-session');
|
const session = localStorage.getItem("lume-session");
|
||||||
if (session) {
|
if (session) {
|
||||||
contextValue?.setSession(session);
|
contextValue?.setSession(session);
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (contextValue === undefined) {
|
if (contextValue === undefined) {
|
||||||
throw new Error('useLumeIndentity hook is being used outside of its context. Please ensure that it is wrapped within a <LumeIdentityProvider>.');
|
throw new Error(
|
||||||
|
"useLumeIdentity hook is being used outside of its context. Please ensure that it is wrapped within a <LumeIdentityProvider>."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isSignedIn: !!contextValue.session,
|
isSignedIn: !!contextValue.session,
|
||||||
signIn: (key: string) => {
|
signIn: (key: string) => {
|
||||||
console.log('signing in with key', key);
|
console.log("signing in with key", key);
|
||||||
// TODO: From the key generate a session, and store it
|
// TODO: From the key generate a session, and store it
|
||||||
contextValue.setSession('session');
|
contextValue.setSession("session");
|
||||||
},
|
},
|
||||||
signOut: () => {
|
signOut: () => {
|
||||||
contextValue.setSession(undefined);
|
contextValue.setSession(undefined);
|
||||||
|
|
Loading…
Reference in New Issue