Skip to content

Commit

Permalink
fix: fix invitation when disable invitation
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Nov 13, 2024
1 parent 87342e2 commit d52fc6e
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ COPY . .

RUN bun install

EXPOSE 5173
EXPOSE 3721

ENTRYPOINT ["/tini", "--"]
CMD ["bun", "run", "dev"]
4 changes: 2 additions & 2 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"build:docker": "bun build --compile src/index.ts --target=bun --packages=external --sourcemap --outfile undb"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.686.0",
"@aws-sdk/s3-request-presigner": "^3.686.0",
"@aws-sdk/client-s3": "^3.689.0",
"@aws-sdk/s3-request-presigner": "^3.689.0",
"@elysiajs/cors": "1.1.0",
"@elysiajs/cron": "1.1.0",
"@elysiajs/html": "1.1.0",
Expand Down
48 changes: 22 additions & 26 deletions apps/backend/src/modules/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Context, Elysia, t } from "elysia"
import type { Session, User } from "lucia"
import { Lucia, generateIdFromEntropySize } from "lucia"
import { TimeSpan, createDate, isWithinExpirationDate } from "oslo"
import { Cookie } from "oslo/cookie"
import { alphabet, generateRandomString, sha256 } from "oslo/crypto"
import { encodeHex } from "oslo/encoding"
import { omit } from "radash"
Expand Down Expand Up @@ -182,8 +183,10 @@ export class Auth {
if (env.UNDB_DISABLE_REGISTRATION) {
this.logger.info("Registration is disabled")
if (!adminEmail || !adminPassword) {
this.logger.fatal("Registration is disabled but admin user is not set")
throw new Error("Registration is disabled but admin user is not set")
const message =
"Registration is disabled but admin user is not set, please set UNDB_ADMIN_EMAIL and UNDB_ADMIN_PASSWORD environment variables"
this.logger.fatal(message)
throw new Error(message)
}

const user = await this.queryBuilder
Expand Down Expand Up @@ -596,14 +599,12 @@ export class Auth {
"Invitation should exist",
)

const spaceId = invitation.spaceId

const cookieHeader = ctx.request.headers.get("Cookie") ?? ""
const sessionId = this.lucia.readSessionCookie(cookieHeader)
const { spaceId, email, role } = invitation

function redirectToSignupOrLogin(
path: "signup" | "login" = "signup",
email: string | undefined = invitation.email,
cookie?: Cookie,
) {
const search = new URLSearchParams()
search.set("invitationId", invitationId)
Expand All @@ -612,31 +613,26 @@ export class Auth {
}

const response = ctx.redirect(`/${path}?${search.toString()}`, 301)
console.log(response)
if (cookie) {
response.headers.set("Set-Cookie", cookie.serialize())
}
return response
}

if (!sessionId) {
return redirectToSignupOrLogin()
}

const { user, session: validatedSession } = await this.lucia.validateSession(sessionId)
const user = await this.queryBuilder
.selectFrom("undb_user")
.selectAll()
.where("email", "=", email)
.executeTakeFirst()
if (!user) {
return redirectToSignupOrLogin()
}
if (invitation.email !== user.email) {
return redirectToSignupOrLogin("login")
return redirectToSignupOrLogin("signup", email)
} else {
await this.spaceMemberService.createMember(user.id, spaceId, role)
const session = await this.lucia.createSession(user.id, { space_id: spaceId })
const sessionCookie = this.lucia.createSessionCookie(session.id)
return redirectToSignupOrLogin("login", email, sessionCookie)
}

await this.spaceMemberService.createMember(user.id, spaceId, invitation.role)
const session = await this.lucia.createSession(user.id, { space_id: spaceId })
const sessionCookie = this.lucia.createSessionCookie(session.id)
return new Response(null, {
status: 302,
headers: {
Location: "/",
"Set-Cookie": sessionCookie.serialize(),
},
})
})
},
{
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"tauri": "tauri"
},
"dependencies": {
"@tauri-apps/api": "^2.0.3"
"@tauri-apps/api": "^2.1.1"
},
"devDependencies": {
"@tauri-apps/cli": "^1.6.3",
"typescript": "^5.6.3",
"vite": "^5.4.10"
"vite": "^5.4.11"
}
}
14 changes: 7 additions & 7 deletions apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
"@sveltejs/kit": "^2.8.0",
"@sveltejs/vite-plugin-svelte": "^3.1.2",
"@tailwindcss/typography": "^0.5.15",
"@tanstack/eslint-plugin-query": "^5.59.20",
"@tanstack/eslint-plugin-query": "^5.60.1",
"@types/eslint": "^8.56.12",
"@types/lodash.unzip": "^3.4.9",
"@types/papaparse": "^5.3.15",
"@types/sortablejs": "latest",
"@typescript-eslint/eslint-plugin": "^8.13.0",
"@typescript-eslint/parser": "^8.13.0",
"@typescript-eslint/eslint-plugin": "^8.14.0",
"@typescript-eslint/parser": "^8.14.0",
"@undb/commands": "workspace:*",
"@undb/domain": "workspace:*",
"@undb/formula": "workspace:*",
Expand All @@ -51,17 +51,17 @@
"houdini-svelte": "^1.2.64",
"lodash.unzip": "^3.4.0",
"papaparse": "^5.4.1",
"postcss": "^8.4.47",
"postcss": "^8.4.49",
"postcss-load-config": "^6.0.1",
"prettier": "^3.3.3",
"prettier-plugin-svelte": "^3.2.7",
"prettier-plugin-svelte": "^3.2.8",
"prettier-plugin-tailwindcss": "^0.6.8",
"radash": "^12.1.0",
"rollup-plugin-visualizer": "^5.12.0",
"safe-flat": "^2.1.0",
"sortablejs": "^1.15.3",
"svelte": "^4.2.19",
"svelte-check": "^4.0.5",
"svelte-check": "^4.0.7",
"svelte-headless-table": "^0.18.3",
"svelte-inview": "^4.0.4",
"svelte-jsoneditor": "^1.1.2",
Expand All @@ -73,7 +73,7 @@
"tslib": "^2.8.1",
"type-fest": "^4.26.1",
"typescript": "^5.6.3",
"vite": "^5.4.10",
"vite": "^5.4.11",
"vite-plugin-node-polyfills": "^0.22.0",
"vitest": "^2.1.4",
"xlsx": "^0.18.5"
Expand Down
18 changes: 16 additions & 2 deletions apps/frontend/src/lib/components/blocks/auth/signup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import { Separator } from "$lib/components/ui/separator"
import PasswordInput from "$lib/components/ui/input/password-input.svelte"
import { LoaderCircleIcon } from "lucide-svelte"
import { cn } from "$lib/utils"
export let redirect: string | null
export let invitationId: string | null
Expand Down Expand Up @@ -113,8 +114,21 @@
<div class="grid gap-2">
<Form.Field {form} name="email">
<Form.Control let:attrs>
<Form.Label for="email">Email</Form.Label>
<Input {...attrs} id="email" type="email" placeholder="Enter your work email" bind:value={$formData.email} />
<Form.Label for="email"
>Email
{#if invitationId}
<span class="text-green-500"> (Invited)</span>
{/if}
</Form.Label>
<Input
{...attrs}
disabled={!!invitationId}
id="email"
type="email"
class={cn(!!invitationId && "border-2 border-green-500")}
placeholder="Enter your work email"
bind:value={$formData.email}
/>
</Form.Control>
<Form.Description />
<Form.FieldErrors />
Expand Down
5 changes: 3 additions & 2 deletions apps/frontend/src/routes/(auth)/signup/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { GetSignupSettingsStore } from "$houdini"
import { redirect } from "@sveltejs/kit"
import type { LayoutLoad } from "./$types"

export const load: LayoutLoad = async () => {
export const load: LayoutLoad = async (event) => {
const store = new GetSignupSettingsStore()

const data = await store.fetch({ policy: "NetworkOnly" })
const invitationId = event.url.searchParams.get("invitationId")

const registrationEnabled = !!data.data?.settings?.registration?.enabled
if (!registrationEnabled) {
if (!registrationEnabled && !invitationId) {
throw redirect(302, "/login")
}

Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ services:
- UNDB_VERIFY_EMAIL=true
- UNDB_MAIL_HOST=maildev
- UNDB_MAIL_PORT=1025
- UNDB_DISABLE_REGISTRATION=true
- [email protected]
- UNDB_ADMIN_PASSWORD=admin
depends_on:
- db
- minio
Expand Down
11 changes: 11 additions & 0 deletions drizzle.turso.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "drizzle-kit"

export default defineConfig({
schema: "./packages/persistence/src/tables.ts",
out: "./apps/backend/drizzle",
dialect: "turso",
tablesFilter: ["undb_*"],
dbCredentials: {
url: process.env.UNDB_DB_TURSO_URL!,
},
})
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
"build:docker": "turbo build:docker",
"predev:desktop": "bun run move-assets",
"dev:desktop": "bun --cwd=apps/desktop run dev:desktop",
"dev:docker": "docker compose -f docker-compose.dev.yaml up",
"dev": "bun --bun turbo dev",
"lint": "turbo lint",
"format": "prettier --write \"**/*.{ts,tsx,md,svelte}\"",
"studio": "drizzle-kit studio --config drizzle.config.ts",
"studio:turso": "drizzle-kit studio --config drizzle.turso.config.ts",
"generate": "bun generate:db",
"generate:db": "drizzle-kit generate --config drizzle.config.ts",
"migrate:db": "drizzle-kit push --config drizzle.config.ts",
"move-assets": "bun run ./scripts/move-assets.ts",
"prepare": "husky || echo 1"
},
"devDependencies": {
"drizzle-kit": "^0.26.2",
"drizzle-kit": "^0.28.0",
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"npm-run-all2": "^7.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"@envelop/opentelemetry": "^6.3.1",
"@opentelemetry/api": "^1.9.0",
"graphql": "16.8.1",
"graphql-yoga": "^5.8.0"
"graphql-yoga": "^5.10.1"
}
}
8 changes: 8 additions & 0 deletions packages/i18n/src/i18n/i18n-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ type RootTranslation = {
* C​a​l​e​n​d​a​r
*/
calendar: string
/**
* P​i​v​o​t
*/
pivot: string
}
widgetTypes: {
/**
Expand Down Expand Up @@ -884,6 +888,10 @@ export type TranslationFunctions = {
* Calendar
*/
calendar: () => LocalizedString
/**
* Pivot
*/
pivot: () => LocalizedString
}
widgetTypes: {
/**
Expand Down
4 changes: 2 additions & 2 deletions packages/persistence/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"devDependencies": {
"@types/bun": "latest",
"drizzle-kit": "^0.26.2"
"drizzle-kit": "^0.28.0"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand All @@ -29,7 +29,7 @@
"@undb/webhook": "workspace:*",
"date-fns": "^4.1.0",
"drizzle-orm": "^0.36.1",
"es-toolkit": "^1.26.1",
"es-toolkit": "^1.27.0",
"kysely": "^0.27.4",
"kysely-bun-sqlite": "^0.3.2",
"radash": "^12.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/zod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"i18next": "^23.16.4",
"i18next": "^23.16.5",
"zod": "^3.23.8",
"zod-i18n-map": "^2.27.0",
"zod-validation-error": "^3.4.0"
Expand Down

0 comments on commit d52fc6e

Please sign in to comment.