@civic/auth
Advanced tools
Comparing version 0.0.1-beta.16 to 0.0.1-beta.17
@@ -1,4 +0,5 @@ | ||
export { A as AuthStorage, C as Config, D as DisplayMode, E as Endpoints, F as ForwardedTokens, T as Tokens, U as User } from './types-Bfi0hVMZ.js'; | ||
export { U as UserContextType } from './index-Bq3T3FPG.js'; | ||
export { A as AuthStorage, C as Config, D as DisplayMode, E as Endpoints, F as ForwardedTokens, T as Tokens, U as User } from './types-HdCjGldB.js'; | ||
export { C as CookieConfig, O as OAuthTokens, T as TokensCookieConfig, U as UserStorage } from './types-b4c1koXj.js'; | ||
export { U as UserContextType } from './index-DTimUlkB.js'; | ||
import 'oslo/oauth2'; | ||
import 'oslo/jwt'; |
@@ -5,13 +5,7 @@ import * as next_dist_shared_lib_image_config from 'next/dist/shared/lib/image-config'; | ||
import { NextConfig } from 'next'; | ||
import { U as User } from './types-Bfi0hVMZ.js'; | ||
import { T as TokensCookieConfig, C as CookieConfig } from './types-b4c1koXj.js'; | ||
import { U as User } from './types-HdCjGldB.js'; | ||
import { NextRequest, NextResponse } from 'next/server.js'; | ||
import 'oslo/oauth2'; | ||
interface CookieConfig { | ||
secure?: boolean; | ||
sameSite?: "strict" | "lax" | "none"; | ||
domain?: string; | ||
path?: string; | ||
maxAge?: number; | ||
} | ||
type AuthConfigWithDefaults = { | ||
@@ -28,3 +22,3 @@ clientId: string; | ||
cookies: { | ||
tokens: CookieConfig; | ||
tokens: TokensCookieConfig; | ||
user: CookieConfig; | ||
@@ -31,0 +25,0 @@ }; |
@@ -10,3 +10,3 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _chunkN4SAZYOXjs = require('./chunk-N4SAZYOX.js'); | ||
var _chunkSEKF2WZXjs = require('./chunk-SEKF2WZX.js'); | ||
@@ -19,3 +19,3 @@ | ||
var _chunkCHY6AHA7js = require('./chunk-CHY6AHA7.js'); | ||
var _chunkRF23Q4V6js = require('./chunk-RF23Q4V6.js'); | ||
@@ -30,5 +30,5 @@ | ||
var _a; | ||
const clientStorage = new (0, _chunkN4SAZYOXjs.NextjsClientStorage)(); | ||
const userSession = new (0, _chunkCHY6AHA7js.GenericUserSession)(clientStorage); | ||
const tokens = _chunkCHY6AHA7js.retrieveTokens.call(void 0, clientStorage); | ||
const clientStorage = new (0, _chunkSEKF2WZXjs.NextjsClientStorage)(); | ||
const userSession = new (0, _chunkRF23Q4V6js.GenericUserSession)(clientStorage); | ||
const tokens = _chunkRF23Q4V6js.retrieveTokens.call(void 0, clientStorage); | ||
const user = userSession.get(); | ||
@@ -60,3 +60,3 @@ if (!user || !tokens) return null; | ||
var applyAuth = (authConfig, request) => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () { | ||
const authConfigWithDefaults = _chunkN4SAZYOXjs.resolveAuthConfig.call(void 0, authConfig); | ||
const authConfigWithDefaults = _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, authConfig); | ||
const isAuthenticated = !!request.cookies.get("id_token"); | ||
@@ -83,3 +83,3 @@ if (request.nextUrl.pathname === authConfigWithDefaults.loginUrl) { | ||
}); | ||
var authMiddleware = (authConfig = _chunkN4SAZYOXjs.defaultAuthConfig) => (request) => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () { | ||
var authMiddleware = (authConfig = _chunkSEKF2WZXjs.defaultAuthConfig) => (request) => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () { | ||
const response = yield applyAuth(authConfig, request); | ||
@@ -109,3 +109,3 @@ if (response) return response; | ||
var _cachejs = require('next/cache.js'); | ||
var logger = _chunkN4SAZYOXjs.loggers.nextjs.handlers.auth; | ||
var logger = _chunkSEKF2WZXjs.loggers.nextjs.handlers.auth; | ||
var AuthError = class extends Error { | ||
@@ -120,4 +120,4 @@ constructor(message, status = 401) { | ||
return _chunkCRTRMMJ7js.__async.call(void 0, this, null, function* () { | ||
const cookieStorage = new (0, _chunkN4SAZYOXjs.NextjsCookieStorage)(); | ||
const pkceProducer = new (0, _chunkCHY6AHA7js.GenericPublicClientPKCEProducer)(cookieStorage); | ||
const cookieStorage = new (0, _chunkSEKF2WZXjs.NextjsCookieStorage)(); | ||
const pkceProducer = new (0, _chunkRF23Q4V6js.GenericPublicClientPKCEProducer)(cookieStorage); | ||
const challenge = yield pkceProducer.getCodeChallenge(); | ||
@@ -131,7 +131,7 @@ return _serverjs.NextResponse.json({ status: "success", challenge }); | ||
const response2 = new (0, _serverjs.NextResponse)( | ||
`<html><body><span style="display:none">${_chunkCHY6AHA7js.TOKEN_EXCHANGE_TRIGGER_TEXT}</span></body></html>` | ||
`<html><body><span style="display:none">${_chunkRF23Q4V6js.TOKEN_EXCHANGE_TRIGGER_TEXT}</span></body></html>` | ||
); | ||
response2.headers.set("Content-Type", "text/html; charset=utf-8"); | ||
console.log( | ||
`handleCallback no code_verifier found, returning ${_chunkCHY6AHA7js.TOKEN_EXCHANGE_TRIGGER_TEXT}` | ||
`handleCallback no code_verifier found, returning ${_chunkRF23Q4V6js.TOKEN_EXCHANGE_TRIGGER_TEXT}` | ||
); | ||
@@ -143,7 +143,7 @@ return response2; | ||
if (!code || !state) throw new AuthError("Bad parameters", 400); | ||
const cookieStorage = new (0, _chunkN4SAZYOXjs.NextjsCookieStorage)(); | ||
const resolvedConfigs = _chunkN4SAZYOXjs.resolveAuthConfig.call(void 0, config); | ||
const callbackUrl = _chunkN4SAZYOXjs.resolveCallbackUrl.call(void 0, resolvedConfigs, request.url); | ||
const resolvedConfigs = _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, config); | ||
const cookieStorage = new (0, _chunkSEKF2WZXjs.NextjsCookieStorage)(resolvedConfigs.cookies.tokens); | ||
const callbackUrl = _chunkSEKF2WZXjs.resolveCallbackUrl.call(void 0, resolvedConfigs, request.url); | ||
try { | ||
yield _chunkCHY6AHA7js.resolveOAuthAccessCode.call(void 0, code, state, cookieStorage, _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, resolvedConfigs), { | ||
yield _chunkRF23Q4V6js.resolveOAuthAccessCode.call(void 0, code, state, cookieStorage, _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, resolvedConfigs), { | ||
redirectUrl: callbackUrl | ||
@@ -155,8 +155,8 @@ })); | ||
} | ||
const user = yield _chunkCHY6AHA7js.getUser.call(void 0, cookieStorage); | ||
const user = yield _chunkRF23Q4V6js.getUser.call(void 0, cookieStorage); | ||
if (!user) { | ||
throw new AuthError("Failed to get user info", 401); | ||
} | ||
const clientStorage = new (0, _chunkN4SAZYOXjs.NextjsClientStorage)(); | ||
const userSession = new (0, _chunkCHY6AHA7js.GenericUserSession)(clientStorage); | ||
const clientStorage = new (0, _chunkSEKF2WZXjs.NextjsClientStorage)(); | ||
const userSession = new (0, _chunkRF23Q4V6js.GenericUserSession)(clientStorage); | ||
userSession.set(user); | ||
@@ -177,3 +177,3 @@ const response = new (0, _serverjs.NextResponse)(`<html></html>`); | ||
var _a; | ||
const resolvedConfigs = _chunkN4SAZYOXjs.resolveAuthConfig.call(void 0, config); | ||
const resolvedConfigs = _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, config); | ||
const defaultRedirectPath = (_a = resolvedConfigs.loginUrl) != null ? _a : "/"; | ||
@@ -187,3 +187,3 @@ const redirectTarget = new URL(request.url).searchParams.get("redirect") || defaultRedirectPath; | ||
const response = _serverjs.NextResponse.redirect(finalRedirectUrl); | ||
_chunkN4SAZYOXjs.clearAuthCookies.call(void 0, ); | ||
_chunkSEKF2WZXjs.clearAuthCookies.call(void 0, ); | ||
try { | ||
@@ -198,3 +198,3 @@ _cachejs.revalidatePath.call(void 0, isAbsoluteRedirect ? finalRedirectUrl : redirectTarget); | ||
var handler = (authConfig = {}) => (request) => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () { | ||
const config = _chunkN4SAZYOXjs.resolveAuthConfig.call(void 0, authConfig); | ||
const config = _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, authConfig); | ||
try { | ||
@@ -219,3 +219,3 @@ const pathname = request.nextUrl.pathname; | ||
const response = _serverjs.NextResponse.json({ error: message }, { status }); | ||
_chunkN4SAZYOXjs.clearAuthCookies.call(void 0, ); | ||
_chunkSEKF2WZXjs.clearAuthCookies.call(void 0, ); | ||
return response; | ||
@@ -231,3 +231,3 @@ } | ||
exports.auth = auth; exports.authMiddleware = authMiddleware; exports.createCivicAuthPlugin = _chunkN4SAZYOXjs.createCivicAuthPlugin; exports.getUser = getUser2; exports.handler = handler; exports.withAuth = withAuth; | ||
exports.auth = auth; exports.authMiddleware = authMiddleware; exports.createCivicAuthPlugin = _chunkSEKF2WZXjs.createCivicAuthPlugin; exports.getUser = getUser2; exports.handler = handler; exports.withAuth = withAuth; | ||
//# sourceMappingURL=nextjs.js.map |
@@ -1,3 +0,4 @@ | ||
import { U as UserContextType, A as AuthContextType } from './index-Bq3T3FPG.js'; | ||
import { F as ForwardedTokens, C as Config, S as SessionData, D as DisplayMode } from './types-Bfi0hVMZ.js'; | ||
import { U as UserContextType, A as AuthContextType } from './index-DTimUlkB.js'; | ||
import { F as ForwardedTokens, C as Config, S as SessionData, b as EmptyObject, U as User, D as DisplayMode } from './types-HdCjGldB.js'; | ||
import { O as OAuthTokens } from './types-b4c1koXj.js'; | ||
import { ReactNode, RefObject, Dispatch, SetStateAction } from 'react'; | ||
@@ -31,9 +32,5 @@ import * as react_jsx_runtime from 'react/jsx-runtime'; | ||
modalIframe?: boolean; | ||
sessionData?: SessionData; | ||
}; | ||
type SessionProviderOutput = SessionData & { | ||
iframeRef: RefObject<HTMLIFrameElement> | null; | ||
setAuthResponseUrl: Dispatch<SetStateAction<string | null>>; | ||
}; | ||
type CivicAuthProviderProps = Omit<AuthProviderProps, "pkceConsumer">; | ||
@@ -47,3 +44,3 @@ declare const CivicAuthProvider: ({ children, ...props }: CivicAuthProviderProps) => react_jsx_runtime.JSX.Element; | ||
declare const useUserCookie: () => any; | ||
declare const useUserCookie: <T extends EmptyObject>() => User<T> | null; | ||
@@ -54,3 +51,3 @@ declare const useToken: () => TokenContextType; | ||
declare const useSession: () => SessionProviderOutput; | ||
declare const useSession: () => SessionData; | ||
@@ -66,2 +63,11 @@ type ConfigProviderOutput = { | ||
declare const useTokenCookie: (tokenName: OAuthTokens) => string | null; | ||
type IframeProviderOutput = { | ||
iframeRef: RefObject<HTMLIFrameElement> | null; | ||
setAuthResponseUrl: Dispatch<SetStateAction<string | null>>; | ||
}; | ||
declare const useIframe: () => IframeProviderOutput; | ||
type CivicAuthIframeContainerProps = { | ||
@@ -91,2 +97,2 @@ onClose?: () => void; | ||
export { AuthContextType, CivicAuthIframeContainer, CivicAuthProvider, type CivicAuthProviderProps, CivicNextAuthProvider, type NextCivicAuthProviderProps, NextLogOut, SignInButton, SignOutButton, type TokenContextType, UserButton, UserContextType, useAuth, useConfig, useSession, useToken, useUser, useUserCookie }; | ||
export { AuthContextType, CivicAuthIframeContainer, CivicAuthProvider, type CivicAuthProviderProps, CivicNextAuthProvider, type NextCivicAuthProviderProps, NextLogOut, SignInButton, SignOutButton, type TokenContextType, UserButton, UserContextType, useAuth, useConfig, useIframe, useSession, useToken, useTokenCookie, useUser, useUserCookie }; |
@@ -5,3 +5,3 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); | ||
var _chunkN4SAZYOXjs = require('./chunk-N4SAZYOX.js'); | ||
var _chunkSEKF2WZXjs = require('./chunk-SEKF2WZX.js'); | ||
@@ -22,3 +22,3 @@ | ||
var _chunkCHY6AHA7js = require('./chunk-CHY6AHA7.js'); | ||
var _chunkRF23Q4V6js = require('./chunk-RF23Q4V6.js'); | ||
@@ -66,4 +66,2 @@ | ||
var _jsxruntime = require('react/jsx-runtime'); | ||
@@ -74,20 +72,6 @@ var defaultSession = { | ||
accessToken: void 0, | ||
displayMode: "iframe", | ||
iframeRef: null, | ||
setAuthResponseUrl: () => { | ||
} | ||
displayMode: "iframe" | ||
}; | ||
var SessionContext = _react.createContext.call(void 0, defaultSession); | ||
var SessionProvider = ({ | ||
children, | ||
session, | ||
iframeRef, | ||
setAuthResponseUrl | ||
}) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, | ||
SessionContext.Provider, | ||
{ | ||
value: _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, session || defaultSession), { iframeRef, setAuthResponseUrl }), | ||
children | ||
} | ||
); | ||
var SessionProvider = ({ children, session }) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, SessionContext.Provider, { value: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultSession), session || {}), children }); | ||
@@ -124,3 +108,3 @@ // src/react/hooks/useSession.tsx | ||
const { forwardedTokens } = parsedJWT.payload; | ||
return forwardedTokens ? _chunkCHY6AHA7js.convertForwardedTokenFormat.call(void 0, forwardedTokens) : null; | ||
return forwardedTokens ? _chunkRF23Q4V6js.convertForwardedTokenFormat.call(void 0, forwardedTokens) : null; | ||
}, [session == null ? void 0 : session.idToken]); | ||
@@ -176,3 +160,3 @@ const value = _react.useMemo.call(void 0, | ||
} | ||
const userSession = new (0, _chunkCHY6AHA7js.GenericUserSession)(storage); | ||
const userSession = new (0, _chunkRF23Q4V6js.GenericUserSession)(storage); | ||
return userSession.get(); | ||
@@ -282,3 +266,3 @@ }); | ||
{ | ||
id: _chunkCHY6AHA7js.IFRAME_ID, | ||
id: _chunkRF23Q4V6js.IFRAME_ID, | ||
ref, | ||
@@ -338,3 +322,3 @@ className: "cac-h-[26rem] cac-w-80 cac-border-none", | ||
const config = useConfig(); | ||
const { setAuthResponseUrl, iframeRef } = useSession(); | ||
const { setAuthResponseUrl, iframeRef } = useIframe(); | ||
const processIframeUrl = _react.useCallback.call(void 0, () => { | ||
@@ -347,5 +331,5 @@ if (iframeRef && iframeRef.current && iframeRef.current.contentWindow) { | ||
const iframeBody = iframeRef.current.contentWindow.document.body.innerHTML; | ||
if (iframeBody.includes(_chunkCHY6AHA7js.TOKEN_EXCHANGE_TRIGGER_TEXT)) { | ||
if (iframeBody.includes(_chunkRF23Q4V6js.TOKEN_EXCHANGE_TRIGGER_TEXT)) { | ||
console.log( | ||
`${_chunkCHY6AHA7js.TOKEN_EXCHANGE_TRIGGER_TEXT}, calling callback URL again...` | ||
`${_chunkRF23Q4V6js.TOKEN_EXCHANGE_TRIGGER_TEXT}, calling callback URL again...` | ||
); | ||
@@ -435,2 +419,19 @@ const params = new URL(iframeUrl).searchParams; | ||
// src/react/providers/IframeProvider.tsx | ||
var defaultIframe = { | ||
iframeRef: null, | ||
setAuthResponseUrl: () => { | ||
} | ||
}; | ||
var IframeContext = _react.createContext.call(void 0, defaultIframe); | ||
var IframeProvider = ({ | ||
children, | ||
iframeRef, | ||
setAuthResponseUrl | ||
}) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, IframeContext.Provider, { value: { iframeRef, setAuthResponseUrl }, children }); | ||
// src/shared/AuthProvider.tsx | ||
@@ -448,3 +449,3 @@ | ||
function BlockDisplay({ children }) { | ||
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "cac-absolute cac-left-0 cac-top-0 cac-z-50 cac-flex cac-h-screen cac-w-screen cac-items-center cac-justify-center cac-bg-white", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "cac-absolute cac-inset-0 cac-flex cac-items-center cac-justify-center cac-bg-white", children }) }); | ||
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "cac-relative cac-left-0 cac-top-0 cac-z-50 cac-flex cac-h-screen cac-w-screen cac-items-center cac-justify-center cac-bg-white", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "cac-absolute cac-inset-0 cac-flex cac-items-center cac-justify-center cac-bg-white", children }) }); | ||
} | ||
@@ -460,3 +461,4 @@ var AuthProvider = ({ | ||
nonce, | ||
modalIframe = true | ||
modalIframe = true, | ||
sessionData: inputSessionData | ||
}) => { | ||
@@ -474,7 +476,7 @@ const [iframeUrl, setIframeUrl] = _react.useState.call(void 0, null); | ||
const iframeRef = _react.useRef.call(void 0, null); | ||
const serverTokenExchange = pkceConsumer instanceof _chunkCHY6AHA7js.ConfidentialClientPKCEConsumer; | ||
const serverTokenExchange = pkceConsumer instanceof _chunkRF23Q4V6js.ConfidentialClientPKCEConsumer; | ||
_react.useEffect.call(void 0, () => { | ||
if (typeof globalThis.window !== "undefined") { | ||
setCurrentUrl(globalThis.window.location.href); | ||
const isInIframeVal = _chunkCHY6AHA7js.isWindowInIframe.call(void 0, globalThis.window); | ||
const isInIframeVal = _chunkRF23Q4V6js.isWindowInIframe.call(void 0, globalThis.window); | ||
setIsInIframe(isInIframeVal); | ||
@@ -490,7 +492,7 @@ } | ||
if (!currentUrl) return; | ||
_chunkCHY6AHA7js.BrowserAuthenticationService.build({ | ||
_chunkRF23Q4V6js.BrowserAuthenticationService.build({ | ||
clientId, | ||
redirectUrl, | ||
oauthServer: config.oauthServer, | ||
scopes: _chunkCHY6AHA7js.DEFAULT_SCOPES, | ||
scopes: _chunkRF23Q4V6js.DEFAULT_SCOPES, | ||
displayMode | ||
@@ -516,5 +518,12 @@ }).then(setAuthService); | ||
} | ||
if (inputSessionData) { | ||
return inputSessionData; | ||
} | ||
const url = new URL( | ||
authResponseUrl ? authResponseUrl : globalThis.window.location.href || "" | ||
); | ||
const existingSessionData = yield authService.validateExistingSession(); | ||
if (existingSessionData.authenticated) { | ||
return existingSessionData; | ||
} | ||
const code = url.searchParams.get("code"); | ||
@@ -530,8 +539,8 @@ const state = url.searchParams.get("state"); | ||
yield authService.tokenExchange(code, state); | ||
const clientStorage = new (0, _chunkCHY6AHA7js.LocalStorageAdapter)(); | ||
const user = yield _chunkCHY6AHA7js.getUser.call(void 0, clientStorage); | ||
const clientStorage = new (0, _chunkRF23Q4V6js.LocalStorageAdapter)(); | ||
const user = yield _chunkRF23Q4V6js.getUser.call(void 0, clientStorage); | ||
if (!user) { | ||
throw new Error("Failed to get user info"); | ||
} | ||
const userSession = new (0, _chunkCHY6AHA7js.GenericUserSession)(clientStorage); | ||
const userSession = new (0, _chunkRF23Q4V6js.GenericUserSession)(clientStorage); | ||
userSession.set(user); | ||
@@ -548,6 +557,2 @@ onSignIn == null ? void 0 : onSignIn(); | ||
} | ||
const existingSessionData = yield authService.validateExistingSession(); | ||
if (existingSessionData.authenticated) { | ||
return existingSessionData; | ||
} | ||
return existingSessionData; | ||
@@ -585,3 +590,3 @@ }) | ||
} | ||
return browserAuthenticationInitiator || new (0, _chunkCHY6AHA7js.BrowserAuthenticationInitiator)({ | ||
return browserAuthenticationInitiator || new (0, _chunkRF23Q4V6js.BrowserAuthenticationInitiator)({ | ||
pkceConsumer, | ||
@@ -591,4 +596,4 @@ // generate and retrieve the challenge client-side | ||
redirectUrl, | ||
state: _chunkCHY6AHA7js.generateState.call(void 0, useDisplayMode), | ||
scopes: _chunkCHY6AHA7js.DEFAULT_SCOPES, | ||
state: _chunkRF23Q4V6js.generateState.call(void 0, useDisplayMode), | ||
scopes: _chunkRF23Q4V6js.DEFAULT_SCOPES, | ||
displayMode: useDisplayMode, | ||
@@ -630,7 +635,3 @@ oauthServer: config.oauthServer, | ||
); | ||
const { | ||
data: autoSignIn, | ||
isLoading: autoSignInLoading, | ||
error: autoSignInError | ||
} = _reactquery.useQuery.call(void 0, { | ||
_reactquery.useQuery.call(void 0, { | ||
queryKey: ["autoSignIn", modalIframe, redirectUrl, isAuthenticated], | ||
@@ -665,8 +666,7 @@ queryFn: () => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () { | ||
children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, | ||
SessionProvider, | ||
IframeProvider, | ||
{ | ||
session, | ||
setAuthResponseUrl, | ||
iframeRef, | ||
children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, TokenProvider, { children: [ | ||
children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, SessionProvider, { session, children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, TokenProvider, { children: [ | ||
modalIframe && !isInIframe && !(session == null ? void 0 : session.authenticated) && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, | ||
@@ -684,3 +684,3 @@ "div", | ||
), | ||
modalIframe && (isInIframe || isRedirecting || isLoading) && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, BlockDisplay, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, LoadingIcon, {}) }), | ||
modalIframe && (isInIframe || isRedirecting || isLoading && !serverTokenExchange) && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, BlockDisplay, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, LoadingIcon, {}) }), | ||
(tokenExchangeError || error) && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, BlockDisplay, { children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { children: [ | ||
@@ -691,3 +691,3 @@ "Error: ", | ||
children | ||
] }) | ||
] }) }) | ||
} | ||
@@ -709,4 +709,4 @@ ) | ||
_chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, props), { | ||
pkceConsumer: new (0, _chunkCHY6AHA7js.BrowserPublicClientPKCEProducer)(), | ||
children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, UserProvider, { storage: new (0, _chunkCHY6AHA7js.LocalStorageAdapter)(), children }) | ||
pkceConsumer: new (0, _chunkRF23Q4V6js.BrowserPublicClientPKCEProducer)(), | ||
children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, UserProvider, { storage: new (0, _chunkRF23Q4V6js.LocalStorageAdapter)(), children }) | ||
}) | ||
@@ -721,2 +721,64 @@ ) }); | ||
// src/react/hooks/useTokenCookie.ts | ||
var _navigationjs = require('next/navigation.js'); | ||
// src/lib/cookies.ts | ||
var getWindowCookieValue = ({ | ||
key, | ||
window: window2, | ||
parseJson = false | ||
}) => { | ||
const cookie = window2.document.cookie; | ||
if (!cookie) return null; | ||
const cookies = cookie.split(";"); | ||
for (const c of cookies) { | ||
const [name, value] = c.trim().split("="); | ||
if (value && name === key) { | ||
try { | ||
const decodeURIComponentValue = decodeURIComponent(value); | ||
return parseJson === true ? JSON.parse(decodeURIComponentValue) : decodeURIComponentValue; | ||
} catch (e) { | ||
return value; | ||
} | ||
} | ||
} | ||
return null; | ||
}; | ||
// src/react/hooks/useTokenCookie.ts | ||
var getTokenFromCookie = (tokenName) => { | ||
return getWindowCookieValue({ | ||
key: tokenName, | ||
window: globalThis.window, | ||
parseJson: false | ||
}); | ||
}; | ||
var useTokenCookie = (tokenName) => { | ||
const hasRunRef = _react.useRef.call(void 0, false); | ||
const router = _navigationjs.useRouter.call(void 0, ); | ||
const { data: token } = _reactquery.useQuery.call(void 0, { | ||
queryKey: ["token", tokenName], | ||
queryFn: () => getTokenFromCookie(tokenName) || null, | ||
refetchInterval: 2e3, | ||
refetchIntervalInBackground: true, | ||
enabled: !hasRunRef.current, | ||
refetchOnWindowFocus: true | ||
}); | ||
_react.useEffect.call(void 0, () => { | ||
if (token) { | ||
if (!hasRunRef.current) { | ||
hasRunRef.current = true; | ||
router.refresh(); | ||
} | ||
} else { | ||
hasRunRef.current = false; | ||
} | ||
}, [token, router]); | ||
return token != null ? token : null; | ||
}; | ||
// src/react/providers/NextAuthProvider.tsx | ||
var queryClient2 = new (0, _reactquery.QueryClient)(); | ||
@@ -730,14 +792,19 @@ var CivicNextAuthProvider = (_a) => { | ||
const [redirectUrl, setRedirectUrl] = _react.useState.call(void 0, ""); | ||
const { clientId, oauthServer, callbackUrl, challengeUrl, logoutUrl } = _chunkN4SAZYOXjs.resolveAuthConfig.call(void 0, ); | ||
const { clientId, oauthServer, callbackUrl, challengeUrl, logoutUrl } = _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, ); | ||
_react.useEffect.call(void 0, () => { | ||
if (typeof globalThis.window !== "undefined") { | ||
const currentUrl = globalThis.window.location.href; | ||
setRedirectUrl(_chunkN4SAZYOXjs.resolveCallbackUrl.call(void 0, _chunkN4SAZYOXjs.resolveAuthConfig.call(void 0, ), currentUrl)); | ||
setRedirectUrl(_chunkSEKF2WZXjs.resolveCallbackUrl.call(void 0, _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, ), currentUrl)); | ||
} | ||
}, [callbackUrl]); | ||
const user = useUserCookie(); | ||
const idToken = useTokenCookie("id_token" /* ID_TOKEN */); | ||
const combinedUser = user ? _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, user || {}), { idToken }) : null; | ||
const sessionData = _chunkCRTRMMJ7js.__spreadValues.call(void 0, { | ||
authenticated: !!user | ||
}, idToken ? { idToken } : {}); | ||
const signOut = () => _chunkCRTRMMJ7js.__async.call(void 0, void 0, null, function* () { | ||
var _a2; | ||
yield fetch(logoutUrl); | ||
(_a2 = props.onSignOut) == null ? void 0 : _a2.call(props); | ||
window.location.href = logoutUrl; | ||
return; | ||
@@ -751,8 +818,9 @@ }); | ||
clientId, | ||
pkceConsumer: new (0, _chunkCHY6AHA7js.ConfidentialClientPKCEConsumer)(challengeUrl), | ||
pkceConsumer: new (0, _chunkRF23Q4V6js.ConfidentialClientPKCEConsumer)(challengeUrl), | ||
sessionData, | ||
children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, | ||
UserProvider, | ||
{ | ||
storage: new (0, _chunkN4SAZYOXjs.NextjsClientStorage)(), | ||
user, | ||
storage: new (0, _chunkSEKF2WZXjs.NextjsClientStorage)(), | ||
user: combinedUser, | ||
signOut, | ||
@@ -777,27 +845,10 @@ children | ||
var _navigationjs = require('next/navigation.js'); | ||
// src/lib/cookies.ts | ||
var getCookieValue = (key, window2) => { | ||
const cookie = window2.document.cookie; | ||
if (!cookie) return null; | ||
const cookies = cookie.split(";"); | ||
for (const c of cookies) { | ||
const [name, value] = c.trim().split("="); | ||
if (value && name === key) { | ||
try { | ||
return JSON.parse(decodeURIComponent(value)); | ||
} catch (e) { | ||
console.log("Error parsing cookie value", e); | ||
return value; | ||
} | ||
} | ||
} | ||
return null; | ||
}; | ||
// src/react/hooks/useUserCookie.ts | ||
var getUserFromCookie = () => { | ||
const userCookie = getCookieValue("user", globalThis.window); | ||
const userCookie = getWindowCookieValue({ | ||
key: "user" /* USER */, | ||
window: globalThis.window, | ||
parseJson: true | ||
}); | ||
return userCookie; | ||
@@ -826,3 +877,3 @@ }; | ||
}, [user, router]); | ||
return user; | ||
return user != null ? user : null; | ||
}; | ||
@@ -840,2 +891,12 @@ | ||
// src/react/hooks/useIframe.tsx | ||
var useIframe = () => { | ||
const context = _react.useContext.call(void 0, IframeContext); | ||
if (!context) { | ||
throw new Error("useIframe must be used within an IframeProvider"); | ||
} | ||
return context; | ||
}; | ||
// src/react/components/UserButton.tsx | ||
@@ -916,3 +977,3 @@ | ||
{ | ||
className: _chunkCHY6AHA7js.cn.call(void 0, | ||
className: _chunkRF23Q4V6js.cn.call(void 0, | ||
"cac-flex cac-w-full cac-items-center cac-justify-between cac-gap-2 cac-rounded-full cac-border cac-border-neutral-500 cac-px-3 cac-py-2 cac-text-neutral-500 cac-transition-colors hover:cac-bg-neutral-200 hover:cac-bg-opacity-50", | ||
@@ -956,3 +1017,3 @@ className | ||
"data-testid": "sign-in-button", | ||
className: _chunkCHY6AHA7js.cn.call(void 0, | ||
className: _chunkRF23Q4V6js.cn.call(void 0, | ||
"cac-rounded-full cac-border cac-border-neutral-500 cac-px-3 cac-py-2 cac-transition-colors hover:cac-bg-neutral-200 hover:cac-bg-opacity-50", | ||
@@ -978,3 +1039,3 @@ className | ||
"data-testid": "sign-in-button", | ||
className: _chunkCHY6AHA7js.cn.call(void 0, | ||
className: _chunkRF23Q4V6js.cn.call(void 0, | ||
"cac-rounded-full cac-border cac-border-neutral-500 cac-px-3 cac-py-2 cac-transition-colors hover:cac-bg-neutral-200 hover:cac-bg-opacity-50", | ||
@@ -996,3 +1057,3 @@ className | ||
{ | ||
className: _chunkCHY6AHA7js.cn.call(void 0, | ||
className: _chunkRF23Q4V6js.cn.call(void 0, | ||
"cac-rounded-full cac-border cac-border-neutral-500 cac-px-3 cac-py-2 cac-transition-colors hover:cac-bg-neutral-200 hover:cac-bg-opacity-50", | ||
@@ -1010,3 +1071,3 @@ className | ||
var NextLogOut = ({ children }) => { | ||
const config = _chunkN4SAZYOXjs.resolveAuthConfig.call(void 0, ); | ||
const config = _chunkSEKF2WZXjs.resolveAuthConfig.call(void 0, ); | ||
const logoutUrl = `${config.logoutUrl}`; | ||
@@ -1029,3 +1090,5 @@ return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "a", { href: logoutUrl, children }); | ||
exports.CivicAuthIframeContainer = CivicAuthIframeContainer; exports.CivicAuthProvider = CivicAuthProvider; exports.CivicNextAuthProvider = CivicNextAuthProvider; exports.NextLogOut = NextLogOut; exports.SignInButton = SignInButton; exports.SignOutButton = SignOutButton; exports.UserButton = UserButton; exports.useAuth = useAuth; exports.useConfig = useConfig; exports.useSession = useSession; exports.useToken = useToken; exports.useUser = useUser; exports.useUserCookie = useUserCookie; | ||
exports.CivicAuthIframeContainer = CivicAuthIframeContainer; exports.CivicAuthProvider = CivicAuthProvider; exports.CivicNextAuthProvider = CivicNextAuthProvider; exports.NextLogOut = NextLogOut; exports.SignInButton = SignInButton; exports.SignOutButton = SignOutButton; exports.UserButton = UserButton; exports.useAuth = useAuth; exports.useConfig = useConfig; exports.useIframe = useIframe; exports.useSession = useSession; exports.useToken = useToken; exports.useTokenCookie = useTokenCookie; exports.useUser = useUser; exports.useUserCookie = useUserCookie; | ||
//# sourceMappingURL=react.js.map |
@@ -1,2 +0,2 @@ | ||
import { S as SessionData, U as User, a as UnknownObject, A as AuthStorage, E as Endpoints, O as OIDCTokenResponseBody } from './types-Bfi0hVMZ.js'; | ||
import { S as SessionData, U as User, a as UnknownObject, A as AuthStorage, E as Endpoints, O as OIDCTokenResponseBody } from './types-HdCjGldB.js'; | ||
import 'oslo/oauth2'; | ||
@@ -3,0 +3,0 @@ |
@@ -8,3 +8,3 @@ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); | ||
var _chunkCHY6AHA7js = require('./chunk-CHY6AHA7.js'); | ||
var _chunkRF23Q4V6js = require('./chunk-RF23Q4V6.js'); | ||
require('./chunk-CRTRMMJ7.js'); | ||
@@ -18,3 +18,3 @@ | ||
exports.CookieStorage = _chunkCHY6AHA7js.CookieStorage; exports.buildLoginUrl = _chunkCHY6AHA7js.buildLoginUrl; exports.getUser = _chunkCHY6AHA7js.getUser; exports.isLoggedIn = _chunkCHY6AHA7js.isLoggedIn; exports.refreshTokens = _chunkCHY6AHA7js.refreshTokens; exports.resolveOAuthAccessCode = _chunkCHY6AHA7js.resolveOAuthAccessCode; | ||
exports.CookieStorage = _chunkRF23Q4V6js.CookieStorage; exports.buildLoginUrl = _chunkRF23Q4V6js.buildLoginUrl; exports.getUser = _chunkRF23Q4V6js.getUser; exports.isLoggedIn = _chunkRF23Q4V6js.isLoggedIn; exports.refreshTokens = _chunkRF23Q4V6js.refreshTokens; exports.resolveOAuthAccessCode = _chunkRF23Q4V6js.resolveOAuthAccessCode; | ||
//# sourceMappingURL=server.js.map |
{ | ||
"name": "@civic/auth", | ||
"version": "0.0.1-beta.16", | ||
"version": "0.0.1-beta.17", | ||
"files": [ | ||
@@ -5,0 +5,0 @@ "dist" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
489709
46
5028
8