Skip to content

Commit

Permalink
refactor: improvement here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
mrevanzak committed May 22, 2024
1 parent 8190e2e commit 4f0ef04
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 67 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/app/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Error components must be Client Components
import * as React from "react";
import { signOut } from "next-auth/react";
import { signOut } from "@/lib/actions/auth";
import { RiAlarmWarningFill } from "react-icons/ri";

import { Button } from "@tanya.in/ui/button";
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/signin-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as React from "react";
import Link from "next/link";
import { signIn } from "@/lib/actions/auth";
import { signInFormSchema } from "@/lib/validation/sign-in";
import { authSchema } from "@/server/api/routers/auth/auth.input";
import { FaEye, FaEyeSlash } from "react-icons/fa";

import { Button } from "@tanya.in/ui/button";
Expand All @@ -22,7 +22,7 @@ export function SignInForm() {
const [isPending, setIsPending] = React.useState(false);

const methods = useForm({
schema: signInFormSchema,
schema: authSchema,
mode: "onTouched",
});
const { handleSubmit, control } = methods;
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/user.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import type { Session } from "next-auth";
import { signOut } from "next-auth/react";
import { signOut } from "@/lib/actions/auth";

import { Avatar } from "@tanya.in/ui/avatar";
import {
Expand Down Expand Up @@ -29,7 +29,7 @@ export function UserButton(props: { user?: Session["user"] }) {
aria-label="Profile Actions"
variant="flat"
disabledKeys={["profile"]}
onAction={async () => signOut()}
onAction={async () => await signOut()}
>
<DropdownItem key="profile" className="h-14 gap-2">
<p className="font-semibold">Signed in as</p>
Expand Down
8 changes: 6 additions & 2 deletions apps/web/src/lib/actions/auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use server";

import { signIn as attempt } from "@/server/auth";
import { signInServer, signOutServer } from "@/server/auth";
import { AuthError } from "next-auth";

export async function signIn(email: string, password: string) {
try {
await attempt("credentials", {
await signInServer("credentials", {
email: email,
password: password,
redirectTo: "/",
Expand All @@ -19,3 +19,7 @@ export async function signIn(email: string, password: string) {
throw error;
}
}

export async function signOut() {
await signOutServer();
}
10 changes: 0 additions & 10 deletions apps/web/src/lib/validation/sign-in.ts

This file was deleted.

8 changes: 7 additions & 1 deletion apps/web/src/server/api/routers/auth/auth.input.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { users } from "@/server/db/schema";
import { createInsertSchema } from "drizzle-zod";
import { z } from "zod";

export const authSchema = createInsertSchema(users).pick({
export const authSchema = createInsertSchema(users, {
email: z.string().email(),
password: z
.string()
.min(8, { message: "Password must be at least 8 characters long" }),
}).pick({
email: true,
password: true,
});
23 changes: 2 additions & 21 deletions apps/web/src/server/api/routers/auth/auth.procedure.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import { createTRPCRouter, publicProcedure } from "@/server/api/trpc";
import { signIn } from "@/server/auth";
import { TRPCError } from "@trpc/server";
import { AuthError } from "next-auth";

import { authSchema } from "./auth.input";
import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc";

export const authRouter = createTRPCRouter({
login: publicProcedure.input(authSchema).mutation(async ({ input }) => {
try {
await signIn("credentials", {
email: input.email,
password: input.password,
redirectTo: "/",
});
} catch (error) {
if (error instanceof AuthError)
throw new TRPCError({
code: "BAD_REQUEST",
message: error.cause?.err?.message ?? "Oops! Something went wrong!",
});
}
}),
me: protectedProcedure.query(({ ctx }) => ctx.session.user.id),
});
11 changes: 7 additions & 4 deletions apps/web/src/server/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NextAuthConfig } from "next-auth";
import { signInFormSchema } from "@/lib/validation/sign-in";
import { authSchema } from "@/server/api/routers/auth/auth.input";
import { db } from "@/server/db";
import bcrypt from "bcryptjs";
import CredentialsProvider from "next-auth/providers/credentials";
Expand All @@ -19,8 +19,7 @@ export default {
},
},
async authorize(credentials) {
const { email, password } =
await signInFormSchema.parseAsync(credentials);
const { email, password } = await authSchema.parseAsync(credentials);
const user = await db.query.users.findFirst({
where: (user, { eq }) => eq(user.email, email),
});
Expand All @@ -30,7 +29,11 @@ export default {
if (!(await bcrypt.compare(password, user.password))) {
throw new Error("Invalid credentials");
}
return user;

return {
...user,
password: undefined,
};
},
}),
],
Expand Down
33 changes: 9 additions & 24 deletions apps/web/src/server/auth.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { DefaultSession } from "next-auth";
import authConfig from "@/server/auth.config";
import NextAuth from "next-auth";

import "next-auth/jwt";

import type { z } from "zod";
import { env } from "@/env";
import authConfig from "@/server/auth.config";
import { users } from "@/server/db/schema";
import { createSelectSchema } from "drizzle-zod";
import NextAuth from "next-auth";

const user = createSelectSchema(users);
type UserData = Omit<z.infer<typeof user>, "password">;
Expand All @@ -18,28 +15,15 @@ declare module "next-auth" {
/**
* Returned by `auth`, `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user: UserData &
/**
* By default, TypeScript merges new interface properties and overwrites existing ones.
* In this case, the default session user properties will be overwritten,
* with the new ones defined above. To keep the default session user properties,
* you need to add them back into the newly declared interface.
*/
DefaultSession["user"];
}
}

declare module "next-auth/jwt" {
interface JWT {
role: UserData["role"];
interface Session extends DefaultSession {
user: User;
}
}

export const {
handlers: { GET, POST },
signIn,
signOut,
signIn: signInServer,
signOut: signOutServer,
auth,
} = NextAuth({
...authConfig,
Expand All @@ -49,11 +33,12 @@ export const {
callbacks: {
jwt({ token, user }) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (user) token.role = user.role;
if (user) token.user = user;
return token;
},
session({ session, token }) {
session.user.role = token.role;
//@ts-expect-error - next-auth type is pain in the ass
session.user = token.user;
return session;
},
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit 4f0ef04

Please sign in to comment.