# Build A Fullstack App with Remix, Prisma & MongoDB: Authentication URL: https://www.prisma.io/blog/fullstack-remix-prisma-mongodb-2-ZTmOy58p4re8 Published: 2022-04-26 Authors: Sabin Adams Tags: education Series: Build A Fullstack App with Remix, Prisma & MongoDB Description: Learn how to build and deploy a fullstack application using Remix, Prisma, and MongoDB. In this article, we will be setting up authentication for our Remix application using session-based authentication. Welcome to the second article of this series where you are learning how to build a full-stack application from the ground up using MongoDB, Prisma, and Remix! In this part, you will set up session-based authentication for your Remix application. ## Table Of Contents [#table-of-contents] * [Introduction](#introduction) * [Development environment](#development-environment) * [Set up a login route](#set-up-a-login-route) * [Create a re-usable layout component](#create-a-re-usable-layout-component) * [Create the sign in form](#create-the-sign-in-form) * [Build the form](#build-the-form) * [Create a form field component](#create-a-form-field-component) * [Add a sign up form](#add-a-sign-up-form) * [Store the form action in state](#store-the-form-action-in-state) * [Add toggleable fields](#add-toggleable-fields) * [The authentication flow](#the-authentication-flow) * [Build the register function](#build-the-register-function) * [Create an instance of `PrismaClient`](#create-an-instance-of-prismaclient) * [Update your data model](#update-your-data-model) * [Add a user service](#add-a-user-service) * [Build the login function](#build-the-login-function) * [Add session management](#add-session-management) * [Handle the login and register form submissions](#handle-the-login-and-register-form-submissions) * [Authorize users on private routes](#authorize-users-on-private-routes) * [Add form validation](#add-form-validation) * [Summary & What's next](#summary--whats-next) [Watch the video on YouTube](https://www.youtube.com/watch?v=vR33ZRJekHk) ## Introduction [#introduction] In the [last part](https://www.prisma.io/blog/fullstack-remix-prisma-mongodb-1-7d0bftxbmb6r) of this series you set up your Remix project and got a MongoDB database up and running. You also configured TailwindCSS and Prisma and began to model out a `User` collection in your `schema.prisma` file. In this part you will implement authentication in your application, allowing a user to create an account and sign in via sign in and sign up forms. > **Note**: The starting point for this project is available in the [part-1](https://github.com/sabinadams/kudos-remix-mongodb-prisma/tree/part-1) branch of the GitHub repository. If you'd like to see the final result of this part, head over to the [part-2](https://github.com/sabinadams/kudos-remix-mongodb-prisma/tree/part-2) branch. ### Development environment [#development-environment] In order to follow along with the examples provided, you will be expected to ... * ... have [Node.js](https://nodejs.org) installed. * ... have [Git](https://git-scm.com/downloads) installed. * ... have the [TailwindCSS VSCode Extension](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss) installed. *(optional)* * ... have the [Prisma VSCode Extension](https://marketplace.visualstudio.com/items?itemName=Prisma.prisma) installed. *(optional)* > **Note**: The optional extensions add some really nice intellisense and syntax highlighting for Tailwind and Prisma. ## Set up a login route [#set-up-a-login-route] The very first thing you need to do to kick things off is set up a `/login` route where your sign in and sign up forms will live. To create a route within the Remix framework, add a file to the `app/routes` folder. The name of that file will be used as the name of the route. For more info on how routing works in Remix, check out their [docs](https://remix.run/docs/en/v1/tutorials/jokes#routes). Create a new file in `app/routes` named `login.tsx` with the following contents: ```tsx // app/routes/login.tsx export default function Login() { return (
The child
` tag is a child of the `Parent` component and will be rendered into the `Parent` component wherever you decide to render the `children` prop value. To see this in action, create a new folder inside the `app` folder named `components`. Inside of that folder create a new file named `layout.tsx`. In that file, export the following [function component](https://reactjs.org/docs/components-and-props.html): ```tsx // app/components/layout.tsx export function Layout({ children }: { children: React.ReactNode }) { return
Child Element
Child Element
Log In To Give Some Praise!