keycloak-demo/controllers/account/index.js

41 lines
932 B
JavaScript
Raw Normal View History

2021-09-01 12:46:41 +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 uuid = require('uuid')
const url = require('url')
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
const controller = {}
module.exports = controller
2021-08-31 10:24:42 +00:00
2021-09-01 11:30:21 +00:00
controller.loginSSO = () => async (ctx) => {
2021-09-01 12:46:41 +00:00
const { back_url: backURL } = ctx.query
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
const state = uuid.v4()
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
const authURL = sso.getAuthURL(state)
2021-08-31 10:24:42 +00:00
// store back url to cache
2021-09-01 12:46:41 +00:00
const cacheKey = `login-${state}`
const cache = getCacheInstance()
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
cache.set(cacheKey, JSON.stringify({ back_url: backURL }), true)
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
const u = new url.URL(authURL)
2021-08-31 10:24:42 +00:00
2021-09-01 12:46:41 +00:00
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) => {
2021-09-01 12:46:41 +00:00
let link = ''
2021-09-01 07:20:53 +00:00
if (ctx.token.sso) {
2021-09-01 12:46:41 +00:00
link = sso.getLogoutURL()
2021-09-01 07:20:53 +00:00
}
2021-09-01 12:46:41 +00:00
ctx.resp(resp.Success, { url: link })
}
2021-09-01 07:20:53 +00:00
2021-09-01 11:30:21 +00:00
controller.getInfo = () => async (ctx) => {
2021-09-01 13:15:26 +00:00
ctx.resp(resp.Success, ctx.token.info)
2021-09-01 12:46:41 +00:00
}