Skip to content

Commit

Permalink
basic auth ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Prajwal-kp-18 committed Aug 26, 2024
1 parent b7ac79c commit ffe9cff
Show file tree
Hide file tree
Showing 38 changed files with 2,294 additions and 240 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ yarn-error.log*

# local env files
.env*.local
.env

# vercel
.vercel
Expand Down
33 changes: 33 additions & 0 deletions actions/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use server";
import * as z from "zod";
import { LoginSchema } from "../schemas";
import { signIn } from "../auth";
import { DEFAULT_LOGIN_REDIRECT } from "../routes";
import { AuthError } from "next-auth";
export const login = async (values: z.infer<typeof LoginSchema>) => {
const validatedFields = LoginSchema.safeParse(values);

if (!validatedFields.success) {
return { error: "Invalid Fields" };
}

const { email, password } = validatedFields.data;

try {
await signIn("credentials", {
email,
password,
redirectTo: DEFAULT_LOGIN_REDIRECT,
});
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case "CredentialsSignin":
return { error: "Invalid credentials " };
default:
return { error: "Something went wrong!" };
}
}
throw error;
}
};
32 changes: 32 additions & 0 deletions actions/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use server";

import bcrypt from "bcryptjs";
import { db } from "@/lib/db";
import * as z from "zod";
import { RegisterSchema } from "../schemas";
import { getUserByEmail } from "../data/user";

export const register = async (values: z.infer<typeof RegisterSchema>) => {
const validatedFields = RegisterSchema.safeParse(values);

if (!validatedFields.success) {
return { error: "Invalid Fields" };
}

const { email, password, name } = validatedFields.data;
const hashedPassword = await bcrypt.hash(password, 10);

const existingUser = await getUserByEmail(email);

if (existingUser) {
return { error: "Email already exists" };
}

await db.user.create({
data: { email, password: hashedPassword, name },
});

// TODO: send verification token email

return { success: "User Created!" };
};
27 changes: 27 additions & 0 deletions auth.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import bcrypt from "bcryptjs";
import type { NextAuthConfig } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { LoginSchema } from "./schemas";
import { getUserByEmail } from "./data/user";

export default {
providers: [
Credentials({
async authorize(credentials) {
const validatedFields = LoginSchema.safeParse(credentials);
if (validatedFields.success) {
const { email, password } = validatedFields.data;

const user = await getUserByEmail(email);
console.log(user);
if (!user || !user.password) return null;

const passwordsMatch = await bcrypt.compare(password, user.password);

if (passwordsMatch) return user;
}
return null;
},
}),
],
} satisfies NextAuthConfig;
15 changes: 15 additions & 0 deletions auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { db } from "@/lib/db";
import authConfig from "./auth.config";

export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
adapter: PrismaAdapter(db),
session: { strategy: "jwt" },
...authConfig,
});
17 changes: 17 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "new-york",
"rsc": true,
"tsx": true,
"tailwind": {
"config": "tailwind.config.ts",
"css": "src/app/globals.css",
"baseColor": "slate",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils"
}
}
19 changes: 19 additions & 0 deletions data/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { db } from "@/lib/db";

export const getUserByEmail = async (email: string) => {
try {
const user = await db.user.findUnique({ where: { email } });
return user;
} catch {
return null;
}
};

export const getUserById = async (id: string) => {
try {
const user = await db.user.findUnique({ where: { id } });
return user;
} catch {
return null;
}
};
Loading

0 comments on commit ffe9cff

Please sign in to comment.