1
0
mirror of synced 2026-02-03 18:01:02 -05:00

Compare commits

...

10 Commits

Author SHA1 Message Date
Fran Zekan
f60f96f5d1 Fix missing redirectAuthenticatedTo 2022-07-30 20:36:33 +02:00
Fran Zekan
3a98d4bd18 Remove redirectAuthenticatedTo from global pageDefaults 2022-07-30 20:35:56 +02:00
Aleksandra
6c3811572e Merge branch 'main' into feature/global-auth-page-definitions 2022-07-25 15:19:43 +02:00
Fran Zekan
b8e73fba43 Update packages/blitz-auth/src/client/index.tsx
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-07-25 12:56:48 +02:00
Fran Zekan
c3e57f392e Update packages/blitz-auth/src/client/index.tsx
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-07-25 12:56:43 +02:00
Fran Zekan
3da5e7bcd3 Merge branch 'main' into feature/global-auth-page-definitions 2022-07-22 22:19:13 +02:00
Fran Zekan
a0b2f80568 Merge branch 'main' into feature/global-auth-page-definitions 2022-07-22 19:14:18 +02:00
Fran Zekan
cbafe1f1a5 Rename defaults -> pageDefaults 2022-07-22 19:14:00 +02:00
Dillon Raphael
7297c33bc0 Create sour-rats-exist.md 2022-07-21 22:12:53 +02:00
Fran Zekan
c702bb0ddd Add default auth options to authPlugin 2022-07-16 02:20:53 +02:00
2 changed files with 68 additions and 50 deletions

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/auth": patch
"blitz": patch
---
Add default auth options to authPlugin

View File

@@ -245,79 +245,91 @@ export function getAuthValues<TProps = any>(
return {authenticate, redirectAuthenticatedTo}
}
function withBlitzAuthPlugin<TProps = any>(Page: ComponentType<TProps> | BlitzPage<TProps>) {
const AuthRoot = (props: ComponentProps<any>) => {
useSession({suspense: false})
interface AuthPluginDefaultOptions {
authenticate?: boolean | {redirectTo?: string | RouteUrlObject}
suppressFirstRenderFlicker?: boolean
}
let {authenticate, redirectAuthenticatedTo} = getAuthValues(Page, props)
function createBlitzAuthPlugin(pageDefaults?: AuthPluginDefaultOptions) {
return function <TProps = any>(Page: ComponentType<TProps> | BlitzPage<TProps>) {
const AuthRoot = (props: ComponentProps<any>) => {
useSession({suspense: false})
useAuthorizeIf(authenticate === true)
let {authenticate, redirectAuthenticatedTo} = getAuthValues(Page, props)
if (authenticate === undefined) {
authenticate = pageDefaults?.authenticate
}
if (typeof window !== "undefined") {
const publicData = getPublicDataStore().getData()
useAuthorizeIf(authenticate === true)
// We read directly from publicData.userId instead of useSession
// so we can access userId on first render. useSession is always empty on first render
if (publicData.userId) {
debug("[BlitzAuthInnerRoot] logged in")
if (typeof window !== "undefined") {
const publicData = getPublicDataStore().getData()
if (typeof redirectAuthenticatedTo === "function") {
redirectAuthenticatedTo = redirectAuthenticatedTo({
session: publicData,
})
}
// We read directly from publicData.userId instead of useSession
// so we can access userId on first render. useSession is always empty on first render
if (publicData.userId) {
debug("[BlitzAuthInnerRoot] logged in")
if (redirectAuthenticatedTo) {
const redirectUrl =
typeof redirectAuthenticatedTo === "string"
? redirectAuthenticatedTo
: formatWithValidation(redirectAuthenticatedTo)
debug("[BlitzAuthInnerRoot] redirecting to", redirectUrl)
const error = new RedirectError(redirectUrl)
error.stack = null!
throw error
}
} else {
debug("[BlitzAuthInnerRoot] logged out")
if (authenticate && typeof authenticate === "object" && authenticate.redirectTo) {
let {redirectTo} = authenticate
if (typeof redirectTo !== "string") {
redirectTo = formatWithValidation(redirectTo)
if (typeof redirectAuthenticatedTo === "function") {
redirectAuthenticatedTo = redirectAuthenticatedTo({
session: publicData,
})
}
const url = new URL(redirectTo, window.location.href)
url.searchParams.append("next", window.location.pathname)
debug("[BlitzAuthInnerRoot] redirecting to", url.toString())
const error = new RedirectError(url.toString())
error.stack = null!
throw error
if (redirectAuthenticatedTo) {
const redirectUrl =
typeof redirectAuthenticatedTo === "string"
? redirectAuthenticatedTo
: formatWithValidation(redirectAuthenticatedTo)
debug("[BlitzAuthInnerRoot] redirecting to", redirectUrl)
const error = new RedirectError(redirectUrl)
error.stack = null!
throw error
}
} else {
debug("[BlitzAuthInnerRoot] logged out")
if (authenticate && typeof authenticate === "object" && authenticate.redirectTo) {
let {redirectTo} = authenticate
if (typeof redirectTo !== "string") {
redirectTo = formatWithValidation(redirectTo)
}
const url = new URL(redirectTo, window.location.href)
url.searchParams.append("next", window.location.pathname)
debug("[BlitzAuthInnerRoot] redirecting to", url.toString())
const error = new RedirectError(url.toString())
error.stack = null!
throw error
}
}
}
return <Page {...props} />
}
return <Page {...props} />
}
for (let [key, value] of Object.entries(Page)) {
// @ts-ignore
AuthRoot[key] = value
}
if (process.env.NODE_ENV !== "production") {
AuthRoot.displayName = `BlitzAuthInnerRoot`
}
for (let [key, value] of Object.entries(Page)) {
// @ts-ignore
AuthRoot[key] = value
return AuthRoot
}
if (process.env.NODE_ENV !== "production") {
AuthRoot.displayName = `BlitzAuthInnerRoot`
}
return AuthRoot
}
export interface AuthPluginClientOptions {
cookiePrefix: string
pageDefaults?: AuthPluginDefaultOptions
}
export const AuthClientPlugin = createClientPlugin((options: AuthPluginClientOptions) => {
globalThis.__BLITZ_SESSION_COOKIE_PREFIX = options.cookiePrefix || "blitz"
return {
withProvider: withBlitzAuthPlugin,
withProvider: createBlitzAuthPlugin(options.pageDefaults),
events: {},
middleware: {},
exports: () => ({