Skip to content

Commit

Permalink
remove authorizedContext: To be done in another PR
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhsuresh committed Mar 18, 2023
1 parent 13f6faf commit 46fe8af
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 70 deletions.
6 changes: 1 addition & 5 deletions apps/next13/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import Link from "next/link"
import styles from "src/styles/Home.module.css"
import Test from "./react-query"
import {getBlitzContext, useAuthenticatedBlitzContext} from "../src/blitz-server"
import {getBlitzContext} from "../src/blitz-server"
import getCurrentUser from "../src/users/queries/getCurrentUser"

export default async function Home() {
// await useAuthenticatedBlitzContext({
// redirectTo: "/auth/login",
// // redirectAuthenticatedTo: "/dashboard",
// })
const ctx = await getBlitzContext()
ctx.session.$create({userId: 1})
console.log("session", ctx.session.userId)
Expand Down
4 changes: 2 additions & 2 deletions apps/next13/src/blitz-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import db from "../prisma"
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
import {BlitzLogger} from "blitz"

const {api, getBlitzContext, useAuthenticatedBlitzContext} = setupBlitzServer({
const {api, getBlitzContext} = setupBlitzServer({
plugins: [
AuthServerPlugin({
cookiePrefix: "web-cookie-prefix",
Expand All @@ -16,7 +16,7 @@ const {api, getBlitzContext, useAuthenticatedBlitzContext} = setupBlitzServer({
logger: BlitzLogger({}),
})

export {api, getBlitzContext, useAuthenticatedBlitzContext}
export {api, getBlitzContext}

export const cliConfig: BlitzCliConfig = {
customTemplates: "src/templates",
Expand Down
3 changes: 1 addition & 2 deletions packages/blitz-auth/src/server/auth-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {BlitzServerPlugin, RequestMiddleware, Ctx, createServerPlugin} from "bli
import {assert} from "blitz"
import {IncomingMessage, ServerResponse} from "http"
import {PublicData, SessionModel, SessionConfigMethods} from "../shared/types"
import {getBlitzContext, getSession, useAuthenticatedBlitzContext} from "./auth-sessions"
import {getBlitzContext, getSession} from "./auth-sessions"

interface SessionConfigOptions {
cookiePrefix?: string
Expand Down Expand Up @@ -129,7 +129,6 @@ export const AuthServerPlugin = createServerPlugin((options: AuthPluginOptions)
requestMiddlewares: [authPluginSessionMiddleware()],
exports: () => ({
getBlitzContext,
useAuthenticatedBlitzContext,
}),
}
})
63 changes: 5 additions & 58 deletions packages/blitz-auth/src/server/auth-sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,59 +209,6 @@ interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query" | "href">
pathname: string
}

export async function useAuthenticatedBlitzContext({
redirectTo,
redirectAuthenticatedTo,
role,
}: {
redirectTo?: string | RouteUrlObject
redirectAuthenticatedTo?: string | RouteUrlObject | ((ctx: Ctx) => string | RouteUrlObject)
role?: string | string[]
}): Promise<void> {
const ctx: Ctx = await getBlitzContext()
const userId = ctx.session.userId
const {redirect} = await import("next/navigation").catch(() => {
throw new Error(
"useAuthenticatedBlitzContext() can only be used in React Server Components in Nextjs 13 or higher",
)
})
if (userId) {
debug("[useAuthenticatedBlitzContext] User is authenticated")
if (redirectAuthenticatedTo) {
if (typeof redirectAuthenticatedTo === "function") {
redirectAuthenticatedTo = redirectAuthenticatedTo(ctx)
}
const redirectUrl =
typeof redirectAuthenticatedTo === "string"
? redirectAuthenticatedTo
: formatWithValidation(redirectAuthenticatedTo)
redirect(redirectUrl)
}
if (redirectTo && role) {
debug("[useAuthenticatedBlitzContext] redirectTo and role are both defined.")
try {
ctx.session.$authorize(role)
} catch (e) {
log.error("Authorization Error: " + (e as Error).message)
if (typeof redirectTo !== "string") {
redirectTo = formatWithValidation(redirectTo)
}
redirect(redirectTo)
}
}
} else {
debug("[useAuthenticatedBlitzContext] User is not authenticated")
if (redirectTo) {
if (typeof redirectTo !== "string") {
redirectTo = formatWithValidation(redirectTo)
}
redirect(redirectTo)
} else {
redirect("/")
}
}
}

const makeProxyToPublicData = <T extends SessionContextClass>(ctxClass: T): T => {
return new Proxy(ctxClass, {
get(target, prop, receiver) {
Expand Down Expand Up @@ -334,7 +281,7 @@ export class SessionContextClass implements SessionContext {

async $create(publicData: PublicData, privateData?: Record<any, any>) {
if (this._appDir) {
unSupportedMessage("$create")
unSupportedMessage("$create").finally(() => {})
return
}
this._kernel = await createNewSession({
Expand All @@ -349,15 +296,15 @@ export class SessionContextClass implements SessionContext {

async $revoke() {
if (this._appDir) {
unSupportedMessage("$revoke")
unSupportedMessage("$revoke").finally(() => {})
return
}
this._kernel = await revokeSession(this._req, this._res, this.$handle)
}

async $revokeAll() {
if (this._appDir) {
unSupportedMessage("$revokeAll")
unSupportedMessage("$revokeAll").finally(() => {})
return
}
// revoke the current session which uses req/res
Expand All @@ -369,7 +316,7 @@ export class SessionContextClass implements SessionContext {

async $setPublicData(data: Record<any, any>) {
if (this._appDir) {
unSupportedMessage("$setPublicData")
unSupportedMessage("$setPublicData").finally(() => {})
return
}
if (this.userId) {
Expand All @@ -383,7 +330,7 @@ export class SessionContextClass implements SessionContext {
}
$setPrivateData(data: Record<any, any>) {
if (this._appDir) {
unSupportedMessage("$setPrivateData")
unSupportedMessage("$setPrivateData").finally(() => {})
return Promise.resolve()
}
return setPrivateData(this._kernel, data)
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46fe8af

Please sign in to comment.