Skip to content

Commit

Permalink
log the user out if their acess token cant be renewed. lower session age
Browse files Browse the repository at this point in the history
  • Loading branch information
ajluker committed Sep 23, 2024
1 parent 77d5c94 commit 8a9a56a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function createApp() {
cookie: {
secure: true, // Set to true if you're using HTTPS
httpOnly: true, // Ensures the cookie is only accessible via HTTP/HTTPS
maxAge: 1000 * 60 * 60 * 24 * 7, // Sets cookie to expire in 7 days,
maxAge: 1000 * 60 * 60 * 8, // Sets cookie to expire in 8 hours,
sameSite: 'none' // Can be 'strict', 'lax', 'none', or boolean (true)
}
})
Expand Down
10 changes: 10 additions & 0 deletions web/modules/authentication/getNewAccessToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export async function obtainValidAccessToken(userId) {
})
}

export async function obtainValidAccessTokenOrDeleteSessionOnFailure(req) {
try {
const {accessToken} = await obtainValidAccessToken(req.user.id);
return accessToken;
} catch(error) {
req.session.destroy();
throw error;
}
}

async function refresh(refreshToken) {
const issuer = await Issuer.discover(process.env.OIDC_ISSUER);

Expand Down
4 changes: 2 additions & 2 deletions web/modules/products/controllers/get-fdc-products.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios';
import dotenv from 'dotenv';
import { join } from 'path';
import { generateShopifyFDCProducts } from '../../../connector/productUtils.js';
import { obtainValidAccessToken } from '../../authentication/getNewAccessToken.js';
import { obtainValidAccessTokenOrDeleteSessionOnFailure } from '../../authentication/getNewAccessToken.js';

dotenv.config({
path: join(process.cwd(), '.env')
Expand All @@ -14,7 +14,7 @@ const getFDCProducts = async (req, res, next) => {
// const { sinceId, remainingProductsCountBeforeNextFetch } = req.query;

try {
const { accessToken } = await obtainValidAccessToken(req.user.id);
const accessToken = await obtainValidAccessTokenOrDeleteSessionOnFailure(req);

const { data } = await axios.get(
`${PRODUCER_SHOP_URL}api/dfc/Enterprises/${PRODUCER_SHOP}/SuppliedProducts`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getMostRecentActiveSalesSession, deactivateAllSalesSessions } from '../../../database/sales-sessions/salesSession.js';
import { completeOrder } from '../../producer-orders/order.js';
import {obtainValidAccessToken} from '../../authentication/getNewAccessToken.js'
import {obtainValidAccessTokenOrDeleteSessionOnFailure} from '../../authentication/getNewAccessToken.js'
const completeCurrentSalesSession = async (req, res, next) => {
try {
const currentSalesSession = await getMostRecentActiveSalesSession();

if (currentSalesSession.orderId) {
const {accessToken} = await obtainValidAccessToken(req.user.id);
const accessToken = await obtainValidAccessTokenOrDeleteSessionOnFailure(req);
await completeOrder(currentSalesSession, accessToken);
}

Expand Down

0 comments on commit 8a9a56a

Please sign in to comment.