Podcast
Questions and Answers
What purpose does the registerUser
function serve in the authController.ts
file?
What purpose does the registerUser
function serve in the authController.ts
file?
- Exports user data
- Logs out the user
- Authenticates existing users
- Handles user registration (correct)
Which of the following is a task performed by authRouter.ts
?
Which of the following is a task performed by authRouter.ts
?
- Connecting to the MongoDB database
- Defining routes for user authentication (correct)
- Managing JWT token generation
- Hashing user passwords
What libraries are installed to enable JWT token management and password hashing?
What libraries are installed to enable JWT token management and password hashing?
- express and bcryptjs
- mongoose and express
- bcryptjs and jsonwebtoken (correct)
- jsonwebtoken and mongoose
What is the role of mongoose
in the structure described?
What is the role of mongoose
in the structure described?
In the index.ts
file, what is the purpose of the app.use(authRouter)
line?
In the index.ts
file, what is the purpose of the app.use(authRouter)
line?
What is the purpose of the comparePassword
method in the IUser interface?
What is the purpose of the comparePassword
method in the IUser interface?
What happens when the userSchema.pre('save')
middleware is triggered?
What happens when the userSchema.pre('save')
middleware is triggered?
Which response status is returned when a user is successfully registered?
Which response status is returned when a user is successfully registered?
What should be done if a user with the same email already exists in the database during registration?
What should be done if a user with the same email already exists in the database during registration?
What response is generated if the user is not found or if the password is incorrect during authentication?
What response is generated if the user is not found or if the password is incorrect during authentication?
What utility function is called to generate a JWT token after a user successfully logs in?
What utility function is called to generate a JWT token after a user successfully logs in?
Which of the following is NOT a field required in the userSchema for creating a new user?
Which of the following is NOT a field required in the userSchema for creating a new user?
What will the logoutUser
function do when called?
What will the logoutUser
function do when called?
What command is used to install the dotenv package?
What command is used to install the dotenv package?
What does the line 'app.use(bodyParser.json())' accomplish?
What does the line 'app.use(bodyParser.json())' accomplish?
Which value must be set to 'true' in the CORS configuration to allow credentials?
Which value must be set to 'true' in the CORS configuration to allow credentials?
What is a likely consequence of not configuring CORS when using different port numbers?
What is a likely consequence of not configuring CORS when using different port numbers?
What is the purpose of the JWT token in the described application setup?
What is the purpose of the JWT token in the described application setup?
What does the line 'dotenv.config();' do in the application?
What does the line 'dotenv.config();' do in the application?
Which package must be installed to enable CORS in the application?
Which package must be installed to enable CORS in the application?
What would likely happen if a user calls an API without a valid token?
What would likely happen if a user calls an API without a valid token?
What should be done with the cookie when a user logs out?
What should be done with the cookie when a user logs out?
What is the purpose of the 'generateToken' function?
What is the purpose of the 'generateToken' function?
What is the default expiration time set for the JWT in the 'generateToken' function?
What is the default expiration time set for the JWT in the 'generateToken' function?
Which of the following is required to connect to the MongoDB database?
Which of the following is required to connect to the MongoDB database?
What is the purpose of the 'clearToken' function?
What is the purpose of the 'clearToken' function?
What environment variable is used to connect to the MongoDB database?
What environment variable is used to connect to the MongoDB database?
What library is used for managing environment variables in this context?
What library is used for managing environment variables in this context?
When should the JWT cookie be set as 'secure'?
When should the JWT cookie be set as 'secure'?
What is the purpose of the authenticate middleware in the route definition?
What is the purpose of the authenticate middleware in the route definition?
What potential risk is associated with retrieving user data based on the ID sent in the URL?
What potential risk is associated with retrieving user data based on the ID sent in the URL?
Which middleware is responsible for securing HTTP response headers?
Which middleware is responsible for securing HTTP response headers?
What data format does the bodyParser middleware recognize for incoming requests?
What data format does the bodyParser middleware recognize for incoming requests?
Which of the following correctly describes the use of dotenv in this application?
Which of the following correctly describes the use of dotenv in this application?
What does the express Router allow you to do in this application?
What does the express Router allow you to do in this application?
What is the significance of declaring the UserBasicInfo interface in this application?
What is the significance of declaring the UserBasicInfo interface in this application?
Which command is used to install the helmet package?
Which command is used to install the helmet package?
Flashcards are hidden until you start studying
Study Notes
Backend Authentication Implementation
- The backend folder structure includes
controllers
,routes
,models
,utils
, andconnections
for better organization.
authController.ts
- Functions for user registration, authentication, and logout are defined but incomplete.
- Exports three main functions:
registerUser
,authenticateUser
, andlogoutUser
.
authRouter.ts
- Uses Express Router to handle specific routes.
- Includes POST routes for registration, login, and logout.
- Controllers are imported from
authController.ts
.
index.ts
- Sets up the Express application to listen on a specified port (defaults to 3000).
- Imports and uses
authRouter
to handle authentication-related endpoints. - Initializes database connection with
connectUserDB
.
Dependency Installation
- Installs
jsonwebtoken
for handling JWT tokens andbcryptjs
for password hashing. - Installs type definitions for the above libraries via
@types/bcryptjs
and@types/jsonwebtoken
. - Uses
mongoose
for MongoDB operations.
User Model (User.ts
)
- Defines
IUser
interface with properties: name, email, password, and a method to compare passwords. - Implements a
userSchema
that includes validation and password hashing before saving to the database.
Auth Functions Implementation
registerUser
checks for existing users, creates a new user, and generates a JWT token on successful registration.authenticateUser
verifies user credentials and generates a JWT if valid.logoutUser
clears the JWT cookie from the client.
JWT Token Management (auth.ts
)
- Functions:
generateToken
for creating JWT tokens and setting security cookies, andclearToken
for clearing cookies upon logout. - Tokens expire in one hour.
MongoDB Connection (userDB.ts
)
- Connects to MongoDB and logs the connection status or errors encountered during the connection process.
Environment Variables
- .env file includes:
PORT
for server portJWT_SECRET
for signing JWTNODE_ENV
for environment typeMONGODB_URI
for MongoDB connection string.
- Uses
dotenv
to load environment variables into the application.
Middleware Integration
- Installs
body-parser
for parsing incoming JSON requests. - Installs
cors
to manage cross-origin requests and sets up middleware for allowed origins. - Adds
helmet
for enhancing API security through setting response headers.
Custom Middleware for Authentication
- Middleware checks token validity before allowing access to certain user-specific routes.
- Exposes user routes under
/users
, ensuring only authenticated requests proceed.
Security Enhancements
- Additional security packages like
helmet
are employed to enhance response security headers. - Properly handles user data by requiring token verification prior to accessing user profiles.
Application Testing
- Use tools like Postman to test API functionalities.
- Troubleshoot CORS issues that may arise due to domain mismatches during frontend and backend interactions.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.