@usehercules/auth 1.0.22 → 1.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/convex-react/index.cjs +2 -0
- package/dist/convex-react/index.cjs.map +1 -0
- package/dist/convex-react/index.d.cts +10 -0
- package/dist/convex-react/index.d.ts +10 -0
- package/dist/convex-react/index.js +2 -0
- package/dist/convex-react/index.js.map +1 -0
- package/dist/react/index.cjs +2 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +32 -0
- package/dist/react/index.d.ts +32 -0
- package/dist/react/index.js +2 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +17 -4
- package/src/convex-react/ConvexProviderWithHercules.tsx +56 -0
- package/src/convex-react/index.ts +1 -0
- package/src/react/HerculesAuthProvider.tsx +48 -0
- package/src/react/index.ts +3 -0
- package/src/react/use-auth.ts +4 -0
- package/src/react/use-user.ts +30 -0
- package/tsup.config.ts +4 -1
- package/dist/index.cjs +0 -44
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -4
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -8
- package/dist/index.js.map +0 -1
- package/src/index.ts +0 -4
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var u=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var A=(t,e)=>{for(var o in e)u(t,o,{get:e[o],enumerable:!0})},p=(t,e,o,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of f(e))!m.call(t,r)&&r!==o&&u(t,r,{get:()=>e[r],enumerable:!(n=d(e,r))||n.enumerable});return t};var v=t=>p(u({},"__esModule",{value:!0}),t);var x={};A(x,{ConvexProviderWithHerculesAuth:()=>C});module.exports=v(x);var s=require("convex/react"),i=require("react"),l=require("react-oidc-context"),a=require("react/jsx-runtime");function C({children:t,client:e}){return(0,a.jsx)(s.ConvexProviderWithAuth,{client:e,useAuth:k,children:t})}function k(){let{isAuthenticated:t,user:e,isLoading:o,signinSilent:n}=(0,l.useAuth)(),r=e?.id_token,c=(0,i.useCallback)(async({forceRefreshToken:h})=>{try{return h?(await n())?.id_token??null:r??null}catch{return null}},[r,n]);return(0,i.useMemo)(()=>({isLoading:!o,isAuthenticated:t??!1,fetchAccessToken:c}),[o,t,c])}0&&(module.exports={ConvexProviderWithHerculesAuth});
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/convex-react/index.ts","../../src/convex-react/ConvexProviderWithHercules.tsx"],"sourcesContent":["export * from \"./ConvexProviderWithHercules\";\n","\"use client\";\n\nimport type { ReactNode } from \"react\";\nimport { ConvexProviderWithAuth, type ConvexReactClient } from \"convex/react\";\nimport { useCallback, useMemo } from \"react\";\nimport { useAuth } from \"react-oidc-context\";\nimport type { HerculesAuthProvider } from \"../react/HerculesAuthProvider\";\n\n/**\n * A wrapper React component which provides a {@link ConvexReactClient}\n * authenticated with Hercules Auth.\n *\n * It must be wrapped by a configured `{@link HerculesAuthProvider}`.\n *\n * @public\n */\nexport function ConvexProviderWithHerculesAuth({\n children,\n client,\n}: {\n children: ReactNode;\n client: ConvexReactClient;\n}) {\n return (\n <ConvexProviderWithAuth client={client} useAuth={useUseAuthFromHercules}>\n {children}\n </ConvexProviderWithAuth>\n );\n}\n\nfunction useUseAuthFromHercules() {\n const { isAuthenticated, user, isLoading, signinSilent } = useAuth();\n const idToken = user?.id_token;\n const fetchAccessToken = useCallback(\n async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => {\n try {\n if (forceRefreshToken) {\n const user = await signinSilent();\n return user?.id_token ?? null;\n }\n return idToken ?? null;\n } catch {\n return null;\n }\n },\n [idToken, signinSilent],\n );\n return useMemo(\n () => ({\n isLoading: !isLoading,\n isAuthenticated: isAuthenticated ?? false,\n fetchAccessToken,\n }),\n [isLoading, isAuthenticated, fetchAccessToken],\n );\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oCAAAE,IAAA,eAAAC,EAAAH,GCGA,IAAAI,EAA+D,wBAC/DA,EAAqC,iBACrCC,EAAwB,8BAmBpBC,EAAA,6BARG,SAASC,EAA+B,CAC7C,SAAAC,EACA,OAAAC,CACF,EAGG,CACD,SACE,OAAC,0BAAuB,OAAQA,EAAQ,QAASC,EAC9C,SAAAF,EACH,CAEJ,CAEA,SAASE,GAAyB,CAChC,GAAM,CAAE,gBAAAC,EAAiB,KAAAC,EAAM,UAAAC,EAAW,aAAAC,CAAa,KAAI,WAAQ,EAC7DC,EAAUH,GAAM,SAChBI,KAAmB,eACvB,MAAO,CAAE,kBAAAC,CAAkB,IAAsC,CAC/D,GAAI,CACF,OAAIA,GACW,MAAMH,EAAa,IACnB,UAAY,KAEpBC,GAAW,IACpB,MAAQ,CACN,OAAO,IACT,CACF,EACA,CAACA,EAASD,CAAY,CACxB,EACA,SAAO,WACL,KAAO,CACL,UAAW,CAACD,EACZ,gBAAiBF,GAAmB,GACpC,iBAAAK,CACF,GACA,CAACH,EAAWF,EAAiBK,CAAgB,CAC/C,CACF","names":["convex_react_exports","__export","ConvexProviderWithHerculesAuth","__toCommonJS","import_react","import_react_oidc_context","import_jsx_runtime","ConvexProviderWithHerculesAuth","children","client","useUseAuthFromHercules","isAuthenticated","user","isLoading","signinSilent","idToken","fetchAccessToken","forceRefreshToken"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { ConvexReactClient } from 'convex/react';
|
|
4
|
+
|
|
5
|
+
declare function ConvexProviderWithHerculesAuth({ children, client, }: {
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
client: ConvexReactClient;
|
|
8
|
+
}): react_jsx_runtime.JSX.Element;
|
|
9
|
+
|
|
10
|
+
export { ConvexProviderWithHerculesAuth };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { ConvexReactClient } from 'convex/react';
|
|
4
|
+
|
|
5
|
+
declare function ConvexProviderWithHerculesAuth({ children, client, }: {
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
client: ConvexReactClient;
|
|
8
|
+
}): react_jsx_runtime.JSX.Element;
|
|
9
|
+
|
|
10
|
+
export { ConvexProviderWithHerculesAuth };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{ConvexProviderWithAuth as c}from"convex/react";import{useCallback as s,useMemo as l}from"react";import{useAuth as a}from"react-oidc-context";import{jsx as d}from"react/jsx-runtime";function v({children:e,client:t}){return d(c,{client:t,useAuth:h,children:e})}function h(){let{isAuthenticated:e,user:t,isLoading:r,signinSilent:o}=a(),n=t?.id_token,i=s(async({forceRefreshToken:u})=>{try{return u?(await o())?.id_token??null:n??null}catch{return null}},[n,o]);return l(()=>({isLoading:!r,isAuthenticated:e??!1,fetchAccessToken:i}),[r,e,i])}export{v as ConvexProviderWithHerculesAuth};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/convex-react/ConvexProviderWithHercules.tsx"],"sourcesContent":["\"use client\";\n\nimport type { ReactNode } from \"react\";\nimport { ConvexProviderWithAuth, type ConvexReactClient } from \"convex/react\";\nimport { useCallback, useMemo } from \"react\";\nimport { useAuth } from \"react-oidc-context\";\nimport type { HerculesAuthProvider } from \"../react/HerculesAuthProvider\";\n\n/**\n * A wrapper React component which provides a {@link ConvexReactClient}\n * authenticated with Hercules Auth.\n *\n * It must be wrapped by a configured `{@link HerculesAuthProvider}`.\n *\n * @public\n */\nexport function ConvexProviderWithHerculesAuth({\n children,\n client,\n}: {\n children: ReactNode;\n client: ConvexReactClient;\n}) {\n return (\n <ConvexProviderWithAuth client={client} useAuth={useUseAuthFromHercules}>\n {children}\n </ConvexProviderWithAuth>\n );\n}\n\nfunction useUseAuthFromHercules() {\n const { isAuthenticated, user, isLoading, signinSilent } = useAuth();\n const idToken = user?.id_token;\n const fetchAccessToken = useCallback(\n async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => {\n try {\n if (forceRefreshToken) {\n const user = await signinSilent();\n return user?.id_token ?? null;\n }\n return idToken ?? null;\n } catch {\n return null;\n }\n },\n [idToken, signinSilent],\n );\n return useMemo(\n () => ({\n isLoading: !isLoading,\n isAuthenticated: isAuthenticated ?? false,\n fetchAccessToken,\n }),\n [isLoading, isAuthenticated, fetchAccessToken],\n );\n}\n"],"mappings":"AAGA,OAAS,0BAAAA,MAAsD,eAC/D,OAAS,eAAAC,EAAa,WAAAC,MAAe,QACrC,OAAS,WAAAC,MAAe,qBAmBpB,cAAAC,MAAA,oBARG,SAASC,EAA+B,CAC7C,SAAAC,EACA,OAAAC,CACF,EAGG,CACD,OACEH,EAACJ,EAAA,CAAuB,OAAQO,EAAQ,QAASC,EAC9C,SAAAF,EACH,CAEJ,CAEA,SAASE,GAAyB,CAChC,GAAM,CAAE,gBAAAC,EAAiB,KAAAC,EAAM,UAAAC,EAAW,aAAAC,CAAa,EAAIT,EAAQ,EAC7DU,EAAUH,GAAM,SAChBI,EAAmBb,EACvB,MAAO,CAAE,kBAAAc,CAAkB,IAAsC,CAC/D,GAAI,CACF,OAAIA,GACW,MAAMH,EAAa,IACnB,UAAY,KAEpBC,GAAW,IACpB,MAAQ,CACN,OAAO,IACT,CACF,EACA,CAACA,EAASD,CAAY,CACxB,EACA,OAAOV,EACL,KAAO,CACL,UAAW,CAACS,EACZ,gBAAiBF,GAAmB,GACpC,iBAAAK,CACF,GACA,CAACH,EAAWF,EAAiBK,CAAgB,CAC/C,CACF","names":["ConvexProviderWithAuth","useCallback","useMemo","useAuth","jsx","ConvexProviderWithHerculesAuth","children","client","useUseAuthFromHercules","isAuthenticated","user","isLoading","signinSilent","idToken","fetchAccessToken","forceRefreshToken"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var n=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var w=(o,e)=>{for(var t in e)n(o,t,{get:e[t],enumerable:!0})},g=(o,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of P(e))!v.call(o,r)&&r!==t&&n(o,r,{get:()=>e[r],enumerable:!(i=A(e,r))||i.enumerable});return o};var b=o=>g(n({},"__esModule",{value:!0}),o);var S={};w(S,{HerculesAuthProvider:()=>x,useAuth:()=>p.useAuth,useUser:()=>C});module.exports=b(S);var c=require("react-oidc-context"),l=require("react/jsx-runtime");function a(){window.history.replaceState({},document.title,window.location.pathname)}function u(){window.location.pathname=""}var k={prompt:"select_account",response_type:"code",scope:"openid profile email",redirect_uri:`${window.location.origin}/auth/callback`,onSigninCallback:a,onSignoutCallback:u};function x({children:o,config:e}){return(0,l.jsx)(c.AuthProvider,{...k,onSigninCallback:a,onSignoutCallback:u,...e,children:o})}var p=require("react-oidc-context");var s=require("react"),d=require("react-oidc-context");function C(){let{user:o,isLoading:e,error:t,isAuthenticated:i}=(0,d.useAuth)();return(0,s.useMemo)(()=>{let r=o?.profile.sub,m=o?.profile.name,f=o?.profile.email,h=o?.profile.picture;return{...o??{},id:r,name:m,email:f,avatar:h,isAuthenticated:i,isLoading:e,error:t}},[o,i,e,t])}0&&(module.exports={HerculesAuthProvider,useAuth,useUser});
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/react/index.ts","../../src/react/HerculesAuthProvider.tsx","../../src/react/use-auth.ts","../../src/react/use-user.ts"],"sourcesContent":["export * from \"./HerculesAuthProvider\";\nexport * from \"./use-auth\";\nexport * from \"./use-user\";\n","\"use client\";\n\nimport {\n AuthProvider as ReactAuthProvider,\n type AuthProviderProps,\n} from \"react-oidc-context\";\n\nfunction onSigninCallback() {\n window.history.replaceState({}, document.title, window.location.pathname);\n}\n\nfunction onSignoutCallback() {\n window.location.pathname = \"\";\n}\n\nconst DEFAULT_AUTH_CONFIG: Partial<AuthProviderProps> = {\n prompt: \"select_account\",\n response_type: \"code\",\n scope: \"openid profile email\",\n redirect_uri: `${window.location.origin}/auth/callback`,\n onSigninCallback,\n onSignoutCallback,\n};\n\n/**\n * A wrapper React component which provides a {@link ReactAuthProvider}\n * configured with Hercules Auth.\n *\n * @public\n */\nexport function HerculesAuthProvider({\n children,\n config,\n}: {\n children: React.ReactNode;\n config: AuthProviderProps;\n}) {\n return (\n <ReactAuthProvider\n {...DEFAULT_AUTH_CONFIG}\n onSigninCallback={onSigninCallback}\n onSignoutCallback={onSignoutCallback}\n {...config}\n >\n {children}\n </ReactAuthProvider>\n );\n}\n","\"use client\";\n\nimport { useAuth } from \"react-oidc-context\";\nexport { useAuth };\n","\"use client\";\n\nimport { useMemo } from \"react\";\nimport { useAuth } from \"react-oidc-context\";\n\n/**\n * A hook that returns the current user if authenticated\n *\n * @public\n */\nexport function useUser() {\n const { user, isLoading, error, isAuthenticated } = useAuth();\n\n return useMemo(() => {\n const id = user?.profile.sub;\n const name = user?.profile.name;\n const email = user?.profile.email;\n const avatar = user?.profile.picture;\n return {\n ...(user ?? {}),\n id,\n name,\n email,\n avatar,\n isAuthenticated,\n isLoading,\n error,\n };\n }, [user, isAuthenticated, isLoading, error]);\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,0BAAAE,EAAA,kCAAAC,IAAA,eAAAC,EAAAJ,GCEA,IAAAK,EAGO,8BAiCHC,EAAA,6BA/BJ,SAASC,GAAmB,CAC1B,OAAO,QAAQ,aAAa,CAAC,EAAG,SAAS,MAAO,OAAO,SAAS,QAAQ,CAC1E,CAEA,SAASC,GAAoB,CAC3B,OAAO,SAAS,SAAW,EAC7B,CAEA,IAAMC,EAAkD,CACtD,OAAQ,iBACR,cAAe,OACf,MAAO,uBACP,aAAc,GAAG,OAAO,SAAS,MAAM,iBACvC,iBAAAF,EACA,kBAAAC,CACF,EAQO,SAASE,EAAqB,CACnC,SAAAC,EACA,OAAAC,CACF,EAGG,CACD,SACE,OAAC,EAAAC,aAAA,CACE,GAAGJ,EACJ,iBAAkBF,EAClB,kBAAmBC,EAClB,GAAGI,EAEH,SAAAD,EACH,CAEJ,CC7CA,IAAAG,EAAwB,8BCAxB,IAAAC,EAAwB,iBACxBC,EAAwB,8BAOjB,SAASC,GAAU,CACxB,GAAM,CAAE,KAAAC,EAAM,UAAAC,EAAW,MAAAC,EAAO,gBAAAC,CAAgB,KAAI,WAAQ,EAE5D,SAAO,WAAQ,IAAM,CACnB,IAAMC,EAAKJ,GAAM,QAAQ,IACnBK,EAAOL,GAAM,QAAQ,KACrBM,EAAQN,GAAM,QAAQ,MACtBO,EAASP,GAAM,QAAQ,QAC7B,MAAO,CACL,GAAIA,GAAQ,CAAC,EACb,GAAAI,EACA,KAAAC,EACA,MAAAC,EACA,OAAAC,EACA,gBAAAJ,EACA,UAAAF,EACA,MAAAC,CACF,CACF,EAAG,CAACF,EAAMG,EAAiBF,EAAWC,CAAK,CAAC,CAC9C","names":["react_exports","__export","HerculesAuthProvider","useUser","__toCommonJS","import_react_oidc_context","import_jsx_runtime","onSigninCallback","onSignoutCallback","DEFAULT_AUTH_CONFIG","HerculesAuthProvider","children","config","ReactAuthProvider","import_react_oidc_context","import_react","import_react_oidc_context","useUser","user","isLoading","error","isAuthenticated","id","name","email","avatar"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as react_oidc_context from 'react-oidc-context';
|
|
3
|
+
import { AuthProviderProps } from 'react-oidc-context';
|
|
4
|
+
export { useAuth } from 'react-oidc-context';
|
|
5
|
+
import * as oidc_client_ts from 'oidc-client-ts';
|
|
6
|
+
|
|
7
|
+
declare function HerculesAuthProvider({ children, config, }: {
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
config: AuthProviderProps;
|
|
10
|
+
}): react_jsx_runtime.JSX.Element;
|
|
11
|
+
|
|
12
|
+
declare function useUser(): {
|
|
13
|
+
id: string | undefined;
|
|
14
|
+
name: string | undefined;
|
|
15
|
+
email: string | undefined;
|
|
16
|
+
avatar: string | undefined;
|
|
17
|
+
isAuthenticated: boolean;
|
|
18
|
+
isLoading: boolean;
|
|
19
|
+
error: react_oidc_context.ErrorContext | undefined;
|
|
20
|
+
id_token?: string;
|
|
21
|
+
session_state?: string | null | undefined;
|
|
22
|
+
access_token?: string | undefined;
|
|
23
|
+
refresh_token?: string;
|
|
24
|
+
token_type?: string | undefined;
|
|
25
|
+
scope?: string;
|
|
26
|
+
profile?: oidc_client_ts.IdTokenClaims | undefined;
|
|
27
|
+
expires_at?: number;
|
|
28
|
+
state?: unknown;
|
|
29
|
+
url_state?: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { HerculesAuthProvider, useUser };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as react_oidc_context from 'react-oidc-context';
|
|
3
|
+
import { AuthProviderProps } from 'react-oidc-context';
|
|
4
|
+
export { useAuth } from 'react-oidc-context';
|
|
5
|
+
import * as oidc_client_ts from 'oidc-client-ts';
|
|
6
|
+
|
|
7
|
+
declare function HerculesAuthProvider({ children, config, }: {
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
config: AuthProviderProps;
|
|
10
|
+
}): react_jsx_runtime.JSX.Element;
|
|
11
|
+
|
|
12
|
+
declare function useUser(): {
|
|
13
|
+
id: string | undefined;
|
|
14
|
+
name: string | undefined;
|
|
15
|
+
email: string | undefined;
|
|
16
|
+
avatar: string | undefined;
|
|
17
|
+
isAuthenticated: boolean;
|
|
18
|
+
isLoading: boolean;
|
|
19
|
+
error: react_oidc_context.ErrorContext | undefined;
|
|
20
|
+
id_token?: string;
|
|
21
|
+
session_state?: string | null | undefined;
|
|
22
|
+
access_token?: string | undefined;
|
|
23
|
+
refresh_token?: string;
|
|
24
|
+
token_type?: string | undefined;
|
|
25
|
+
scope?: string;
|
|
26
|
+
profile?: oidc_client_ts.IdTokenClaims | undefined;
|
|
27
|
+
expires_at?: number;
|
|
28
|
+
state?: unknown;
|
|
29
|
+
url_state?: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { HerculesAuthProvider, useUser };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{AuthProvider as p}from"react-oidc-context";import{jsx as d}from"react/jsx-runtime";function i(){window.history.replaceState({},document.title,window.location.pathname)}function n(){window.location.pathname=""}var s={prompt:"select_account",response_type:"code",scope:"openid profile email",redirect_uri:`${window.location.origin}/auth/callback`,onSigninCallback:i,onSignoutCallback:n};function A({children:o,config:e}){return d(p,{...s,onSigninCallback:i,onSignoutCallback:n,...e,children:o})}import{useAuth as g}from"react-oidc-context";import{useMemo as m}from"react";import{useAuth as f}from"react-oidc-context";function C(){let{user:o,isLoading:e,error:t,isAuthenticated:r}=f();return m(()=>{let c=o?.profile.sub,a=o?.profile.name,u=o?.profile.email,l=o?.profile.picture;return{...o??{},id:c,name:a,email:u,avatar:l,isAuthenticated:r,isLoading:e,error:t}},[o,r,e,t])}export{A as HerculesAuthProvider,g as useAuth,C as useUser};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/react/HerculesAuthProvider.tsx","../../src/react/use-auth.ts","../../src/react/use-user.ts"],"sourcesContent":["\"use client\";\n\nimport {\n AuthProvider as ReactAuthProvider,\n type AuthProviderProps,\n} from \"react-oidc-context\";\n\nfunction onSigninCallback() {\n window.history.replaceState({}, document.title, window.location.pathname);\n}\n\nfunction onSignoutCallback() {\n window.location.pathname = \"\";\n}\n\nconst DEFAULT_AUTH_CONFIG: Partial<AuthProviderProps> = {\n prompt: \"select_account\",\n response_type: \"code\",\n scope: \"openid profile email\",\n redirect_uri: `${window.location.origin}/auth/callback`,\n onSigninCallback,\n onSignoutCallback,\n};\n\n/**\n * A wrapper React component which provides a {@link ReactAuthProvider}\n * configured with Hercules Auth.\n *\n * @public\n */\nexport function HerculesAuthProvider({\n children,\n config,\n}: {\n children: React.ReactNode;\n config: AuthProviderProps;\n}) {\n return (\n <ReactAuthProvider\n {...DEFAULT_AUTH_CONFIG}\n onSigninCallback={onSigninCallback}\n onSignoutCallback={onSignoutCallback}\n {...config}\n >\n {children}\n </ReactAuthProvider>\n );\n}\n","\"use client\";\n\nimport { useAuth } from \"react-oidc-context\";\nexport { useAuth };\n","\"use client\";\n\nimport { useMemo } from \"react\";\nimport { useAuth } from \"react-oidc-context\";\n\n/**\n * A hook that returns the current user if authenticated\n *\n * @public\n */\nexport function useUser() {\n const { user, isLoading, error, isAuthenticated } = useAuth();\n\n return useMemo(() => {\n const id = user?.profile.sub;\n const name = user?.profile.name;\n const email = user?.profile.email;\n const avatar = user?.profile.picture;\n return {\n ...(user ?? {}),\n id,\n name,\n email,\n avatar,\n isAuthenticated,\n isLoading,\n error,\n };\n }, [user, isAuthenticated, isLoading, error]);\n}\n"],"mappings":"AAEA,OACE,gBAAgBA,MAEX,qBAiCH,cAAAC,MAAA,oBA/BJ,SAASC,GAAmB,CAC1B,OAAO,QAAQ,aAAa,CAAC,EAAG,SAAS,MAAO,OAAO,SAAS,QAAQ,CAC1E,CAEA,SAASC,GAAoB,CAC3B,OAAO,SAAS,SAAW,EAC7B,CAEA,IAAMC,EAAkD,CACtD,OAAQ,iBACR,cAAe,OACf,MAAO,uBACP,aAAc,GAAG,OAAO,SAAS,MAAM,iBACvC,iBAAAF,EACA,kBAAAC,CACF,EAQO,SAASE,EAAqB,CACnC,SAAAC,EACA,OAAAC,CACF,EAGG,CACD,OACEN,EAACD,EAAA,CACE,GAAGI,EACJ,iBAAkBF,EAClB,kBAAmBC,EAClB,GAAGI,EAEH,SAAAD,EACH,CAEJ,CC7CA,OAAS,WAAAE,MAAe,qBCAxB,OAAS,WAAAC,MAAe,QACxB,OAAS,WAAAC,MAAe,qBAOjB,SAASC,GAAU,CACxB,GAAM,CAAE,KAAAC,EAAM,UAAAC,EAAW,MAAAC,EAAO,gBAAAC,CAAgB,EAAIL,EAAQ,EAE5D,OAAOD,EAAQ,IAAM,CACnB,IAAMO,EAAKJ,GAAM,QAAQ,IACnBK,EAAOL,GAAM,QAAQ,KACrBM,EAAQN,GAAM,QAAQ,MACtBO,EAASP,GAAM,QAAQ,QAC7B,MAAO,CACL,GAAIA,GAAQ,CAAC,EACb,GAAAI,EACA,KAAAC,EACA,MAAAC,EACA,OAAAC,EACA,gBAAAJ,EACA,UAAAF,EACA,MAAAC,CACF,CACF,EAAG,CAACF,EAAMG,EAAiBF,EAAWC,CAAK,CAAC,CAC9C","names":["ReactAuthProvider","jsx","onSigninCallback","onSignoutCallback","DEFAULT_AUTH_CONFIG","HerculesAuthProvider","children","config","useAuth","useMemo","useAuth","useUser","user","isLoading","error","isAuthenticated","id","name","email","avatar"]}
|
package/package.json
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usehercules/auth",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.23",
|
|
4
4
|
"description": "Authentication utilities for Hercules applications using OIDC",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
7
|
-
|
|
6
|
+
"exports": {
|
|
7
|
+
"./react": {
|
|
8
|
+
"types": "./dist/react/index.d.ts",
|
|
9
|
+
"import": "./dist/react/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./convex-react": {
|
|
12
|
+
"types": "./dist/convex-react/index.d.ts",
|
|
13
|
+
"import": "./dist/convex-react/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
8
16
|
"author": "Hercules Team",
|
|
9
17
|
"license": "MIT",
|
|
10
18
|
"repository": {
|
|
@@ -22,13 +30,18 @@
|
|
|
22
30
|
"access": "public"
|
|
23
31
|
},
|
|
24
32
|
"dependencies": {
|
|
33
|
+
"convex": "^1.28.0",
|
|
25
34
|
"oidc-client-ts": "^3.3.0",
|
|
26
35
|
"react-oidc-context": "^3.3.0"
|
|
27
36
|
},
|
|
28
37
|
"peerDependencies": {
|
|
29
|
-
"react": "
|
|
38
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
30
39
|
},
|
|
31
40
|
"devDependencies": {
|
|
41
|
+
"@types/react": "^18.0.0",
|
|
42
|
+
"@types/react-dom": "^18.0.0",
|
|
43
|
+
"react": "^18.0.0",
|
|
44
|
+
"react-dom": "^18.0.0",
|
|
32
45
|
"tsup": "^8.5.0",
|
|
33
46
|
"typescript": "^5.9.3"
|
|
34
47
|
},
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { ConvexProviderWithAuth, type ConvexReactClient } from "convex/react";
|
|
5
|
+
import { useCallback, useMemo } from "react";
|
|
6
|
+
import { useAuth } from "react-oidc-context";
|
|
7
|
+
import type { HerculesAuthProvider } from "../react/HerculesAuthProvider";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A wrapper React component which provides a {@link ConvexReactClient}
|
|
11
|
+
* authenticated with Hercules Auth.
|
|
12
|
+
*
|
|
13
|
+
* It must be wrapped by a configured `{@link HerculesAuthProvider}`.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export function ConvexProviderWithHerculesAuth({
|
|
18
|
+
children,
|
|
19
|
+
client,
|
|
20
|
+
}: {
|
|
21
|
+
children: ReactNode;
|
|
22
|
+
client: ConvexReactClient;
|
|
23
|
+
}) {
|
|
24
|
+
return (
|
|
25
|
+
<ConvexProviderWithAuth client={client} useAuth={useUseAuthFromHercules}>
|
|
26
|
+
{children}
|
|
27
|
+
</ConvexProviderWithAuth>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function useUseAuthFromHercules() {
|
|
32
|
+
const { isAuthenticated, user, isLoading, signinSilent } = useAuth();
|
|
33
|
+
const idToken = user?.id_token;
|
|
34
|
+
const fetchAccessToken = useCallback(
|
|
35
|
+
async ({ forceRefreshToken }: { forceRefreshToken: boolean }) => {
|
|
36
|
+
try {
|
|
37
|
+
if (forceRefreshToken) {
|
|
38
|
+
const user = await signinSilent();
|
|
39
|
+
return user?.id_token ?? null;
|
|
40
|
+
}
|
|
41
|
+
return idToken ?? null;
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
[idToken, signinSilent],
|
|
47
|
+
);
|
|
48
|
+
return useMemo(
|
|
49
|
+
() => ({
|
|
50
|
+
isLoading: !isLoading,
|
|
51
|
+
isAuthenticated: isAuthenticated ?? false,
|
|
52
|
+
fetchAccessToken,
|
|
53
|
+
}),
|
|
54
|
+
[isLoading, isAuthenticated, fetchAccessToken],
|
|
55
|
+
);
|
|
56
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./ConvexProviderWithHercules";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
AuthProvider as ReactAuthProvider,
|
|
5
|
+
type AuthProviderProps,
|
|
6
|
+
} from "react-oidc-context";
|
|
7
|
+
|
|
8
|
+
function onSigninCallback() {
|
|
9
|
+
window.history.replaceState({}, document.title, window.location.pathname);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function onSignoutCallback() {
|
|
13
|
+
window.location.pathname = "";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const DEFAULT_AUTH_CONFIG: Partial<AuthProviderProps> = {
|
|
17
|
+
prompt: "select_account",
|
|
18
|
+
response_type: "code",
|
|
19
|
+
scope: "openid profile email",
|
|
20
|
+
redirect_uri: `${window.location.origin}/auth/callback`,
|
|
21
|
+
onSigninCallback,
|
|
22
|
+
onSignoutCallback,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A wrapper React component which provides a {@link ReactAuthProvider}
|
|
27
|
+
* configured with Hercules Auth.
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export function HerculesAuthProvider({
|
|
32
|
+
children,
|
|
33
|
+
config,
|
|
34
|
+
}: {
|
|
35
|
+
children: React.ReactNode;
|
|
36
|
+
config: AuthProviderProps;
|
|
37
|
+
}) {
|
|
38
|
+
return (
|
|
39
|
+
<ReactAuthProvider
|
|
40
|
+
{...DEFAULT_AUTH_CONFIG}
|
|
41
|
+
onSigninCallback={onSigninCallback}
|
|
42
|
+
onSignoutCallback={onSignoutCallback}
|
|
43
|
+
{...config}
|
|
44
|
+
>
|
|
45
|
+
{children}
|
|
46
|
+
</ReactAuthProvider>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { useAuth } from "react-oidc-context";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A hook that returns the current user if authenticated
|
|
8
|
+
*
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export function useUser() {
|
|
12
|
+
const { user, isLoading, error, isAuthenticated } = useAuth();
|
|
13
|
+
|
|
14
|
+
return useMemo(() => {
|
|
15
|
+
const id = user?.profile.sub;
|
|
16
|
+
const name = user?.profile.name;
|
|
17
|
+
const email = user?.profile.email;
|
|
18
|
+
const avatar = user?.profile.picture;
|
|
19
|
+
return {
|
|
20
|
+
...(user ?? {}),
|
|
21
|
+
id,
|
|
22
|
+
name,
|
|
23
|
+
email,
|
|
24
|
+
avatar,
|
|
25
|
+
isAuthenticated,
|
|
26
|
+
isLoading,
|
|
27
|
+
error,
|
|
28
|
+
};
|
|
29
|
+
}, [user, isAuthenticated, isLoading, error]);
|
|
30
|
+
}
|
package/tsup.config.ts
CHANGED
|
@@ -3,9 +3,12 @@ import { defineConfig } from "tsup";
|
|
|
3
3
|
export default defineConfig((options) => [
|
|
4
4
|
{
|
|
5
5
|
...options,
|
|
6
|
-
entry: ["src/index.ts"],
|
|
6
|
+
entry: ["src/react/index.ts", "src/convex-react/index.ts"],
|
|
7
7
|
format: ["cjs", "esm"],
|
|
8
|
+
external: ["react", "react/jsx-runtime"],
|
|
8
9
|
dts: true,
|
|
10
|
+
clean: true,
|
|
9
11
|
sourcemap: true,
|
|
12
|
+
minify: !options.watch,
|
|
10
13
|
},
|
|
11
14
|
]);
|
package/dist/index.cjs
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var index_exports = {};
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
oidc: () => oidc,
|
|
34
|
-
reactOidc: () => reactOidc
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(index_exports);
|
|
37
|
-
var oidc = __toESM(require("oidc-client-ts"), 1);
|
|
38
|
-
var reactOidc = __toESM(require("react-oidc-context"), 1);
|
|
39
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
40
|
-
0 && (module.exports = {
|
|
41
|
-
oidc,
|
|
42
|
-
reactOidc
|
|
43
|
-
});
|
|
44
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import * as oidc from \"oidc-client-ts\";\nimport * as reactOidc from \"react-oidc-context\";\n\nexport { oidc, reactOidc };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,WAAsB;AACtB,gBAA2B;","names":[]}
|
package/dist/index.d.cts
DELETED
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import * as oidc from \"oidc-client-ts\";\nimport * as reactOidc from \"react-oidc-context\";\n\nexport { oidc, reactOidc };\n"],"mappings":";AAAA,YAAY,UAAU;AACtB,YAAY,eAAe;","names":[]}
|
package/src/index.ts
DELETED