1- What is passport-google-oauth20
?
2- Why do we use cookieParser
and encryptCookieNodeMiddleware
?
3- How do you let Express app use cookie middlewares (cookieParser, encryptCookieNodeMiddleware) with secret key for handling encryption of cookies *(answer might require code snippets)?
4- How to let your Express app use a middleware function that sends 401 (When not authenticated) error response code for auth errors and 500 (other errors) *(answer might require code snippets)?
5- In the auth.js
there is four paths:
/google, /me, /google/callback, /logout
can you guess what does each path refer too?
6- How do you let your Express app use JWT middleware to be used on all routes starting with /api?
a. How to exclude certain paths from the JWT authentication?
b. How to extract the JWT token from the request?
7- How do you Prepare Google Auth handling configuration?
8- How do you connect and configure Google OAuth 2.0 strategy on PassportJS instance?
9- How do you initialize PassportJS middleware?
Mohammed Nazar, Shinak Mohammed, Nada Al-Shakiry, Mohammed Nazm, Abdulrahman khalil
Install the necessary packages:
npm install cookie-parser cookie-encrypter
Require the packages in your Express app file:
const express = require('express'); const cookieParser = require('cookie-parser'); const encryptCookieNodeMiddleware = require('cookie-encrypter');
Set up the secret key for encryption:
const secretKey = 'yourSecretKey';
Use the cookieParser middleware to parse incoming cookies:
app.use(cookieParser());
Use the encryptCookieNodeMiddleware middleware with the secret key for encrypting cookies:
app.use(encryptCookieNodeMiddleware(secretKey));
By following these steps, your Express app will be able to use cookie middlewares with a secret key for handling encryption of cookies. Make sure to replace 'yourSecretKey' with your actual secret key for encryption.
// Custom error handling middleware
``app.use((err, req, res, next) => {
if (err.name === 'UnauthorizedError') {
res.status(401).json({ error: 'Unauthorized' });
} else {
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Example route that triggers an authentication error
app.get('/protected', (req, res, next) => {
const error = new Error('Unauthorized');
error.name = 'UnauthorizedError';
next(error);
});``
In this code snippet:
We define a custom error handling middleware function that takes four arguments (err, req, res, next).
Inside the middleware function, we check if the err object has a name property equal to 'UnauthorizedError'. If it does, we send a 401 error response with a JSON object containing the error message 'Unauthorized'. Otherwise, we send a 500 error response with the message 'Internal Server Error'.
We then have an example route /protected that triggers an authentication error by creating a new Error object with the name set to 'UnauthorizedError'.
By using this custom error handling middleware, you can easily send the appropriate error response codes based on the type of error encountered in your Express app.
/google likely refers to the authentication process using a Google account or Google OAuth.
/me may refer to a user profile or information endpoint, where the user's details are retrieved.
/google/callback may refer to the callback URL that is redirected to after successfully authenticating with Google.
/logout likely refers to the action of logging out or ending the current session and authentication.