Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: auto logout

Nevo David3084fb425512
diff --git a/apps/backend/src/services/auth/auth.middleware.ts b/apps/backend/src/services/auth/auth.middleware.ts
index cdf6aac63..df1c7514c 100644
--- a/apps/backend/src/services/auth/auth.middleware.ts
+++ b/apps/backend/src/services/auth/auth.middleware.ts
@@ -4,6 +4,16 @@ import { AuthService } from '@gitroom/helpers/auth/auth.service';
import { User } from '@prisma/client';
import { OrganizationService } from '@gitroom/nestjs-libraries/database/prisma/organizations/organization.service';
import { UsersService } from '@gitroom/nestjs-libraries/database/prisma/users/users.service';
+import { removeSubdomain } from '@gitroom/helpers/subdomain/subdomain.management';
+
+const removeAuth = (res: Response) =>
+ res.cookie('auth', '', {
+ domain: '.' + new URL(removeSubdomain(process.env.FRONTEND_URL!)).hostname,
+ secure: true,
+ httpOnly: true,
+ sameSite: 'none',
+ expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365),
+ });
@Injectable()
export class AuthMiddleware implements NestMiddleware {
@@ -14,14 +24,16 @@ export class AuthMiddleware implements NestMiddleware {
async use(req: Request, res: Response, next: NextFunction) {
const auth = req.headers.auth || req.cookies.auth;
if (!auth) {
- throw new Error('Unauthorized');
+ removeAuth(res);
+ res.status(401).send('Unauthorized');
}
try {
let user = AuthService.verifyJWT(auth) as User | null;
const orgHeader = req.cookies.showorg || req.headers.showorg;
if (!user) {
- throw new Error('Unauthorized');
+ removeAuth(res);
+ res.status(401).send('Unauthorized');
}
if (user?.isSuperAdmin && req.cookies.impersonate) {
@@ -39,12 +51,15 @@ export class AuthMiddleware implements NestMiddleware {
req.user = user;
// @ts-ignore
- loadImpersonate.organization.users = loadImpersonate.organization.users.filter(f => f.userId === user.id);
+ loadImpersonate.organization.users =
+ loadImpersonate.organization.users.filter(
+ (f) => f.userId === user.id
+ );
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
req.org = loadImpersonate.organization;
next();
- return ;
+ return;
}
}
@@ -63,7 +78,8 @@ export class AuthMiddleware implements NestMiddleware {
// @ts-expect-error
req.org = setOrg;
} catch (err) {
- throw new Error('Unauthorized');
+ removeAuth(res);
+ res.status(401).send('Unauthorized');
}
next();
}