-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Prajwal-kp-18
committed
Aug 26, 2024
1 parent
b7ac79c
commit ffe9cff
Showing
38 changed files
with
2,294 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ yarn-error.log* | |
|
||
# local env files | ||
.env*.local | ||
.env | ||
|
||
# vercel | ||
.vercel | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
Oops, something went wrong.