Next.js Full Tutorial for Beginners | Next.js 13 Full Stack App by Lama Dev

ref – https://www.youtube.com/watch?v=VE8BkImUciY

go t o your dirctory folder

npx create-next-app@latest .

no typescript
yes lint
no tailwind
yes /src
yes app router
yes import alias

Client Side

Next JS has client side and server side. Client side is usually components that will be rendered on the client, such as dashboard components, header, footers, etc.

We signify it by using “use client” at the very top.

Now if you log, you’ll be able to see it on the browser log. We need our footer components to be rendered on the client side. So we must do it for our Footer component.

Similarly, for Register page, Login Page…whatever page that should be rendered on the client side.

src/app/dashboard/(auth)/register/page.jsx

We create a AuthProvider component that uses SessionProvider to keep a session going in its children.
We use this AuthProvider
src/components/AuthProvider/AuthProvider.jsx

we use it in client side layout:

src/app/layout.js

Server Side (API)

On the client side, we have client component dashboard. In our dashboard’s register’s page.jsx, we have a form, and in the submit handler, have this implementation to do a POST request on our server side api:

src/app/dashboard/(auth)/register/page.jsx

We fetch from our server api (/api/auth/register).

There, we have a route file, which has a POST implementation. The parameter is a request object sent from dashboard/(auth)/register.page.jsx

1) We retrieve the body data from via JSON.
2) We’re on the server, so we connect to our mongo db.
3) We then hash the password.
4) Create a new User model, and then save it into the mongo db collections.

src/app/api/auth/register/route.js

Create user

Creating a User starts at the client page dashboard/(auth)/register/page.jsx. We have a form, and after inputting data, we execute the submit handler.

src/app/dashboard/(auth)/register/page.jsx

The request object gets passed into the POST implementation.
We extract it.
We then need to connect to the mongodb because we need to make an INSERT into the mongodb via Models.
But first, let’s has our password before we save it into the db.

When the hashedPassword is successfull, we create a new User model from it and the rest of the request body data.

Then since this is a mongoose model, we simply call save() on it to INSERT the data into the remote database.

src/app/api/auth/register/route.js

After successful insertion, we have our router go to dashboard’s login:

with parameter key success and a value Account has been created.

This means that we have a param object that will be available in Login component.

In it, useEffect will see that params have been updated and then setSuccess to get the value of the key ‘success’, which is ‘Account has been created’.

Therefore, it will set the state success to “Account has been created”.

Finally, make sure your user exists in the db

After you create a User, you Log In

We start off at dashboard’s login component.

We fill in the email/pwd and click the submit handler.

src/app/dashboard/(auth)/login/page.jsx

The submit handler is simply:

src/app/dashboard/(auth)/login/page.jsx

The event object’s target property has all the values stored.
We use signIn with the provider name “credential”, and a param object.

src/app/api/auth/[…nextauth]/route.js

In our Server api’s auth, we use NextAuth to authenticate credentials:

We specify how we want to authenticate credentials. Earlier we saw that we used signIn(“credentail”, {..})
“credential” matches ouCredentialsProvider’s id “credentials”. So this means that we will hit this authorize function.

The credential object looks like this:

Then we connect to the DB. When we connect to the DB, all models’ functionalities will update the mongo db. So we use User model to findOne with the email.

Once its found, we use bcrypt’s compare to see if the incoming password and the db password match.

If the password is correct, we return the user object.
Else, we throw a Wrong Credential msg.

Logged in and ready to Create New Post

Add New Post form is in dashboard’s page.

src/app/dashboard/page.jsx

The submit handler gets all textfield values and then put inside of a body of the fetch.
We do a fetch using POST on api/posts

This is very straightforward because we simply hit the POST function, extract the body, and create a new Post model from that body.
We call save on the model, and this updates the mongo db.

src/app/api/posts/route.js

Delete Post

Simply click on the delete link to delete the Post. We make a DELETE request on /api/posts/:id to trigger the DELETE function in the API.

src/app/dashboard/page.jsx

the id of the post gets passed as params.

When you delete, you’ll see the log:

API – You are trying to (DELETE) post id 64f1d35062e470734364fc72

So the id of the posts matches that of the document in our mongodb.