[feat] Update route format

This commit is contained in:
JasonWu
2021-09-01 15:20:53 +08:00
parent b1e9c5e62a
commit 9174b540fd
11 changed files with 152 additions and 76 deletions
+11 -5
View File
@@ -1,11 +1,11 @@
const IORedis = require('ioredis');
const config = require('src/config/index.js');
const IORedis = require("ioredis");
const config = require("src/config/index.js");
class Redis extends IORedis {
constructor() {
let { prefix } = config.redis;
const { host, port, password, db } = config.redis;
if (prefix && !/:$/.test(prefix)) prefix += ':';
if (prefix && !/:$/.test(prefix)) prefix += ":";
super({
host,
port,
@@ -23,7 +23,13 @@ class Redis extends IORedis {
* @param {string} s state
* @return {string}
*/
ssoLoginCache: s => self.getKeyWithPrefix(`sso-login:${s}`),
ssoLoginCache: (s) => self.getKeyWithPrefix(`sso-login:${s}`),
/**
* 儲存 Token
* @param {string} s state
* @return {string}
*/
userToken: (s) => self.getKeyWithPrefix(`token:${s}`),
};
}
@@ -33,7 +39,7 @@ class Redis extends IORedis {
* @return {string}
*/
getKeyWithPrefix(s) {
if (typeof s !== 'string') throw new Error('input key not a string');
if (typeof s !== "string") throw new Error("input key not a string");
return `${this.prefix}${s}`;
}
+12 -3
View File
@@ -42,6 +42,9 @@ mod.getAuthURL = state => {
return `${value.authorized_endpoint}?${querystring.stringify(qs)}`;
};
/**
* @return {string}
*/
mod.getLogoutURL = () => {
const input = joi
.object({
@@ -59,6 +62,9 @@ mod.getLogoutURL = () => {
/**
* @typedef SSOAccount
* @property {string} access_token
* @property {string} refresh_token
* @property {string} user_id
* @property {string} username
* @property {string} display_name
* @property {string} email
@@ -106,13 +112,13 @@ mod.getToken = async (code, state) => {
const { body } = resp;
if (!body) throw new Error('resopnse body empty');
const { id_token: idToken, access_token: accessToken } = body;
const { id_token: idToken, access_token: accessToken, refresh_token: refreshToken } = body;
if (!idToken) throw new Error('get id token fail');
const decoded = jwt.decode(idToken);
if (!decoded || typeof decoded !== 'object') throw new Error('jwt decode fail');
console.log(decoded)
console.log(body)
console.log('decoded ::: ', decoded)
console.log('body ::: ', body)
// @ts-ignore
const { preferred_username: preferredUsername } = decoded;
if (!preferredUsername) throw new Error('id token field missing');
@@ -121,6 +127,9 @@ mod.getToken = async (code, state) => {
/** @type {SSOAccount} */
const ssoAccount = {
access_token: accessToken,
refresh_token: refreshToken,
user_id: decoded.sub,
username: preferredUsername.toLowerCase(),
display_name: displayName ?? preferredUsername,
email: decoded.email ?? '',