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?
1- passport-google-oauth20 is a part of Passport.js, used for integrating Google OAuth 2.0 authentication into Node.js apps. It simplifies user authentication via Google accounts by handling the authentication process, token exchange, and user profile retrieval.
2- cookieParser is middleware in Node.js to parse cookies from HTTP request headers. It populates req.cookies, easing cookie handling.
encryptCookieNodeMiddleware encrypts cookies for enhanced security, preventing tampering or unauthorized access.
3- To let an Express app use cookie middlewares such as cookieParser and encryptCookieNodeMiddleware with a secret key for handling encryption of cookies, you need to follow these steps:
Install necessary packages: You need to install cookie-parser middleware for parsing cookies and any encryption middleware you're using for encrypting cookies.
Configure middleware: Set up the middleware in your Express app to use cookieParser and any encryption middleware with your secret key.
snippets:
const express = require('express');
const cookieParser = require('cookie-parser');
const encryptCookie = require('cookie-encrypter');
const app = express();
app.use(cookieParser());
const secretKey = 'yourSecretKeyHere';
app.use(encryptCookie(secretKey));
app.get('/', (req, res) => {
// Access cookies
console.log(req.cookies);
res.send('Hello World!');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(
Server is running on port ${PORT}
);});
4- checkAuth middleware checks authentication. If not authenticated, it sends a 401 response.
Error handling middleware catches any errors and sends a 500 response.
/protected route demonstrates using checkAuth middleware. If authenticated, it sends a success response.
5- /google: Initiates Google OAuth authentication.
/me: Retrieves authenticated user information.
/google/callback: Callback URL for successful Google OAuth authentication.
/logout: Logs the user out and revokes access tokens.
Rafeef Thamer, Aras Yousef, Jwan Kareem