REFACTOR: Adicionado o parametro source no post para a API, e refatorado callback de autenticação.

This commit is contained in:
Gabriel Amancio 2026-01-09 08:52:15 -03:00
parent 2542bb1f44
commit e5b530f9a4
2 changed files with 14 additions and 7 deletions

View File

@ -9,6 +9,7 @@ const path = require('path');
async function consultarViabilidadeController(req, res) { async function consultarViabilidadeController(req, res) {
try { try {
const data = req.body; const data = req.body;
data.source = 'viabiliza.sothis.com.br';
const result = await consultarViabilidade(data); const result = await consultarViabilidade(data);
res.json(result); res.json(result);
} catch (error) { } catch (error) {

View File

@ -1,14 +1,15 @@
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const dotenv = require('dotenv');
dotenv.config();
/** /**
* Página simples de login * Página simples de login
*/ */
router.get('/login', (req, res) => { router.get('/login', (req, res) => {
res.send(` // Redirect straight to Microsoft OAuth start to avoid an extra click/page
<h2>Login necessário</h2> return res.redirect('/auth/microsoft');
<a href="/auth/microsoft">Entrar com Microsoft</a>
`);
}); });
/** /**
@ -31,7 +32,8 @@ router.get('/auth/microsoft', (req, res) => {
/** /**
* Callback do Azure * Callback do Azure
*/ */
router.get('/auth/microsoft/callback', async (req, res) => { // shared handler so we accept both /auth/microsoft/callback and /auth/callback
async function oauthCallbackHandler(req, res) {
const code = req.query.code; const code = req.query.code;
// Verbose logging for debugging the OAuth callback flow // Verbose logging for debugging the OAuth callback flow
console.log('[OAuth callback] incoming query:', { console.log('[OAuth callback] incoming query:', {
@ -50,7 +52,7 @@ router.get('/auth/microsoft/callback', async (req, res) => {
console.log('[OAuth callback] exchanging code for tokens (will not log secrets)'); console.log('[OAuth callback] exchanging code for tokens (will not log secrets)');
const tokenRespRaw = await fetch( const tokenRespRaw = await fetch(
`https://login.microsoftonline.com/${process.env.AZURE_TENANT_ID}/oauth2/v2.0/token`, `https://login.microsoftonline.com/${process.env.OAUTH_TENANT_ID}/oauth2/v2.0/token`,
{ {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
@ -113,7 +115,11 @@ router.get('/auth/microsoft/callback', async (req, res) => {
console.error('[OAuth callback] unexpected error during token exchange or session creation:', err && (err.stack || err.message || err)); console.error('[OAuth callback] unexpected error during token exchange or session creation:', err && (err.stack || err.message || err));
res.redirect('/login'); res.redirect('/login');
} }
}); }
router.get('/auth/microsoft/callback', oauthCallbackHandler);
// some Azure app registrations (or tooling like ngrok) may use /auth/callback — accept that too
router.get('/auth/callback', oauthCallbackHandler);
router.get('/logout', (req, res) => { router.get('/logout', (req, res) => {
req.session.destroy(() => res.redirect('/login')); req.session.destroy(() => res.redirect('/login'));