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?
Ibrahim Muhaned, Mardin Luqman, Mawj M.Basheer , Elaf Gardi
1- passport-google-oauth20 is a Node.js module for easily adding Google OAuth 2.0 authentication to your application. It simplifies the process of integrating Google authentication into Node.js apps by handling the OAuth flow and providing a configurable strategy for authentication.
2-cookieParser is a middleware for Express.js that parses cookies attached to the client's request object. It populates req.cookies with an object keyed by the cookie names. encryptCookieNodeMiddleware, as the name suggests, is likely a custom middleware used for encrypting cookies for enhanced security.
3- To let an Express app use cookie middlewares like cookieParser and encryptCookieNodeMiddleware with a secret key for handling encryption of cookies, you need to do the following:
A. Install the necessary middleware packages:
npm install cookie-parser cookie-encrypter
B. Require the middleware packages in your Express app:
const express = require('express');
const cookieParser = require('cookie-parser');
const encryptCookieNodeMiddleware = require('cookie-encrypter');
C. Set up the cookie parser middleware with a secret key:
const app = express();
app.use(cookieParser('your-secret-key'));
D. Set up the cookie encryption middleware with the same secret key:
app.use(encryptCookieNodeMiddleware('your-secret-key'));
4- It will be better to return 401 - Unauthorized if the token is invalid or if missing the header by default. We can also provide a fail callback, so the user can customize like this:
app.use(express.jwt({
fail: function (req, res, next) {
if (!req.headers.authorization) res.send(400, 'missing authorization header');
res.send(401);
}
}));
5-
A.
/google
: This path is likely the route for initiating the Google OAuth 2.0 authentication process. When a user accesses this route, they are redirected to Google's authentication page where they can sign in with their Google account.B.
/me
: This path might be used to retrieve information about the currently authenticated user. Once a user is authenticated through Google OAuth, they might be redirected to this route to display their profile or perform other authenticated actions.C.
/google/callback
: This path is typically the callback URL used by the OAuth 2.0 authentication process. After the user has authenticated with Google, Google redirects the user back to this URL along with an authorization code. The server then exchanges this code for an access token.D.
/logout
: This path is likely used to log the user out of the application. When a user accesses this route, their session is terminated, and they are redirected to a logged-out state or another page, such as the home page.