keycloak-demo/controllers/account/index.js

41 lines
932 B
JavaScript

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 uuid = require('uuid')
const url = require('url')
const controller = {}
module.exports = controller
controller.loginSSO = () => async (ctx) => {
const { back_url: backURL } = ctx.query
const state = uuid.v4()
const authURL = sso.getAuthURL(state)
// store back url to cache
const cacheKey = `login-${state}`
const cache = getCacheInstance()
cache.set(cacheKey, JSON.stringify({ back_url: backURL }), true)
const u = new url.URL(authURL)
ctx.resp(resp.Success, { url: u.toString() })
}
controller.logout = () => async (ctx) => {
let link = ''
if (ctx.token.sso) {
link = sso.getLogoutURL()
}
ctx.resp(resp.Success, { url: link })
}
controller.getInfo = () => async (ctx) => {
ctx.resp(resp.Success, ctx.token.info)
}