Skip to content

Commit

Permalink
fix: build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ephraimduncan committed Feb 21, 2024
1 parent 2f55f06 commit 3627e36
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"dependencies": {
"@hookform/resolvers": "^3.3.2",
"@lucia-auth/adapter-drizzle": "1.0.0",
"@lucia-auth/adapter-drizzle": "1.0.0-beta.6",
"@planetscale/database": "^1.11.0",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-checkbox": "^1.0.4",
Expand Down
2 changes: 1 addition & 1 deletion src/app/(auth)/signup/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
CardTitle,
} from "~/components/ui/card";
import { Input } from "~/components/ui/input";
import { githubLogoIcon } from "~/components/icons";
import { APP_TITLE } from "~/lib/constants";
import { Label } from "~/components/ui/label";
import { signup } from "~/lib/auth/actions";
import { SubmitButton } from "~/components/submit-button";
import Link from "next/link";

export function Signup() {
const [state, formAction] = useFormState(signup, null);
Expand Down
3 changes: 2 additions & 1 deletion src/app/(auth)/verify-email/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { redirect } from "next/navigation";
import { validateRequest } from "~/lib/auth/validate-request";
import { VerifyCode } from "./verify-code";
import { redirects } from "~/lib/constants";
import type { User } from "@/server/db/schema";

export const metadata = {
title: "Verify Email",
description: "Verify Email Page",
};

export default async function ForgotPasswordPage() {
const { user } = await validateRequest();
const { user } = (await validateRequest()) as { user: User | null };

if (!user) redirect(redirects.toLogin);
if (user.emailVerified) redirect(redirects.afterVerify);
Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/_components/header.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Link from "next/link";
import { type User } from "lucia";
import { UserDropdown } from "~/app/(main)/_components/user-dropdown";
import { FunctionSquare } from "lucide-react";
import { APP_TITLE } from "@/lib/constants";
import type { User } from "~/server/db/schema";

const routes = [{ name: "Dashboard", href: "/dashboard" }] as const;

Expand Down
3 changes: 2 additions & 1 deletion src/app/(main)/account/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { User } from "~/server/db/schema";
import { redirect } from "next/navigation";
import { SubmitButton } from "~/components/submit-button";
import {
Expand All @@ -13,7 +14,7 @@ import { validateRequest } from "~/lib/auth/validate-request";
import { redirects } from "~/lib/constants";

export default async function AccountPage() {
const { user } = await validateRequest();
const { user } = (await validateRequest()) as { user: User | null };
if (!user) redirect(redirects.toLogin);

return (
Expand Down
3 changes: 2 additions & 1 deletion src/app/(main)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { redirect } from "next/navigation";
import { Header } from "./_components/header";
import { validateRequest } from "~/lib/auth/validate-request";
import { redirects } from "~/lib/constants";
import type { User } from "~/server/db/schema";

const MainLayout = async ({ children }: { children: ReactNode }) => {
const { user } = await validateRequest();
const { user } = (await validateRequest()) as { user: User | null };

if (!user) redirect(redirects.toLogin);
if (user.emailVerified === false) redirect(redirects.toVerify);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/auth/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import {
emailVerificationCodes,
passwordResetTokens,
type User,
users,
} from "~/server/db/schema";
import { sendMail } from "~/server/send-mail";
Expand Down Expand Up @@ -166,7 +167,7 @@ export async function resendVerificationEmail(): Promise<{
error?: string;
success?: boolean;
}> {
const { user } = await validateRequest();
const { user } = (await validateRequest()) as { user: User | null };
if (!user) {
return redirect(redirects.toLogin);
}
Expand Down Expand Up @@ -201,7 +202,7 @@ export async function verifyEmail(
if (typeof code !== "string" || code.length !== 8) {
return { error: "Invalid code" };
}
const { user } = await validateRequest();
const { user } = (await validateRequest()) as { user: User | null };
if (!user) {
return redirect(redirects.toLogin);
}
Expand Down
19 changes: 11 additions & 8 deletions src/lib/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Lucia, TimeSpan } from "lucia";
import type { Adapter } from "lucia";
import { GitHub } from "arctic";
import { env } from "~/env.js";
import { luciaAdapter } from "~/server/db";
import { type User as DbUser } from "~/server/db/schema";
import { sessions, users, type User as DbUser } from "@/server/db/schema";
import { db } from "@/server/db";
import { webcrypto } from "node:crypto";
import { DrizzlePostgreSQLAdapter } from "@lucia-auth/adapter-drizzle";

// Uncomment the following lines if you are using nodejs 18 or lower. Not required in Node.js 20, CloudFlare Workers, Deno, Bun, and Vercel Edge Functions.
// import { webcrypto } from "node:crypto";
// globalThis.crypto = webcrypto as Crypto;
globalThis.crypto = webcrypto as unknown as Crypto;

export const lucia = new Lucia(luciaAdapter, {
const adapter = new DrizzlePostgreSQLAdapter(db, sessions, users);

export const lucia = new Lucia(adapter as unknown as Adapter, {
getSessionAttributes: (/* attributes */) => {
return {};
},
Expand All @@ -34,8 +37,8 @@ export const lucia = new Lucia(luciaAdapter, {
});

export const github = new GitHub(
env.GITHUB_CLIENT_ID as string,
env.GITHUB_CLIENT_SECRET as string,
env.GITHUB_CLIENT_ID,
env.GITHUB_CLIENT_SECRET,
{
redirectURI: env.NEXT_PUBLIC_APP_URL + "/login/github/callback",
},
Expand Down
3 changes: 2 additions & 1 deletion src/lib/auth/validate-request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cache } from "react";
import { cookies } from "next/headers";
import type { Session, User } from "lucia";
import { lucia } from "~/lib/auth";
import type { User, Session } from "lucia";

export const uncachedValidateRequest = async (): Promise<
{ user: User; session: Session } | { user: null; session: null }
Expand Down Expand Up @@ -32,6 +32,7 @@ export const uncachedValidateRequest = async (): Promise<
} catch {
console.error("Failed to set session cookie");
}

return result;
};

Expand Down
2 changes: 1 addition & 1 deletion src/server/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (env.NODE_ENV === "production") {

export { db };

export const luciaAdapter = new DrizzlePostgreSQLAdapter(
export const adapter = new DrizzlePostgreSQLAdapter(
db,
schema.sessions,
schema.users,
Expand Down
2 changes: 2 additions & 0 deletions src/server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,5 @@ export type NewFormData = typeof formDatas.$inferInsert;

export type Form = typeof forms.$inferSelect;
export type NewForm = typeof forms.$inferInsert;

export type Session = typeof sessions.$inferSelect;

0 comments on commit 3627e36

Please sign in to comment.