Files
dashback/src/api/routes/auth/selectedCompany.js
seb fc2b136c46 Store Google avatar bytes and slim the session JWT to id only.
Download profile images on login into picturebinary, return them as base64 from /auth/me, and resolve sessions via getById.
2026-08-05 11:12:36 +02:00

86 lines
2.8 KiB
JavaScript

import * as db from '../../../db/googleAuth.js';
import { clearCachedUser } from '../../middleware/auth.js';
/**
* Registers the selected-company route (requires auth middleware).
* @param {import('express').Application} app - Express app
* @param {string} [basePath=''] - Optional URL prefix for all routes in this module
*/
export function register(app, basePath = '') {
/**
* @openapi
* /auth/selected-company:
* put:
* tags: [Auth]
* summary: Set selected company
* description: Sets the authenticated user's selected company.
* security:
* - cookieAuth: []
* x-markdown: auth/selected-company.md
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [companyId]
* properties:
* companyId:
* type: integer
* minimum: 1
* example:
* companyId: 1
* responses:
* 200:
* description: Updated user
* content:
* application/json:
* schema:
* type: object
* properties:
* user:
* $ref: '#/components/schemas/User'
* 400:
* description: Invalid companyId
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Unauthorized
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.put(`${basePath}/auth/selected-company`, async (req, res) => {
const companyId = Number(req.body?.companyId);
if (!Number.isInteger(companyId) || companyId <= 0) {
return res.status(400).json({ error: 'companyId is required' });
}
try {
await db.setSelectedCompany(req.user.id, companyId);
clearCachedUser(req.user.id);
const updated = await db.getAllowedEmail(req.user.email);
const user = {
id: updated.id,
email: updated.email,
selectedCompanyId: updated.selectedCompanyId,
};
res.status(200).json({ user });
} catch (error) {
console.error('Set selected company failed:', error);
res.status(500).json({ error: 'Failed to set selected company' });
}
});
}