Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@aplus-frontend/aplus-auth-sdk

Package Overview
Dependencies
Maintainers
12
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aplus-frontend/aplus-auth-sdk

aplus auth sdk for aplus frontend project

npmnpm
Version
1.1.2
Version published
Weekly downloads
68
7.94%
Maintainers
12
Weekly downloads
 
Created
Source

@aplus-frontend/aplus-auth-sdk

NPM Version NPM Downloads NPM License

💁 介绍

aplus前端项目登录SDK,适用于aplus单体项目或者微前端项目,提供快速接入登录功能,无需在鉴权的业务逻辑担心。

⚙️ 快速开始

  #根路径加 -w
  pnpm add @aplus-frontend/aplus-auth-sdk

🔌 接入案例

  • 登录中心
//vue3 项目 main.ts中

import { authHub, authClass } from '@aplus-frontend/aplus-auth-sdk';
//初始化链接
const authClient = authHub({
  platform: 'aplus',
  used: 'login', //指明用途
  env: import.meta.env.MODE,
  debug: true,
  apiUrl: import.meta.env.VITE_GLOB_API_URL,
  urlPrefix: import.meta.env.VITE_GLOB_API_URL_PREFIX
});
//用途是login,init什么都不返回
await authClient.init();
//这里是一些登录请求逻辑
//成功之后设置token
await authClient?.setAuthToken(res.token);

🚨 注意

  • authHub内部会动态插入iframe,vue会报错SecurityError: Failed to read a named property '__v_isRef' from 'Window',解决就是authHub外层包markRaw,禁止vue代理响应式对象
  • 客户端(应用端)
//vue3 项目 main.ts中

import { authHub, authClass } from '@aplus-frontend/aplus-auth-sdk';
//初始化链接
authClient = authHub({
  platform: 'aplus',
  used: 'site', //指明用途
  env: import.meta.env.MODE,
  debug: true,
  apiUrl: import.meta.env.VITE_GLOB_API_URL,
  urlPrefix: import.meta.env.VITE_GLOB_API_URL_PREFIX
});
//用途是site init会返回token
const token = await authClient.init();
console.log('token:', token);
if (token) {
  tokenRef.value = token;
}
const userInfo = await authClient.getUserInfo();
if (userInfo) {
  console.log('userInfo:', userInfo);
}

const menus = await authClient.getUserMenus();
if (menus) {
  console.log('menus:', menus);
}

const permissions = await authClient.getUserPermissions();
if (permissions) {
  console.log('permissions:', permissions);
}
//获取url上的语言
const lang = await authClient.getUrlParam('lang')
if(lang){
  //lang zh_CN
  console.log('lang',lang)
}

🥽 authHub 参数

属性类型描述是否必传填
envdevelopment | dev | test | uat | prod环境设置否 默认dev
hubUrlstringHub URL否 默认dev的hub
redirectUrlstring重定向 URL否 默认dev的登录
whetherRedirectboolean是否重定向否 默认true
platformaplus | admin | quick平台否 默认aplus
usedlogin | site用途类型否 默认site
debugboolean调试模式,建议仅在开发环境开启,重定向地址之前会暂停否 默认false
maxRetriesnumber最大重试次数否 默认3
apiUrlstringAPI URL是 建议直接设置import.meta.env.VITE_GLOB_API_URL
urlPrefixstringURL 前缀是 建议直接设置import.meta.env.VITE_GLOB_API_URL_PREFIX

📦️ authHub 方法

方法名描述参数返回值
init初始化认证中心,usedsite时候初始化后,如果已经登录返回token,未登录自定跳转登录页,可以设置whetherRedirectfalse关闭自动跳转。usedlogin时候不返回任何值options: UserConfigPromise<string | void>
getAuthToken获取认证 tokenPromise<string>
setAuthToken设置认证 token,usedlogin时候自动跳转应用,设置whetherRedirectfalse关闭自动跳转token: stringPromise<void>
isHasAuthToken检查是否存在认证 tokenPromise<boolean>
isLegalToken检查 token 是否合法Promise<boolean>
removeAuthToken移除认证 tokenPromise<{ code: number; message: string }>
getUserInfo获取用户信息Promise<CurrentUserInfo | void>
getUserMenus获取用户菜单Promise<UserMenus | void>
getUserPermissions获取用户权限Promise<UserButton | void>
redirectLogin重定向到登录页面,可选参数records对象作为跳转到登录页需要拼接的参数records?:Record<string, any>Promise<void>
redirectUrl重定向到某个特定业务页面,url为必选,可选参数records对象作为跳转到特定页需要拼接的参数url: string;records?: Record<string, any>Promise<void>
getUrlParam获取url上的指定参数name: stringPromise<string | null>

🚨 注意

  • init 方法在 used'login' 时不会返回任何值,为site时会返回token。另外开发环境下,因为vite热重载会提示报错:init方法已经被调用过这是正常现象。
  • setAuthToken 方法需要在登录接口成功后再调用,以设置用户的认证 token。
  • redirectLogin 方法会清空浏览器历史记录并重定向到登录页面。

原理介绍

实现原理介绍

不同源的域名是如何共享数据的?答案就是iframepostMessage实现跨域共享数据,包里依赖了cross-storage这个lib。

@aplus-frontend/aplus-auth-sdk用途有两个应用场景一个是site,另一个是login,当场景是site时候链接hub共享中心(动态插入iframe hub.html),init方法检测hub中是否存在token是否过期,过期会自动跳转登录页面,当然也可以手动关掉whetherRedirect等于false,这样设置的话需要自己重定向逻辑。当场景是login时候表示是登录界面需要接入,init方法什么都不返回,调用真正的登录接口后需要调用setAuthToken把真正的token设置到hub.html中。注意:如何使用了Reactvue3框架,authHub是一个带有副作用的函数,需要结合框架的生命周期调用。init方法中hub.html的地址被预设了4个环境,如果有特殊需求,也可以通过hubUrl自行设置。

🚨 注意

2024年1月25日在高版本Chrome115中启用了存储分区,简单来说example.com通过iframe方式嵌入a.com和b.com中访问的localstorage是不同的存储分区,所有跨域名的存储共享数据失效了。查看原因 所以使用cross-storage不能直接解决问题,需要配合同源策略。

aplus本地开发指南设置

1. 修改本地host文件


  #windows电脑 C:\Windows\System32\drivers\etc\hosts

  #mac  sudo vi /etc/hosts

  #hosts文件中加入下面这行

  127.0.0.1 aplus.elnkpro.com

2. 修改vite代理配置


  //vite.config.ts

  server: {
      port: 4009,
      host: 'aplus.elnkpro.com',
      trictPort: true,
      proxy: {
        '/api': {
          target: 'http://api.dev.elnkpro.com',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    }

3. 本地开发访问网址

http://aplus.elnkpro.com:4009/

经过以上这置位于本地localhost:4009的项目可以使用dev环境的登录中心,本地项目不在有登录模块

Keywords

@aplus-frontend/aplus-auth-sdk

FAQs

Package last updated on 06 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts