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?
Papula Azad | Afeaa Khudur | Yousra Yaarob | Hana Abdulla
a module that allows Node.js applications to authenticate with Google using the OAuth 2.0 API. It can be integrated into any application or framework that supports Connect-style middleware, including Express.
cookieParser is used to parse cookies from incoming HTTP requests in Express.js, while encryptCookieNodeMiddleware encrypts cookies for enhanced security. Together, they provide a secure and efficient way to handle cookies in web applications.
const express = require('express');
const cookieParser = require('cookie-parser');
const encryptCookieNodeMiddleware = require('encrypt-cookie-node');
const app = express();
const secretKey = 'yourSecretKeyHere'; // Replace this with your actual secret key
// Middleware to parse cookies
app.use(cookieParser());
// Middleware to encrypt cookies
app.use(encryptCookieNodeMiddleware(secretKey));
// Example route to set a cookie
app.get('/set-cookie', (req, res) => {
res.cookie('user', 'john', { signed: true }); // Use signed cookie
res.send('Cookie has been set');
});
// Example route to get a cookie
app.get('/get-cookie', (req, res) => {
console.log('Signed Cookies: ', req.signedCookies); // Access signed cookie
console.log('Cookies: ', req.cookies); // Access regular cookie
res.send('Check the console for cookies');
});
// Start the server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
/google: Initiates the authentication process using Google OAuth.
/me: Retrieves the authenticated user's profile information.
/google/callback: Handles the callback from Google after authentication.
/logout: Logs the user out of the application.