Skip to content

Commit

Permalink
feat: implmenet auth middlwares, update tsconfig, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dallen4 committed Jan 18, 2025
1 parent 87847a4 commit ce0d5eb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"husky": "^9.1.7",
"jscpd": "^4.0.5",
"postcss": "^8.4.49",
"prettier": "3.4.2",
"prettier": "^3.4.2",
"ts-prune": "^0.10.3",
"tsx": "^4.19.2",
"typescript": "^5.7.2",
Expand Down
14 changes: 0 additions & 14 deletions web/pages/api/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ export default async function stripeWebhook(
const sessionId = (event.data.object as Record<string, string>).id;

const email = await getEmailForCheckout(sessionId);

// const usersToUpdate = await getUserIdsByEmail(email);

// const userUpdates = usersToUpdate.map((id) =>
// updateUser(id, { premium: true }),
// );

// try {
// await Promise.all(userUpdates);

// console.log(`${userUpdates.length} users updated...`);
// } catch (err) {
// console.error(err);
// }
}

return res.status(200).send({ message: 'success' });
Expand Down
12 changes: 4 additions & 8 deletions worker/src/routers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { getAuth } from '@hono/clerk-auth';
import { AppRouteParts } from '../constants';
import { hono } from '../lib/http/core';
import { NotAuthenticated } from '../lib/messages';
import { authenticated } from '../lib/middleware';

const authRouter = hono().get(
AppRouteParts.CreateSignInToken,
authenticated(),
async (c) => {
const auth = getAuth(c);

if (!auth?.userId) {
return c.json(NotAuthenticated, 401);
}
const userId = c.get('clerkAuth')!.userId!;

const clerkClient = c.get('clerk');

const { token } =
await clerkClient.signInTokens.createSignInToken({
userId: auth!.userId,
userId,
expiresInSeconds: 25,
});

Expand Down
49 changes: 15 additions & 34 deletions worker/src/routers/vault.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { getAuth } from '@hono/clerk-auth';
import { zValidator } from '@hono/zod-validator';
import { z } from 'zod';
import { AppRouteParts } from '../constants';
import { hono } from '../lib/http/core';
import { NotAuthenticated } from '../lib/messages';
import { createVaultUtils, vaultNameFromUserId } from '../lib/vault';
import { hono } from 'lib/http/core';
import { createVaultUtils, vaultNameFromUserId } from 'lib/vault';
import { authenticated, restricted } from 'lib/middleware';

const VaultNameSchema = z.object({ name: z.string() });

const vaultRouter = hono()
.post(
AppRouteParts.Root,
restricted(),
zValidator('json', VaultNameSchema.partial()),
async (c) => {
const auth = getAuth(c);

if (!auth?.userId) {
return c.json(NotAuthenticated, 401);
}
const userId = c.get('clerkAuth')!.userId!;

const { createVault, createVaultToken } = createVaultUtils(
c.env.TURSO_ORGANIZATION,
Expand All @@ -27,10 +23,7 @@ const vaultRouter = hono()
try {
const { name } = c.req.valid('json');

const vaultName = await vaultNameFromUserId(
auth.userId,
name,
);
const vaultName = await vaultNameFromUserId(userId!, name);

const vaultDatabase = await createVault(vaultName);

Expand All @@ -57,13 +50,10 @@ const vaultRouter = hono()
)
.post(
AppRouteParts.Share,
restricted(),
zValidator('json', VaultNameSchema),
async (c) => {
const auth = getAuth(c);

if (!auth?.userId) {
return c.json(NotAuthenticated, 401);
}
const userId = c.get('clerkAuth')!.userId!;

const { createVaultToken } = createVaultUtils(
c.env.TURSO_ORGANIZATION,
Expand All @@ -73,10 +63,7 @@ const vaultRouter = hono()
const { name } = c.req.valid('json');

try {
const vaultName = await vaultNameFromUserId(
auth.userId,
name,
);
const vaultName = await vaultNameFromUserId(userId!, name);

const token = await createVaultToken(vaultName, 'read-only');

Expand All @@ -91,17 +78,14 @@ const vaultRouter = hono()
)
.get(
AppRouteParts.NameParam,
authenticated(),
zValidator('param', z.object({ name: z.string() })),
async (c) => {
const auth = getAuth(c);

if (!auth?.userId) {
return c.json(NotAuthenticated, 401);
}
const userId = c.get('clerkAuth')!.userId!;

const { name } = c.req.valid('param');

const vaultName = await vaultNameFromUserId(auth.userId, name);
const vaultName = await vaultNameFromUserId(userId!, name);

const { getVault } = createVaultUtils(
c.env.TURSO_ORGANIZATION,
Expand All @@ -115,17 +99,14 @@ const vaultRouter = hono()
)
.delete(
AppRouteParts.NameParam,
restricted(),
zValidator('param', z.object({ name: z.string() })),
async (c) => {
const auth = getAuth(c);

if (!auth?.userId) {
return c.json(NotAuthenticated, 401);
}
const userId = c.get('clerkAuth')!.userId!;

const { name } = c.req.valid('param');

const vaultName = await vaultNameFromUserId(auth.userId, name);
const vaultName = await vaultNameFromUserId(userId!, name);

const { deleteVault } = createVaultUtils(
c.env.TURSO_ORGANIZATION,
Expand Down
4 changes: 2 additions & 2 deletions worker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"*": ["./src/*"],
"@shared/*": ["../shared/*"]
},

Expand All @@ -20,8 +22,6 @@
"module": "es2022" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
"types": [
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9832,7 +9832,7 @@ preserve@^0.2.0:
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
integrity sha512-s/46sYeylUfHNjI+sA/78FAHlmIuKqI9wNnzEOGehAlUUYeObv5C2mOinXBjyUyWmJ2SfcS2/ydApH4hTF4WXQ==

[email protected]:
prettier@^3.4.2:
version "3.4.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.4.2.tgz#a5ce1fb522a588bf2b78ca44c6e6fe5aa5a2b13f"
integrity sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ==
Expand Down

0 comments on commit ce0d5eb

Please sign in to comment.