Skip to content

Commit

Permalink
Updating example with dummy db call
Browse files Browse the repository at this point in the history
  • Loading branch information
jayair committed Dec 16, 2024
1 parent c37f913 commit d7ade2d
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 8 deletions.
Binary file modified bun.lockb
Binary file not shown.
8 changes: 7 additions & 1 deletion examples/authorizer/bun/authorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { PasswordAdapter } from "@openauthjs/openauth/adapter/password"
import { PasswordUI } from "@openauthjs/openauth/ui/password"
import { subjects } from "../../subjects.js"

async function getUser(email: string) {
// Get user from database
// Return user ID
return "123"
}

export default authorizer({
subjects,
storage: MemoryStorage({
Expand All @@ -21,7 +27,7 @@ export default authorizer({
success: async (ctx, value) => {
if (value.provider === "password") {
return ctx.subject("user", {
email: value.email,
id: await getUser(value.email),
})
}
throw new Error("Invalid provider")
Expand Down
8 changes: 7 additions & 1 deletion examples/authorizer/cloudflare/authorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ interface Env {
CloudflareAuthKV: KVNamespace
}

async function getUser(email: string) {
// Get user from database
// Return user ID
return "123"
}

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
return authorizer({
Expand All @@ -31,7 +37,7 @@ export default {
success: async (ctx, value) => {
if (value.provider === "password") {
return ctx.subject("user", {
email: value.email,
id: await getUser(value.email),
})
}
throw new Error("Invalid provider")
Expand Down
8 changes: 7 additions & 1 deletion examples/authorizer/custom-frontend/auth/authorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { MemoryStorage } from "@openauthjs/openauth/storage/memory"
import { CodeAdapter } from "@openauthjs/openauth/adapter/code"
import { subjects } from "../../../subjects.js"

async function getUser(email: string) {
// Get user from database
// Return user ID
return "123"
}

export default authorizer({
subjects,
storage: MemoryStorage({
Expand All @@ -29,7 +35,7 @@ export default authorizer({
success: async (ctx, value) => {
if (value.provider === "code") {
return ctx.subject("user", {
email: value.claims.email,
id: await getUser(value.claims.email),
})
}
throw new Error("Invalid provider")
Expand Down
8 changes: 7 additions & 1 deletion examples/authorizer/lambda/authorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { Resource } from "sst"
import { PasswordAdapter } from "@openauthjs/openauth/adapter/password"
import { PasswordUI } from "@openauthjs/openauth/ui/password"

async function getUser(email: string) {
// Get user from database
// Return user ID
return "123"
}

const app = authorizer({
storage: DynamoStorage({
table: Resource.LambdaAuthTable.name,
Expand All @@ -23,7 +29,7 @@ const app = authorizer({
success: async (ctx, value) => {
if (value.provider === "password") {
return ctx.subject("user", {
email: value.email,
id: await getUser(value.email),
})
}
throw new Error("Invalid provider")
Expand Down
8 changes: 7 additions & 1 deletion examples/authorizer/node/authorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { PasswordUI } from "@openauthjs/openauth/ui/password"
import { serve } from "@hono/node-server"
import { subjects } from "../../subjects"

async function getUser(email: string) {
// Get user from database
// Return user ID
return "123"
}

const app = authorizer({
subjects,
storage: MemoryStorage({
Expand All @@ -22,7 +28,7 @@ const app = authorizer({
success: async (ctx, value) => {
if (value.provider === "password") {
return ctx.subject("user", {
email: value.email,
id: await getUser(value.email),
})
}
throw new Error("Invalid provider")
Expand Down
2 changes: 1 addition & 1 deletion examples/client/astro/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import Layout from '../layouts/Layout.astro';
---

<Layout>
Hello {Astro.locals.subject?.properties.email}
Hello {Astro.locals.subject?.properties.id}
</Layout>
2 changes: 1 addition & 1 deletion examples/client/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default async function Home() {
{subject ? (
<>
<li>
Logged in as <code>{subject.properties.email}</code>.
Logged in as <code>{subject.properties.id}</code>.
</li>
<li>
And then check out <code>app/page.tsx</code>.
Expand Down
2 changes: 1 addition & 1 deletion examples/subjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { createSubjects } from "@openauthjs/openauth"

export const subjects = createSubjects({
user: object({
email: string(),
id: string(),
}),
})

0 comments on commit d7ade2d

Please sign in to comment.