@deppon/deppon-auth

Login and permission management package
18,162
Install
npm install @deppon/deppon-auth
Latest Version:2.5.105
Vue:3
License:MIT
Last Updated:Sep 11, 2026
Links: npm
Maintainer: wangyang1103

@deppon/deppon-auth

登录与权限管理包,提供登录守卫、路由解析,以及京 ME(JingME)客户端授权能力。

功能特性

  • ✅ 登录守卫(AuthGuard):基于 Cookie 的登录状态检测和路由守卫
  • ✅ 京 ME 授权:检测是否在京 ME 内,并完成 requestAuthCode 授权(兼容 jm-clues)
  • ✅ 双模式兼容:authMode: 'cookie' | 'jingme' | 'auto'
  • ✅ 路由解析器(RouteResolver):将菜单树转换为 Vue Router 路由配置
  • ✅ Token 解析工具:提供 Cookie 读取和 Base64 解码工具函数
  • ✅ TypeScript 支持:完整的类型定义
  • ✅ 轻量级:只包含核心逻辑,不包含业务状态管理

安装

npm install @deppon/deppon-auth

京 ME 场景建议同时安装 SDK(可选 peer):

npm install @jd/jme-sdk-adapter

使用方式

1. 登录守卫(AuthGuard)

Cookie 登录(原有能力,默认)
import { createDepponRouter } from '@deppon/deppon-router';
import { createAuthGuard } from '@deppon/deppon-auth';

const router = createDepponRouter({
    routes,
    guards: {
        beforeEach: createAuthGuard({
            cookieKeys: ['TGC', '_TOKENUUMS', 'U'],
            checkLogin: cookies => !!(cookies.TGC && cookies._TOKENUUMS),
            onRequireLogin: () => {
                window.location.href = BASE_API + 'login/login.action';
            },
            whiteList: ['/login', '/404'],
        }),
    },
});
京 ME / Cookie 自动兼容
import JMESDK from '@jd/jme-sdk-adapter';
import { createAuthGuard, isInJingme } from '@deppon/deppon-auth';

createAuthGuard({
    // auto:在京 ME 内走京 ME 授权,否则走 Cookie 登录
    authMode: 'auto',
    jingmeAuth: {
        appKey: 'your-app-key',
        sdk: JMESDK,
        // 兼容 jm-clues:把授权码直接当作 accessToken
        useAuthCodeAsToken: true,
    },
    cookieKeys: ['TGC', '_TOKENUUMS', 'U'],
    checkLogin: cookies => !!(cookies.TGC && cookies._TOKENUUMS),
    onRequireLogin: () => {
        window.location.href = BASE_API + 'login/login.action';
    },
    onJingmeLoggedIn: result => {
        console.log('京 ME 登录完成', result, isInJingme());
    },
});

仅京 ME 场景:

createAuthGuard({
    authMode: 'jingme',
    jingmeAuth: {
        appKey: 'your-app-key',
        sdk: JMESDK,
    },
});

2. 京 ME 授权(可脱离路由守卫单独使用)

适合 H5 微应用(如 jm-clues)在 axios 拦截器里等待登录完成:

import JMESDK from '@jd/jme-sdk-adapter';
import {
    isInJingme,
    initJingmeAuth,
    waitForJingmeAccessToken,
    getJingmeAccessToken,
    injectJingmeIdentity,
} from '@deppon/deppon-auth';

if (isInJingme()) {
    initJingmeAuth({
        appKey: 'r5qDa7istcFJJ3mtEcoN',
        sdk: JMESDK,
        useAuthCodeAsToken: true,
        swallowError: true,
    });
}

await waitForJingmeAccessToken();
const userId = getJingmeAccessToken();

axios.interceptors.request.use(async config => {
    await waitForJingmeAccessToken();
    return injectJingmeIdentity(config, { platform: 'jm' });
});

环境判定规则(任一命中即为 true),对齐 @jd/jme-sdk-adapter 的 isInJDME():

  1. 官方 Native 桥(优先,含 PC / Mac)
    • 移动端 DSBridge:window._dsbridge / window._dswk / UA 含 _dsbridge
    • PC / Mac:window.jsBridge / offlineJsBridge / inBifrost / __JOYME_JS_BRIDGE__
    • PC / Mac Electron 主窗口或 iframe(UA 含 Electron)
    • 工作台:window.meJsBridge
    • JoyME:window.__JOYME_JS_BRIDGE__.isJoyMe()
    • PC 离线包:file://
  2. UA 含 JDME / JingME / JoyME / JM_PC / JM_MAC / _dsbridge
  3. URL 参数 appId=jdme(德邦历史业务兼容)

不要用「页面上存在带 requestAuthCode 的 JS SDK 对象」判断环境:npm 包在普通浏览器里也会有这个方法。

3. 路由解析器(RouteResolver)

import { resolveRoutes } from '@deppon/deppon-auth';

const routes = resolveRoutes(menuTree, {
    componentResolver: path => () => import(`@/views/${path}.vue`),
    skipNoComponent: true,
});

4. Token 解析工具

import { getToken, getCookies, parseToken, parseTokenJSON, base64Decode } from '@deppon/deppon-auth';

API 摘要

API 说明
isInJingme() 是否在京 ME 环境(官方桥 + UA 兜底 + appId=jdme)
hasJingmeOfficialRuntime() 仅官方 Native 运行时(对齐 isInJDME)
initJingmeAuth(options) 初始化京 ME 授权(单例)
waitForJingmeAccessToken() 等待授权完成
getJingmeAccessToken() 读取当前 token
injectJingmeIdentity(config) 给请求配置注入身份字段
createAuthGuard(options) 登录守卫,支持 authMode
resolveRoutes(menuTree, options) 菜单转路由
getToken / parseToken Cookie / Token 工具

initJingmeAuth 主要选项:

  • appKey - 京 ME 应用 key
  • sdk - JMESDK 实例
  • useAuthCodeAsToken - 把授权码当 token(默认 true,兼容 jm-clues)
  • exchangeAccessToken - 完整三步换票(默认 false)
  • outsideJingme - skip | error(默认 skip)
  • swallowError - 失败是否吞错(默认 true)

注意事项

  1. Cookie 模式仍需自行实现 checkLogin / onRequireLogin
  2. @jd/jme-sdk-adapter 为可选 peer,业务侧自行安装并传入 sdk
  3. authMode: 'auto' 可同时兼容 PC Cookie 登录与京 ME 内打开
  4. 本包不包含用户信息状态管理

License

MIT

Related Packages

novadata-vue-auth

Authentication plugin for Vue 3 with JWT refresh token support

3,938
v-keycloak-store

Plugin de autenticação e autorização para Vue 3 usando Keycloak, Vue Router e Pi...

3,140 3
@authup/client-auth-console

Authup's hosted login, consent, registration, password recovery and device pages...

1,630 41
@d0whc3r/vue-auth-plugin

A simple authentication and authorization library for Vue.js using typescript

4,149 58
vue-router-tiny

A router for Vue.js

562 3

Version History

Version Released Vue License
2.5.105 ^3.0.0 MIT
2.5.103 ^3.0.0 MIT
2.5.102 ^3.0.0 MIT
2.5.101 ^3.0.0 MIT
2.5.100 ^3.0.0 MIT