|
1 | | -This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). |
| 1 | +# NextJs V5 Authentication |
2 | 2 |
|
3 | | -## Getting Started |
| 3 | +7. Create Register page UI |
| 4 | +8. Install prisma |
| 5 | + `npm i -D prisma` |
| 6 | +9. install prisma client |
| 7 | + `npm i @prisma/client` |
| 8 | +10. Go to `database.connection.ts` and add following code |
4 | 9 |
|
5 | | -First, run the development server: |
| 10 | +``` |
| 11 | +import { PrismaClient } from "@prisma/client"; |
| 12 | +declare global { |
| 13 | + var prisma: PrismaClient | undefined; |
| 14 | +} |
| 15 | +export const db = globalThis.prisma || new PrismaClient(); |
| 16 | +
|
| 17 | +if (process.env.NODE_ENV !== "production") globalThis.prisma = db |
| 18 | +
|
| 19 | +``` |
| 20 | + |
| 21 | +11. Run command |
| 22 | + `npx prisma init` |
| 23 | +12. We used `Neon DB` as our database |
| 24 | +13. Got to `Neon DB ` to create a new database |
| 25 | +14. Paste connection string in `schema.prisma` and `.env` file |
| 26 | +15. Start creating schema in `schema.prisma` file |
| 27 | +16. With help of `db` in `database.connection.ts` file we can access our models |
| 28 | +17. Create `User` model in `schema.prisma` file |
| 29 | +18. Should also run following command to access `User` model in `database.connection.ts` file |
| 30 | + `npx prisma generate` |
| 31 | +19. To psuh your schema to database |
| 32 | + `npx prisma db push` |
| 33 | + |
| 34 | +20. Move to `Auth JS site` |
| 35 | +21. Select database adapter as `Prisma` |
| 36 | +22. Install `@auth/prisma-adapter` |
| 37 | +23. Comand to install `@auth/prisma-adapter` |
| 38 | + `npm i @auth/prisma-adapter` |
| 39 | +24. Copy model `User` and paste in `schema.prisma` file |
| 40 | +25. Copy model `Account` and paste in `schema.prisma` file ( We are not using session model from `Auth JS site`) |
| 41 | +26. Push again to database |
| 42 | + `npx prisma generate` and `npx prisma db push` |
| 43 | +27. Auth does not use `password` field in `User` model. So we need to add it to user model as optional because google aut h providers do not require password in `schema.prisma` file. |
| 44 | +28. Add the code below to validate fields in `register.ts` |
| 45 | + |
| 46 | +``` |
| 47 | +"use server"; |
| 48 | +
|
| 49 | +import { RegisterSchema } from "@/schema"; |
| 50 | +import * as z from "zod"; |
| 51 | +
|
| 52 | +export const register = async (values: z.infer<typeof RegisterSchema>) => { |
| 53 | +const validatedFields = RegisterSchema.safeParse(values); // safeParse returns a ZodResult object, and it is used to validate the input values |
| 54 | +if (!validatedFields.success) { |
| 55 | + return { error: "Invalid fields!" }; |
| 56 | +} |
| 57 | +return { success: "Email sent!" }; |
| 58 | +}; |
| 59 | +
|
| 60 | +``` |
| 61 | + |
| 62 | +29. Install `bcrypt` to hash password |
| 63 | + `npm i bcrypt` and `npm i -D @types/bcrypt` |
| 64 | +30. Create register function |
6 | 65 |
|
7 | | -```bash |
8 | | -npm run dev |
9 | | -# or |
10 | | -yarn dev |
11 | | -# or |
12 | | -pnpm dev |
13 | | -# or |
14 | | -bun dev |
15 | 66 | ``` |
| 67 | +export const register = async (values: z.infer<typeof RegisterSchema>) => { |
| 68 | +
|
| 69 | + // * check and store user in database |
| 70 | +
|
| 71 | + const validatedFields = RegisterSchema.safeParse(values); // safeParse returns a ZodResult object, and it is used to validate the input values |
| 72 | + if (!validatedFields.success) { |
| 73 | + return { error: "Invalid fields!" }; |
| 74 | + } |
| 75 | +
|
| 76 | + const { email, password, name } = validatedFields.data; |
| 77 | + const hashedPassword = await bcrypt.hash(password, 10); // 10 is the number of salt rounds |
| 78 | +
|
| 79 | + //finding the email in database |
| 80 | + const exisitingUser = await db.user.findUnique({ |
| 81 | + where: { |
| 82 | + email, |
| 83 | + }, |
| 84 | + }); |
| 85 | +
|
| 86 | + // if user already exists, return error |
| 87 | + if (exisitingUser) { |
| 88 | + return { error: "Email already exists!" }; |
| 89 | + } |
16 | 90 |
|
17 | | -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. |
| 91 | + // if not, create and save it in database |
| 92 | + await db.user.create({ |
| 93 | + data: { |
| 94 | + name, |
| 95 | + email, |
| 96 | + password: hashedPassword, |
| 97 | + }, |
| 98 | + }); |
18 | 99 |
|
19 | | -You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. |
| 100 | + // TODO: send verification email |
20 | 101 |
|
21 | | -This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. |
| 102 | + return { success: "Email sent!" }; |
| 103 | +}; |
22 | 104 |
|
23 | | -## Learn More |
| 105 | +``` |
| 106 | + |
| 107 | +31. Create user actions in `user.action.ts` file, to get user by email and id |
24 | 108 |
|
25 | | -To learn more about Next.js, take a look at the following resources: |
| 109 | +``` |
| 110 | +export const getUserByEmail = async (email: string) => { |
| 111 | + try { |
| 112 | + const user = await db.user.findUnique({ where: { email } }); |
| 113 | + return user; |
| 114 | + } catch { |
| 115 | + return null; |
| 116 | + } |
| 117 | +}; |
26 | 118 |
|
27 | | -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. |
28 | | -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. |
| 119 | +export const getUserById = async (id: string) => { |
| 120 | + try { |
| 121 | + const user = await db.user.findUnique({ where: { id } }); |
| 122 | + return user; |
| 123 | + } catch { |
| 124 | + return null; |
| 125 | + } |
| 126 | +}; |
29 | 127 |
|
30 | | -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! |
| 128 | +``` |
31 | 129 |
|
32 | | -## Deploy on Vercel |
| 130 | +32. Use it in `register.ts` function |
33 | 131 |
|
34 | | -The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. |
| 132 | +``` |
| 133 | + //finding the email in database |
| 134 | + const exisitingUser = await getUserByEmail(email) |
35 | 135 |
|
36 | | -Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. |
| 136 | +``` |
| 137 | +33. Now, for `login` we have to install nextauth v5 |
| 138 | + `npm i next-auth@beta` |
0 commit comments