keycloak-demo/controllers/account/index.js

42 lines
1003 B
JavaScript
Raw Normal View History

2021-09-01 11:30:21 +00:00
const { resp } = require("src/utils/response/index.js");
const { get: getCacheInstance } = require("src/utils/cache.js");
const sso = require("src/utils/sso/index.js");
const { OPENID_EXPIRE } = require("src/constants/index.js");
const uuid = require("uuid");
const url = require("url");
2021-08-31 10:24:42 +00:00
const controller = {};
module.exports = controller;
2021-09-01 11:30:21 +00:00
controller.loginSSO = () => async (ctx) => {
2021-08-31 10:24:42 +00:00
const { back_url: backURL } = ctx.query;
const state = uuid.v4();
const authURL = sso.getAuthURL(state);
// store back url to cache
2021-09-01 11:30:21 +00:00
const cacheKey = `login-${state}`;
const cache = getCacheInstance();
2021-08-31 10:24:42 +00:00
2021-09-01 11:30:21 +00:00
cache.set(cacheKey, JSON.stringify({ back_url: backURL }), true);
2021-08-31 10:24:42 +00:00
const u = new url.URL(authURL);
ctx.resp(resp.Success, { url: u.toString() });
};
2021-09-01 07:20:53 +00:00
2021-09-01 11:30:21 +00:00
controller.logout = () => async (ctx) => {
let link = "";
2021-09-01 07:20:53 +00:00
if (ctx.token.sso) {
link = sso.getLogoutURL();
}
ctx.resp(resp.Success, { url: link });
};
2021-09-01 11:30:21 +00:00
controller.getInfo = () => async (ctx) => {
2021-09-01 07:20:53 +00:00
ctx.resp(resp.Success, {});
2021-09-01 11:30:21 +00:00
};