Skip to content

Commit

Permalink
feat(auth): implement Google auth strategy
Browse files Browse the repository at this point in the history
- Integrate Google ID into the user model for OAuth.
- Set up Google authentication strategy with Passport.
- Establish '/auth/google' and '/auth/google/callback' routes.
- Develop controller logic for handling Google OAuth.

[Finishes #187419056]
  • Loading branch information
Hakizimana-Clement committed Apr 26, 2024
1 parent 40e9cba commit b20cb8d
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 136 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ jobs:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}

DEPLOYED_URL: ${{ secrets.DEPLOYED_URL }}
CLOUDINARY_CLOUD_NAME: ${{ secrets.CLOUDINARY_CLOUD_NAME }}
CLOUDINARY_API_KEY: ${{ secrets.CLOUDINARY_API_KEY }}
CLOUDINARY_API_SECRET: ${{ secrets.CLOUDINARY_API_SECRET }}
CLOUDINARY_FOLDER_NAME: ${{ secrets.CLOUDINARY_FOLDER_NAME }}

GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
GOOGLE_SECRET_ID: ${{ secrets.GOOGLE_SECRET_ID }}
GOOGLE_CALLBACK_URL: ${{ secrets.GOOGLE_CALLBACK_URL }}

strategy:
matrix:
node-version: ["20.x"]
Expand Down
112 changes: 96 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"check": "prettier . --check",
"prepare": "husky",
"precommit": "prettier . --write && eslint --fix .",
"test": "jest --no-cache --detectOpenHandles",
"test": "jest --no-cache --detectOpenHandles --verbose",
"test-test": "jest --watchAll --no-cache --detectOpenHandles",
"test:ci": "jest --detectOpenHandles --coverage --verbose"
},
Expand All @@ -26,19 +26,21 @@
"dependencies": {
"@types/jsonwebtoken": "^9.0.6",
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"express-session": "^1.18.0",
"joi": "^17.12.3",
"jsonwebtoken": "^9.0.2",
"passport": "^0.5.0",
"passport": "^0.5.3",
"passport-google-oauth20": "^2.0.0",
"passport-local": "^1.0.0",
"passport-stub": "^1.1.1",
"pg": "^8.11.5",
"pg-hstore": "^2.3.4",
"reflect-metadata": "^0.2.2",
"sequelize": "^6.37.3",
"uuid": "^9.0.0"
"uuid": "^9.0.1"
},
"jest": {
"preset": "ts-jest",
Expand All @@ -52,7 +54,9 @@
"lcov"
],
"coveragePathIgnorePatterns": [
"/src/database/config/db.config.ts"
"/src/database/config/db.config.ts",
"/src/utils/token.validation.ts",
"/src/utils/database.utils.ts"
]
},
"devDependencies": {
Expand All @@ -65,6 +69,7 @@
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "^20.12.7",
"@types/passport": "^1.0.16",
"@types/passport-google-oauth20": "^2.0.14",
"@types/passport-local": "^1.0.38",
"@types/pg": "^8.11.5",
"@types/supertest": "^6.0.2",
Expand Down
59 changes: 59 additions & 0 deletions src/__test__/google.auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import app from "../app";
import request from "supertest";
import { connectionToDatabase } from "../database/config/db.config";
import { deleteTableData } from "../utils/database.utils";
import { User } from "../database/models/User";
import { google_profile } from "../mock/static";
jest.setTimeout(30000);

function logErrors(
err: { stack: any },
_req: any,
_res: any,
next: (arg0: any) => void,
) {
console.log(err.stack);
next(err);
}

const Jest_request = request(app.use(logErrors));

describe("GOOGLE API TEST", () => {
beforeAll(async () => {
await connectionToDatabase();
});

afterAll(async () => {
await deleteTableData(User, "users");
});

it("should simulate Google oAuth flow and return 302", async () => {
const response = await Jest_request.get("/api/v1/users/auth/google").expect(
302,
);
expect(response.header.location).toStrictEqual(
"/api/v1/users/auth/google/callback",
);
expect(response.redirect).toBeTruthy();
expect(response.header["set-cookie"]).toBeDefined();
});

it("should handle google oauth callback and display user data to log in or to create user", async () => {
const response = await Jest_request.get(
"/api/v1/users/auth/google/callback",
).query(google_profile);
expect(response.status).toBe(302);

const google_url = response.header.location;

expect(google_url).toBeDefined();
});

it("Welcome to Hacker's e-commerce backend and return 200", async () => {
const { body } = await Jest_request.get("/").expect(200);
});

it("should display login home page and return 200", async () => {
const { body } = await Jest_request.get("/api/v1/").expect(200);
});
});
15 changes: 0 additions & 15 deletions src/__test__/product.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/__test__/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ describe("USER API TEST", () => {
expect(body.message).toStrictEqual("Account Created successfully!");
expect(body.token).toBeDefined();
});

it("it should return a user not found and status 400", async () => {
const { body } = await Jest_request.post("/api/v1/users/register")
.send(user_bad_request)
Expand Down
Loading

0 comments on commit b20cb8d

Please sign in to comment.