Skip to content

Commit 5da7c62

Browse files
authored
Merge pull request #17 from flycatch/fix/phase-3
fix(oauth): upadate oauth2 workflow to fix cookie cors issue
2 parents 4d0fe27 + 4c386af commit 5da7c62

File tree

10 files changed

+164
-109
lines changed

10 files changed

+164
-109
lines changed

README.md

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,19 @@ app.use(
6767
prefix: "/auth",
6868
successRedirect: "http://localhost:3000/oauth-success",
6969
failureRedirect: "http://localhost:3000/oauth-failure",
70-
autoProvision: true,
7170
defaultRole: "ROLE_USER",
72-
setRefreshCookie: true,
73-
appendTokensInRedirect: false,
74-
includeAuthorities: true,
75-
issueJwt: true,
71+
onSuccess(info)=>{
72+
const {profile, existingUser,} =info;
73+
if(existingUser){
74+
return existingUser;
75+
}
76+
77+
// Logic to create new user
78+
reateUser(profile)
79+
},
80+
onfailure(info)=>{
81+
// Logic to be executed onFailure
82+
},
7683
providers: {
7784
google: {
7885
clientID: "GOOGLE_CLIENT_ID",
@@ -82,15 +89,6 @@ app.use(
8289
},
8390
},
8491
},
85-
cookies: {
86-
enabled: true,
87-
name: "AuthRefreshToken",
88-
httpOnly: true,
89-
secure: false,
90-
sameSite: "Strict",
91-
maxAge: 7 * 24 * 60 * 60 * 1000,
92-
path: "/",
93-
},
9492
twoFA: {
9593
enabled: false,
9694
prefix: "/auth/2fa",
@@ -250,7 +248,6 @@ oauth2: {
250248
- **defaultRole**: Default role assigned to new users.
251249
- **providers**: Supported providers (e.g., Google, GitHub).
252250

253-
254251
### **Two-Factor Authentication**
255252

256253
```javascript
@@ -334,6 +331,7 @@ All endpoints use the configured prefix. Default prefixes shown below:
334331
- **GET** `/auth/{provider}` - Initiate OAuth login
335332
- **GET** `/auth/{provider}/callback` - OAuth callback
336333
- **GET** `/auth/error` - OAuth error redirect
334+
- **POST** `/auth/token` - to get token from temporary code
337335

338336
### **Two-Factor Authentication**
339337

@@ -368,6 +366,15 @@ All endpoints use the configured prefix. Default prefixes shown below:
368366
3. Server processes authentication and auto-creates user if add any logic onSuccess.
369367
4. Server redirects to success URL with tokens as cookies.
370368
5. Subsequent requests use JWT or session authentication.
369+
6. After successful provider authentication, the temporary code will be set as a query parameter on the redirect URL.
370+
7. Frontend can then trigger `{prefix}/token` with a `POST` request and payload:
371+
372+
```json
373+
{
374+
"code": "code-from-redirect-url"
375+
}
376+
```
377+
371378

372379
## Logout Behavior
373380

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@flycatch/auth-core",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "A unified authentication module for Express.js, NestJS frameworks, supporting JWT, session-based, and Google OAuth login.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextFunction, Request, Response, Router } from "express";
22
import { Config } from "./interfaces/config.interface";
33
import express from "express";
4-
import createLogger from "./lib/wintson.logger";
4+
import createLogger from "./lib/winston.logger";
55
import jwtRoutes from "./routes/jwt.routes";
66
import sessionRoutes from "./routes/session.routes";
77
import setupSession from "./config/session.config";

src/middlewares/jwt.middleware.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import { Request, Response, NextFunction } from "express";
33
import jwt from "jsonwebtoken";
44
import { Config } from "../interfaces/config.interface";
5-
import createLogger from "../lib/wintson.logger";
6-
import { isTokenBlacklisted } from "../routes/jwt.routes";
5+
import createLogger from "../lib/winston.logger";
6+
import { isInBlacklist } from "../utils/jwt-blacklist";
77

88
/**
99
* Express middleware for validating JWT access tokens.
@@ -16,7 +16,7 @@ import { isTokenBlacklisted } from "../routes/jwt.routes";
1616
* @returns {import("express").RequestHandler} Express middleware function.
1717
*/
1818
export default (config: Config) => {
19-
return (req: Request, res: Response, next: NextFunction) => {
19+
return async (req: Request, res: Response, next: NextFunction) => {
2020
const logger = createLogger(config);
2121

2222
if (!config.jwt) {
@@ -51,8 +51,10 @@ export default (config: Config) => {
5151
});
5252
}
5353

54+
const isBlackisted = await isInBlacklist(token)
55+
5456
// Check if token is blacklisted (only if blacklisting is enabled)
55-
if (config.jwt.tokenBlacklist?.enabled && isTokenBlacklisted(token)) {
57+
if (config.jwt.tokenBlacklist?.enabled && isBlackisted) {
5658
logger.warn("JWT middleware: Blacklisted token used");
5759
return res.status(401).json({
5860
error: "Unauthorized",

src/middlewares/session.middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextFunction, Request, Response } from "express";
22
import { Config } from "../interfaces/config.interface";
3-
import createLogger from "../lib/wintson.logger";
3+
import createLogger from "../lib/winston.logger";
44

55
/**
66
* Express middleware for validating user sessions.

src/routes/jwt.routes.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Request, Response, Router } from "express";
33
import { Config } from "../interfaces/config.interface";
44
import jwt from "jsonwebtoken";
55
import express from "express";
6-
import createLogger from "../lib/wintson.logger";
6+
import createLogger from "../lib/winston.logger";
77
import apiResponse from "../utils/api-response";
88
import { createJwtTokens } from "../utils/jwt";
99
import twoFactorAuth, {
@@ -15,35 +15,8 @@ import {
1515
setBlacklistStorage,
1616
} from "../utils/jwt-blacklist";
1717

18-
/**
19-
* In-memory token blacklist
20-
* Used only if no custom storage is configured
21-
*/
22-
const tokenBlacklist = new Set<string>();
23-
24-
/**
25-
* Add a token to the in-memory blacklist
26-
* @param token JWT token string
27-
*/
28-
export const blacklistToken = (token: string): void => {
29-
tokenBlacklist.add(token);
30-
};
3118

32-
/**
33-
* Check if a token exists in the in-memory blacklist
34-
* @param token JWT token string
35-
* @returns boolean indicating if token is blacklisted
36-
*/
37-
export const isTokenBlacklisted = (token: string): boolean => {
38-
return tokenBlacklist.has(token);
39-
};
4019

41-
/**
42-
* Clear all tokens from the in-memory blacklist
43-
*/
44-
export const clearBlacklist = (): void => {
45-
tokenBlacklist.clear();
46-
};
4720

4821
/**
4922
* JWT Routes

0 commit comments

Comments
 (0)