fixing route
This commit is contained in:
parent
00bc8e097b
commit
8fa8a81891
1 changed files with 20 additions and 19 deletions
39
sso.js
39
sso.js
|
|
@ -13,7 +13,7 @@ const fs = require("fs");
|
|||
const axios = require("axios");
|
||||
|
||||
const cookieName = process.env.SSO_COOKIE_NAME || "ssotoken";
|
||||
const privateKey = fs.readFileSync(`${process.cwd()}/BMA`, "utf8");
|
||||
const privateKey = fs.readFileSync(`./BMA`, "utf8");
|
||||
const signOptions = {
|
||||
issuer: "BMA corp",
|
||||
subject: "sso@bangkok.go.th",
|
||||
|
|
@ -27,12 +27,12 @@ const urlKeycloakToken = `${process.env.KC_URL}/realms/${process.env.KC_REALMS}/
|
|||
const app = express();
|
||||
app.use(cors({
|
||||
origin: "*",
|
||||
credentials: true,
|
||||
// credentials: true,
|
||||
}));
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
app.use(cookieParser());
|
||||
app.post("/signin", async (req, res) => {
|
||||
app.post("/api/v1/sso/signin", async (req, res) => {
|
||||
|
||||
try {
|
||||
const login_user = req.body;
|
||||
|
|
@ -43,15 +43,13 @@ app.post("/signin", async (req, res) => {
|
|||
formdata.append("username", login_user.username);
|
||||
formdata.append("password", login_user.password);
|
||||
|
||||
// console.log("formdata===>", formdata);
|
||||
console.log("formdata===>", formdata);
|
||||
|
||||
const response = await axios.post(urlKeycloakToken, formdata, {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
|
||||
if (response.data) {
|
||||
}).then(() => {
|
||||
const payload = { username: login_user.username };
|
||||
let token = jwt.sign(payload, privateKey, signOptions);
|
||||
|
||||
|
|
@ -62,22 +60,25 @@ app.post("/signin", async (req, res) => {
|
|||
});
|
||||
|
||||
res.sendStatus(200);
|
||||
} else {
|
||||
res.status(401).send("Incorrect user or password");
|
||||
}
|
||||
}).catch((err) => {
|
||||
if (err.status) {
|
||||
res.status(401).send("Incorrect user or password");
|
||||
} else
|
||||
res.status(err.status).send(error);
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).send("Incorrect user or password!");
|
||||
res.status(500).send(error);
|
||||
}
|
||||
});
|
||||
|
||||
// kcauth
|
||||
const useBMA = Boolean(process.env.USE_BMA) || false;
|
||||
const publicKeyLanding = fs.readFileSync(`${process.cwd()}/BMA.pub.pem`, "utf8");
|
||||
const clientSecret = process.env.KC_CLIENT_SECRET;
|
||||
const clientId = process.env.KC_CLIENT_ID;
|
||||
|
||||
app.post("/kcauth", async (req, res) => {
|
||||
app.post("/api/v1/sso/kcauth", async (req, res) => {
|
||||
try {
|
||||
// kcauth
|
||||
const useBMA = Boolean(process.env.USE_BMA) || false;
|
||||
const publicKeyLanding = fs.readFileSync(`./BMA.pub.pem`, "utf8");
|
||||
const clientSecret = process.env.KC_CLIENT_SECRET;
|
||||
const clientId = process.env.KC_CLIENT_ID;
|
||||
|
||||
const cookies = req.cookies;
|
||||
const tokenSSO = cookies[cookieName];
|
||||
|
||||
|
|
@ -142,7 +143,7 @@ app.post("/kcauth", async (req, res) => {
|
|||
} catch (error) {
|
||||
// console.log("error===>", error);
|
||||
|
||||
res.status(401).send(error);
|
||||
res.status(500).send(error);
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue