parent
3bbc1b4b36
commit
ca72009ae7
@ -0,0 +1 @@ |
|||||||
|
dist |
@ -1,9 +0,0 @@ |
|||||||
import { createContext, useContext } from "react"; |
|
||||||
// There is no initial value for this context. It will be given in the provider
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
||||||
// @ts-ignore
|
|
||||||
export const NextGraphAuthContext = createContext(undefined); |
|
||||||
export function useNextGraphAuth() { |
|
||||||
return useContext(NextGraphAuthContext); |
|
||||||
} |
|
||||||
//# sourceMappingURL=NextGraphAuthContext.js.map
|
|
@ -1 +0,0 @@ |
|||||||
{"version":3,"file":"NextGraphAuthContext.js","sourceRoot":"","sources":["../../src/NextGraphAuthContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAYlD,+EAA+E;AAC/E,6DAA6D;AAC7D,aAAa;AACb,MAAM,CAAC,MAAM,oBAAoB,GAAG,aAAa,CAAwB,SAAS,CAAC,CAAC;AAEpF,MAAM,UAAU,gBAAgB;IAC5B,OAAO,UAAU,CAAC,oBAAoB,CAAC,CAAC;AAC5C,CAAC"} |
|
@ -1,80 +0,0 @@ |
|||||||
import { jsx as _jsx } from "react/jsx-runtime"; |
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react"; |
|
||||||
import { NextGraphAuthContext, useNextGraphAuth } from "./NextGraphAuthContext.js"; |
|
||||||
import { default as ng, init } from "nextgraphweb"; |
|
||||||
/** |
|
||||||
* Creates special react methods specific to the NextGraph Auth |
|
||||||
* @param dataset the connectedLdoDataset with a nextGraphConnectedPlugin |
|
||||||
* @returns { BrowserNGLdoProvider, useNextGraphAuth } |
|
||||||
*/ |
|
||||||
export function createBrowserNGReactMethods(dataset) { |
|
||||||
const BrowserNGLdoProvider = ({ children, }) => { |
|
||||||
const [session, setSession] = useState({ |
|
||||||
ng: undefined, |
|
||||||
}); |
|
||||||
const [ranInitialAuthCheck, setRanInitialAuthCheck] = useState(false); |
|
||||||
const runInitialAuthCheck = useCallback(async () => { |
|
||||||
//console.log("runInitialAuthCheck called", ranInitialAuthCheck)
|
|
||||||
if (ranInitialAuthCheck) |
|
||||||
return; |
|
||||||
//console.log("init called");
|
|
||||||
setRanInitialAuthCheck(true); |
|
||||||
// TODO: export the types for the session object coming from NG.
|
|
||||||
await init((event) => { |
|
||||||
//console.log("called back in react", event)
|
|
||||||
// callback
|
|
||||||
// once you receive event.status == "loggedin"
|
|
||||||
// you can use the full API
|
|
||||||
if (event.status == "loggedin") { |
|
||||||
setSession({ |
|
||||||
ng, |
|
||||||
sessionId: event.session.session_id, //FIXME: sessionId should be a Number.
|
|
||||||
protectedStoreId: event.session.protected_store_id, |
|
||||||
privateStoreId: event.session.private_store_id, |
|
||||||
publicStoreId: event.session.public_store_id |
|
||||||
}); // TODO: add event.session.user too
|
|
||||||
dataset.setContext("nextgraph", { |
|
||||||
ng, |
|
||||||
sessionId: event.session.session_id |
|
||||||
}); |
|
||||||
} |
|
||||||
else if (event.status == "cancelled" || event.status == "error" || event.status == "loggedout") { |
|
||||||
setSession({ ng: undefined }); |
|
||||||
dataset.setContext("nextgraph", { |
|
||||||
ng: undefined, |
|
||||||
}); |
|
||||||
} |
|
||||||
}, true // singleton: boolean (will your app create many docs in the system, or should it be launched as a unique instance)
|
|
||||||
, []); //list of AccessRequests (for now, leave this empty)
|
|
||||||
}, []); |
|
||||||
const login = useCallback(async () => { |
|
||||||
await ng.login(); |
|
||||||
}, []); |
|
||||||
const logout = useCallback(async () => { |
|
||||||
await ng.logout(); |
|
||||||
}, []); |
|
||||||
useEffect(() => { |
|
||||||
runInitialAuthCheck(); |
|
||||||
}, []); |
|
||||||
const nextGraphAuthFunctions = useMemo(() => ({ |
|
||||||
runInitialAuthCheck, |
|
||||||
login, |
|
||||||
logout, |
|
||||||
session, |
|
||||||
ranInitialAuthCheck, |
|
||||||
}), [ |
|
||||||
login, |
|
||||||
logout, |
|
||||||
ranInitialAuthCheck, |
|
||||||
runInitialAuthCheck, |
|
||||||
session, |
|
||||||
]); |
|
||||||
return (_jsx(NextGraphAuthContext.Provider, { value: nextGraphAuthFunctions, children: children })); |
|
||||||
}; |
|
||||||
return { |
|
||||||
BrowserNGLdoProvider, |
|
||||||
useNextGraphAuth: useNextGraphAuth |
|
||||||
}; |
|
||||||
} |
|
||||||
; |
|
||||||
//# sourceMappingURL=createBrowserNGReactMethods.js.map
|
|
@ -1 +0,0 @@ |
|||||||
{"version":3,"file":"createBrowserNGReactMethods.js","sourceRoot":"","sources":["../../src/createBrowserNGReactMethods.tsx"],"names":[],"mappings":";AAAA,OAAc,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEzE,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAEnF,OAAO,EAAC,OAAO,IAAI,EAAE,EAAE,IAAI,EAAC,MAAM,cAAc,CAAC;AAKjD;;;;GAIG;AACH,MAAM,UAAU,2BAA2B,CACzC,OAA4E;IAG5E,MAAM,oBAAoB,GAAyC,CAAC,EAClE,QAAQ,GACT,EAAE,EAAE;QACH,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CACpC;YACE,EAAE,EAAE,SAAS;SACd,CACF,CAAC;QACF,MAAM,CAAC,mBAAmB,EAAE,sBAAsB,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEtE,MAAM,mBAAmB,GAAG,WAAW,CAAC,KAAK,IAAI,EAAE;YACjD,gEAAgE;YAChE,IAAI,mBAAmB;gBAAE,OAAO;YAEhC,6BAA6B;YAC7B,sBAAsB,CAAC,IAAI,CAAC,CAAC;YAC7B,gEAAgE;YAChE,MAAM,IAAI,CAAE,CAAC,KAA+I,EAAE,EAAE;gBAC9J,4CAA4C;gBAE5C,WAAW;gBACX,8CAA8C;gBAC9C,2BAA2B;gBAC3B,IAAI,KAAK,CAAC,MAAM,IAAI,UAAU,E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GAAG,EAAE,CAAC,CAAC;YACL,mBAAmB;YACnB,KAAK;YACL,MAAM;YACN,OAAO;YACP,mBAAmB;SACpB,CAAC,EACF;YACE,KAAK;YACL,MAAM;YACN,mBAAmB;YACnB,mBAAmB;YACnB,OAAO;SACR,CACF,CAAC;QAEF,OAAO,CACL,KAAC,oBAAoB,CAAC,QAAQ,IAAC,KAAK,EAAE,sBAAsB,YACzD,QAAQ,GACqB,CACjC,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO;QACL,oBAAoB;QACpB,gBAAgB,EAAE,gBAAgB;KACnC,CAAC;AACJ,CAAC;AAAA,CAAC"} |
|
@ -1,2 +0,0 @@ |
|||||||
export * from "./createBrowserNGReactMethods.js"; |
|
||||||
//# sourceMappingURL=index.js.map
|
|
@ -1 +0,0 @@ |
|||||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC"} |
|
@ -1,12 +0,0 @@ |
|||||||
/** |
|
||||||
* Functions for authenticating with NextGraph |
|
||||||
*/ |
|
||||||
export interface NGWalletAuthFunctions { |
|
||||||
login: () => Promise<void>; |
|
||||||
logout: () => Promise<void>; |
|
||||||
session: unknown; |
|
||||||
ranInitialAuthCheck: boolean; |
|
||||||
} |
|
||||||
export declare const NextGraphAuthContext: import("react").Context<NGWalletAuthFunctions>; |
|
||||||
export declare function useNextGraphAuth(): NGWalletAuthFunctions; |
|
||||||
//# sourceMappingURL=NextGraphAuthContext.d.ts.map
|
|
@ -1 +0,0 @@ |
|||||||
{"version":3,"file":"NextGraphAuthContext.d.ts","sourceRoot":"","sources":["../../src/NextGraphAuthContext.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,WAAW,qBAAqB;IAClC,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3B,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,mBAAmB,EAAE,OAAO,CAAC;CAChC;AAKD,eAAO,MAAM,oBAAoB,gDAAkD,CAAC;AAEpF,wBAAgB,gBAAgB,IAAI,qBAAqB,CAExD"} |
|
@ -1,16 +0,0 @@ |
|||||||
import React from "react"; |
|
||||||
import { useNextGraphAuth } from "./NextGraphAuthContext.js"; |
|
||||||
import type { ConnectedLdoDataset, ConnectedPlugin } from "@ldo/connected"; |
|
||||||
import type { NextGraphConnectedPlugin } from "@ldo/connected-nextgraph"; |
|
||||||
/** |
|
||||||
* Creates special react methods specific to the NextGraph Auth |
|
||||||
* @param dataset the connectedLdoDataset with a nextGraphConnectedPlugin |
|
||||||
* @returns { BrowserNGLdoProvider, useNextGraphAuth } |
|
||||||
*/ |
|
||||||
export declare function createBrowserNGReactMethods(dataset: ConnectedLdoDataset<(NextGraphConnectedPlugin | ConnectedPlugin)[]>): { |
|
||||||
BrowserNGLdoProvider: React.FunctionComponent<{ |
|
||||||
children?: React.ReactNode | undefined; |
|
||||||
}>; |
|
||||||
useNextGraphAuth: typeof useNextGraphAuth; |
|
||||||
}; |
|
||||||
//# sourceMappingURL=createBrowserNGReactMethods.d.ts.map
|
|
@ -1 +0,0 @@ |
|||||||
{"version":3,"file":"createBrowserNGReactMethods.d.ts","sourceRoot":"","sources":["../../src/createBrowserNGReactMethods.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAoD,MAAM,OAAO,CAAC;AAEzE,OAAO,EAAwB,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAInF,OAAO,KAAK,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAC3E,OAAO,KAAK,EAAE,wBAAwB,EAA6B,MAAM,0BAA0B,CAAC;AAEpG;;;;GAIG;AACH,wBAAgB,2BAA2B,CACzC,OAAO,EAAE,mBAAmB,CAAC,CAAC,wBAAwB,GAAG,eAAe,CAAC,EAAE,CAAC;;;;;EAgG7E"} |
|
@ -1,2 +0,0 @@ |
|||||||
export * from "./createBrowserNGReactMethods.js"; |
|
||||||
//# sourceMappingURL=index.d.ts.map
|
|
@ -1 +0,0 @@ |
|||||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC"} |
|
@ -0,0 +1,99 @@ |
|||||||
|
import React, { useCallback, useEffect, useMemo, useState } from "react"; |
||||||
|
import type { FunctionComponent, PropsWithChildren } from "react"; |
||||||
|
import { NextGraphAuthContext, useNextGraphAuth } from "./NextGraphAuthContext.js"; |
||||||
|
import type { NextGraphConnectedContext } from "@ldo/connected-nextgraph"; |
||||||
|
import {default as ng, init} from "nextgraphweb"; |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates special react methods specific to the NextGraph Auth |
||||||
|
* @returns { BrowserNextGraphAuth, useNextGraphAuth } |
||||||
|
*/ |
||||||
|
export function createNextGraphAuthMethod () { |
||||||
|
|
||||||
|
const NextGraphAuthMethod: FunctionComponent<PropsWithChildren> = ({ |
||||||
|
children, |
||||||
|
}) => { |
||||||
|
const [session, setSession] = useState<NextGraphConnectedContext>( |
||||||
|
{ |
||||||
|
ng: undefined, |
||||||
|
} |
||||||
|
); |
||||||
|
const [ranInitialAuthCheck, setRanInitialAuthCheck] = useState(false); |
||||||
|
|
||||||
|
const runInitialAuthCheck = useCallback(async () => { |
||||||
|
//console.log("runInitialAuthCheck called", ranInitialAuthCheck)
|
||||||
|
if (ranInitialAuthCheck) return; |
||||||
|
|
||||||
|
//console.log("init called");
|
||||||
|
setRanInitialAuthCheck(true); |
||||||
|
// TODO: export the types for the session object coming from NG.
|
||||||
|
await init( (event: { status: string; session: { session_id: unknown; protected_store_id: unknown; private_store_id: unknown; public_store_id: unknown; }; }) => { |
||||||
|
//console.log("called back in react", event)
|
||||||
|
|
||||||
|
// callback
|
||||||
|
// once you receive event.status == "loggedin"
|
||||||
|
// you can use the full API
|
||||||
|
if (event.status == "loggedin") { |
||||||
|
setSession({
|
||||||
|
ng,
|
||||||
|
sessionId: event.session.session_id as string, //FIXME: sessionId should be a Number.
|
||||||
|
protectedStoreId: event.session.protected_store_id as string, |
||||||
|
privateStoreId: event.session.private_store_id as string, |
||||||
|
publicStoreId: event.session.public_store_id as string |
||||||
|
}); // TODO: add event.session.user too
|
||||||
|
|
||||||
|
} |
||||||
|
else if (event.status == "cancelled" || event.status == "error" || event.status == "loggedout") { |
||||||
|
setSession({ ng: undefined }); |
||||||
|
} |
||||||
|
} |
||||||
|
, true // singleton: boolean (will your app create many docs in the system, or should it be launched as a unique instance)
|
||||||
|
, []); //list of AccessRequests (for now, leave this empty)
|
||||||
|
|
||||||
|
}, []); |
||||||
|
|
||||||
|
|
||||||
|
const login = useCallback( |
||||||
|
async () => { |
||||||
|
await ng.login(); |
||||||
|
}, |
||||||
|
[], |
||||||
|
); |
||||||
|
|
||||||
|
const logout = useCallback(async () => { |
||||||
|
await ng.logout(); |
||||||
|
}, []); |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
runInitialAuthCheck(); |
||||||
|
}, []); |
||||||
|
|
||||||
|
const nextGraphAuthFunctions = useMemo( |
||||||
|
() => ({ |
||||||
|
runInitialAuthCheck, |
||||||
|
login, |
||||||
|
logout, |
||||||
|
session, |
||||||
|
ranInitialAuthCheck, |
||||||
|
}), |
||||||
|
[ |
||||||
|
login, |
||||||
|
logout, |
||||||
|
ranInitialAuthCheck, |
||||||
|
runInitialAuthCheck, |
||||||
|
session, |
||||||
|
], |
||||||
|
); |
||||||
|
|
||||||
|
return ( |
||||||
|
<NextGraphAuthContext.Provider value={nextGraphAuthFunctions}> |
||||||
|
{children} |
||||||
|
</NextGraphAuthContext.Provider> |
||||||
|
); |
||||||
|
}; |
||||||
|
|
||||||
|
return { |
||||||
|
NextGraphAuthMethod, |
||||||
|
useNextGraphAuth: useNextGraphAuth |
||||||
|
}; |
||||||
|
}; |
@ -1,2 +1,4 @@ |
|||||||
export * from "./createBrowserNGReactMethods.js"; |
export * from "./createBrowserNGReactMethods.js"; |
||||||
|
|
||||||
|
export * from "./createNextGraphAuthMethods.js"; |
||||||
|
|
||||||
|
Loading…
Reference in new issue