TEST: Subindo configuração de autenticação pelo office para teste.
This commit is contained in:
parent
40cc76fa54
commit
46d66f9c1b
2
.env
2
.env
@ -7,6 +7,6 @@ PORT="3000"
|
|||||||
OAUTH_CLIENT_ID=e2104cd1-d67c-4ac1-8fe2-36e8caac89b7
|
OAUTH_CLIENT_ID=e2104cd1-d67c-4ac1-8fe2-36e8caac89b7
|
||||||
OAUTH_CLIENT_SECRET=sVj8Q~eSXJpnQoqjvpOwjYeesVf_DJNRqTa4ua-6
|
OAUTH_CLIENT_SECRET=sVj8Q~eSXJpnQoqjvpOwjYeesVf_DJNRqTa4ua-6
|
||||||
OAUTH_TENANT_ID=5cd8533a-4260-48c5-87fd-8511b1b42f9b
|
OAUTH_TENANT_ID=5cd8533a-4260-48c5-87fd-8511b1b42f9b
|
||||||
OAUTH_REDIRECT_URI=http://localhost:3000/auth/callback
|
OAUTH_REDIRECT_URI=http://viabiliza.sothis.com.br/auth/callback
|
||||||
OAUTH_SCOPES=https://graph.microsoft.com/.default
|
OAUTH_SCOPES=https://graph.microsoft.com/.default
|
||||||
SESSION_SECRET=j2633669
|
SESSION_SECRET=j2633669
|
||||||
18
app.js
18
app.js
@ -537,6 +537,24 @@ function createApp() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// Servir arquivos estáticos (index.html)
|
||||||
|
app.use("/public", express.static(path.join(__dirname, "src/public")));
|
||||||
|
|
||||||
|
// Usa as rotas de autenticação
|
||||||
|
app.use("/", authRoutes);
|
||||||
|
|
||||||
|
// Middleware para proteger rotas
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
if (!req.session.user && req.path !== "/login" && !req.path.startsWith("/auth")) {
|
||||||
|
return res.redirect("/login");
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////
|
||||||
|
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
35
routes/authRoutes.js
Normal file
35
routes/authRoutes.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import express from "express";
|
||||||
|
import {getAuthUrl, getTokenFomCode} from "../service/authService.js";
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
// Rota para iniciar o fluxo de autenticação
|
||||||
|
|
||||||
|
router.get("/login", (req, res) => {
|
||||||
|
const authUrl = getAuthUrl();
|
||||||
|
return res.redirect(authUrl);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// Rota de callback após autenticação
|
||||||
|
|
||||||
|
router.get("/auth/callback", async (req, res) => {
|
||||||
|
const code = req.query.code;
|
||||||
|
|
||||||
|
if (!code) {
|
||||||
|
return res.status(400).send("Código de autenticação não fornecido.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const tokens = await getTokenFomCode(code);
|
||||||
|
// Armazena os tokens na sessão do usuário
|
||||||
|
req.session.tokens = tokens;
|
||||||
|
return res.redirect("/public/index.html");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Erro ao obter tokens:", error);
|
||||||
|
return res.status(500).send("Erro ao processar a autenticação.");
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
export default router;
|
||||||
Loading…
Reference in New Issue
Block a user