This commit is contained in:
seb
2026-07-31 18:25:42 +02:00
parent 1a05a5b7d6
commit 6074aa7304
33 changed files with 3041 additions and 470 deletions

View File

@@ -12,14 +12,64 @@ const googleClient = new OAuth2Client(env.googleClientId);
*/
export function register(app, basePath = '') {
/**
* @route POST /auth/login
* @description Verifies a Google ID token, checks allowlist (or creates entry when `IMPLICIT_CREATE=true`), and sets an HTTP-only `token` cookie.
* @body {string} idToken - Google ID token from Google Identity Services
* @returns {Object} 200 - `{ message, user }` with `{ id, email, selectedCompanyId, picture? }` in `user`
* @returns {Object} 400 - Missing token or Google client not configured
* @returns {Object} 401 - Invalid Google token
* @returns {Object} 403 - Email not on allowlist
* @returns {Object} 500 - Server error
* @openapi
* /auth/login:
* post:
* tags: [Auth]
* summary: Login with Google ID token
* description: Verifies a Google ID token, checks allowlist (or creates entry when `IMPLICIT_CREATE=true`), and sets an HTTP-only `token` cookie.
* security: []
* x-markdown: auth/login.md
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required: [idToken]
* properties:
* idToken:
* type: string
* description: Google ID token from Google Identity Services
* example:
* idToken: "<google-id-token>"
* responses:
* 200:
* description: Login successful; `token` cookie set
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: Login successful
* user:
* $ref: '#/components/schemas/User'
* 400:
* description: Missing token or Google client not configured
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 401:
* description: Invalid Google token or account
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 403:
* description: Email not on allowlist
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
app.post(`${basePath}/auth/login`, async (req, res) => {
try {

View File

@@ -9,9 +9,25 @@ import { clearCachedUser, clearTokenCookie } from '../../middleware/auth.js';
*/
export function register(app, basePath = '') {
/**
* @route POST /auth/logout
* @description Clears the HTTP-only `token` cookie set at login.
* @returns {Object} 200 - `{ message }` confirming logout
* @openapi
* /auth/logout:
* post:
* tags: [Auth]
* summary: Logout
* description: Clears the HTTP-only `token` cookie set at login.
* security: []
* x-markdown: auth/logout.md
* responses:
* 200:
* description: Logged out
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: Logged out successfully
*/
app.post(`${basePath}/auth/logout`, (req, res) => {
const token = req.cookies.token;

View File

@@ -7,9 +7,38 @@ import { meJson } from '../../auth/meJson.js';
*/
export function register(app, basePath = '') {
/**
* @route GET /auth/me
* @description Returns the authenticated user from `req.user` and the Google client ID.
* @returns {Object} 200 - `{ user: { id, email, selectedCompanyId, picture? }, googleClientId }`
* @openapi
* /auth/me:
* get:
* tags: [Auth]
* summary: Current user
* description: Returns the authenticated user from the session cookie and the Google client ID. Unauthenticated calls still receive `googleClientId` with 401.
* security:
* - cookieAuth: []
* x-markdown: auth/me.md
* responses:
* 200:
* description: Authenticated
* content:
* application/json:
* schema:
* type: object
* properties:
* user:
* $ref: '#/components/schemas/User'
* googleClientId:
* type: string
* 401:
* description: Unauthorized — still includes `googleClientId` for the frontend
* content:
* application/json:
* schema:
* type: object
* properties:
* error:
* type: string
* googleClientId:
* type: string
*/
app.get(`${basePath}/auth/me`, (req, res) => {
const { id, email, selectedCompanyId, picture } = req.user;

View File

@@ -8,10 +8,56 @@ import { clearCachedUser } from '../../middleware/auth.js';
*/
export function register(app, basePath = '') {
/**
* @route PUT /auth/selected-company
* @description Sets the authenticated user's selected company.
* @body {number} companyId
* @returns {Object} 200 - `{ user: { id, email, selectedCompanyId, picture? } }`
* @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);