This version uses React, Redux, Express, Passport, and PostgreSQL (a full list of dependencies can be found in package.json
). This version uses React to control the login requests and redirection in coordination with client-side routing.
NOTE: Do not clone this repository.
- Don't Fork or Clone. Use the template!
Create a new database called passport_intro
and create the following tables:
CREATE TABLE "user" (
"id" SERIAL PRIMARY KEY,
"username" VARCHAR (80) UNIQUE NOT NULL,
"password" VARCHAR (1000) NOT NULL
);
CREATE TABLE "pet" (
"id" SERIAL PRIMARY KEY,
"firstname" VARCHAR (80) UNIQUE NOT NULL,
"user_id" INT REFERENCES "user"
);
npm install
npm run server
npm run client
This project has been set up with passport. There is a lot of code that has already been written. You're not expected to understand all of the code but will need to be able to check if the user is authenticated in your routes.
Let's look through some of the provided code to get an idea of what's happening.
- server.js
- strategies/user.strategy.js
- modules/session-middleware.js
- modules/authentication-middleware.js
- constants/warnings.js
req.isAuthenticated()
is a function available to you on all of your routes.- If the user is authenticated,
req.user
will be available to you.
Let's take a look at this in practice.
To use Postman with this repo, you will need to set up requests in Postman to register a user and login a user at a minimum.
Keep in mind that once you using the login route, Postman will manage your session cookie for you just like a browser, ensuring it is sent with each subsequent request. If you delete the localhost
cookie in Postman, it will effectively log you out.
- Start the server -
npm run server
- Import the sample routes JSON file by clicking
Import
in Passport. Select the file. - Click
Collections
andSend
the following three calls in order:POST /api/user/register
registers a new user, see body to change username/passwordPOST /api/user/login
will login a user, see body to change username/passwordGET /api/user
will get user information, by default it's not very much
After running the login route above, you can try any other route you've created that requires a logged in user!