npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tntd/sso-login

v1.0.1

Published

koa 统一登录中间件

Downloads

9

Readme

sso-longin koa 统一登录中间件


安装

npm i -g @tntd/sso-login 

使用

const Koa = require('koa');
const ssoLogin = require('@tntd/sso-login ');
const app = new Koa();

app.use(ssoLogin(options)); // options 为 JSON object

options

| key | 是否必填 | 描述 | | --- | --- | --- | | rsaKey | 否 | E-hr node 解密key | | logoutUrl | 否 | 退出接口,默认 '/api/logout' | | loginUrl | 是 | 登录页面地址,参考下面统一登录 | | ehrapi | 是 | 参考 ehrapi 描述 |

ehrapi 描述

ehrapi = { 
	host: "",  
	url: '/ehrapi/empinfo',  // 建议不填,除非接口地址变更了
	token: ""
}

上下文挂载用户信息

ctx.session.user = {
    sso: true,
    ehrId: '',
    badge: '',
    nickname: '',
    empStatus: '',
    email: '',
    account: '',
    depId: '',
    leaderId: '',
    roleId: ''
};

字段描述 具体类型,参考返回值

| 字段 | 类型 | 是否为空 | 说明 | | --- | --- | --- | --- | | sso | Boolean | 否 | 上下文二次挂载用户新标识,默认 true | | ehrId | int | 否 | 员工ID | | badge | String | 否 | 员工工号 | | nickname | String |否 | 员工姓名 | | empStatus | int | 否 | 在职状态:1-在职;2-离职 | | email | String | 否 | email | | account | String | 否 | AD账号 | | depId | String | 否 | 所在部门id | | leaderId | int | 否 | 直接汇报对象人员ID | | roleId | int | 否 | 在职状态:1-在职;2-离职 |

二次上下文挂载当前系统用户信息

format-session.js 中间件

const UserDao = require('daos/user');
const userDao = new UserDao();

module.exports = (options = {}) => {
	return async (ctx, next) => {
		if (ctx.session && ctx.session.user && ctx.session.user.sso) { // sso 标识
			let userOne = await userDao.findOne({
				where: {
					account: ctx.session.user.account
				}
			});// 根据域账号 获取用户信息
			if (!userOne) {// 当前系统没用找到用户,立即新增用户
				userOne = await userDao.create({ ...ctx.session.user });
			}
			ctx.session.user = {
				...userOne.dataValues,
				password: undefined
			};
		}
		await next();
	};
};
const Koa = require('koa');
const ssoLogin = require('@tntd/sso-login ');
const app = new Koa();

app.use(ssoLogin(options)); // options 为 JSON object
app.use(formatSession());// 上面的中间件 format-session.js

前端

401 跳转登录

const goToLogin = (url, params = {}) => {
    params.callbackUrl = params.callbackUrl || location.href;
    window.location.href = `${url || config.ssoLoginUrl}?tokenEncoding=true&callbackUrl=${params.callbackUrl}`;
};

退出登录

import { stringify } from 'query-string';

const logout = (params = {}) => {
	params.backUrl = params.backUrl || location.href;
	window.location.href = `/api/logout?${stringify(params)}`;
};

前后端工程分开项目

import { searchToObject } from '@/utils'; // URL format对象
import { stringify } from 'query-string';

// 统一登录 token 判断 本地开发使用,线上不会进 if
const { token, ...rest } = searchToObject(location.search);
const backUrl = `${location.protocol}//${location.host}${location.pathname}?${stringify(rest)}`;
if (token) {
    location.href = `/api/login?token=${encodeURIComponent(token)}&backUrl=${encodeURIComponent(backUrl)}`;
}