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

uaa-core-api

v1.1.5

Published

统一认证

Downloads

2

Readme

uaa-core-api

统一认证

Browser Support

> 1%
last 2 versions
not ie <= 8

Installing

Using npm:

npm install uaa-core-api

use

import uaa from 'uaa-core-api'
uaa.initServer({
  uaaServer: '', // uaa认证服务地址
  userServer: '', // user-center
  clientId: '', // uaa服务注册的clientId
  clientSecret: '',// uaa服务注册的clientSecret
  grantType: '',  // 授权方式
  scope: '' // 授权范围
})
uaa.checkLogin().then(res => {
  const userInfo = res
}).catch(() => {
  uaa.login(redirectUri)
})

API

uaa对象提供一组方法

  • uaa.initServer(options) 详细配置查看下方options说明

  • uaa.checkLogin() return promise,resolve(userInfo);reject(Error('未登录'))

  • uaa.getToken() return token,type: string;e.g: 'Bearer decf0911-cbc9-48ab-ac5b-1d6aac25ecdc'

  • uaa.getAccessToken() return access_token, type: string, e.g: decf0911-cbc9-48ab-ac5b-1d6aac25ecdc (和token的不同是不带有前缀“Bearer ”)

  • uaa.getUserInfo() return userInfo,type: Object

  • uaa.login(redirectUri) return promise,resolve(userInfo),reject(Error);redirectUri: 授权重定向地址,重定向地址必须与数据库配置保持一致

  • uaa.logout(redirectUri) redirectUri: 登出后重定向的地址,重定向地址必须与数据库配置保持一致

options

  • options.uaaServer: uaa认证服务地址

  • options.userServer: userCenter服务地址

  • options.clientId: uaa注册的clientId

  • option.clientSecret: uaa 注册的clientSecret

  • options.grant_type: 授权方式(password | authorization_code | implicit | client_credentials | openId);默认值: 'authorization_code'

  • options.scope: 授权范围,数据库配置的scope, 默认值: 'all'; 该参数只对grant_type值是authorization_code 或 implicit 生效

  • options.customUaaServer: 选填, 自定义认证授权服务,在统一认证后,需要再自定义应用自身的认证授权方案时用到。

  • options.customUserServer: 选填,自定义认证授权服务对应的用户服务, 获取应用自身的用户信息,不传则获取统一认证的用户信息。

  • options.customClientId: 选填, 默认值: lcdp

  • options.customClientSecret: 选填, 默认值: lcdp

  • options.customGrantType: 选填, 默认: mg

Example

  1. step1 路由守卫中检测是否登录,处理登录后流程
import uaa from 'uaa-core-api'
const initSystem = (userInfo, to, next) => {
  store.commit('user/setUserInfo', userInfo)
  const cacheRoutePath = getCacheRoutePath()
  if (cacheRoutePath) {
    removeCacheRoutePath()
    next({path: cacheRoutePath, replace: true})
  } else {
    next({...to, replace: true})
  }
}
const setCacheRoutePath = (path) => {
  if (path && !getCacheRoutePath()) {
    window.sessionStorage.setItem('cacheRoutePath', path)
  }
}
const getCacheRoutePath = () => {
  const cacheRoutePath = window.sessionStorage.getItem('cacheRoutePath')
  return cacheRoutePath
}
const removeCacheRoutePath = () => {
  window.sessionStorage.removeItem('cacheRoutePath')
}
uaa.initServer({
  uaaServer: '', // uaa认证服务地址
  userServer: '', // user-center
  clientId: '', // uaa服务注册的clientId
  clientSecret: '',// uaa服务注册的clientSecret
  grantType: '',  // 授权方式
  scope: '' // 授权范围
})
router.beforeEach((to, from ,next) => {
  uaa.checkLogin().then(res => {
    const userInfo = res
    const storeUserInfo = store.state.user.userInfo
    //  判断是否已初始化过系统数据
    if(storeUserInfo && storeUserInfo.id) {
      next()
    } else {
      // 初始化系统数据,存储userInfo, 加载异步路由、初始化菜单
      initSystem(userInfo, to, next)
    }
  }).catch(e => {
    // redirectUri必须与数据库配置保持一致
    const getBaseUrl = () => {
      const baseUrl = process.env.BASE_URL
      if (baseUrl.includes('http://') || baseUrl.includes('https://')) {
        return baseUrl
      } else {
        return window.location.protocol + '//' + window.location.host + baseUrl
      }
    }
    const redirectUri = getBaseUrl()
    // 存储登录前的path
    setCacheRoutePath(to.fullPath)
    uaa.login(redirectUri).then(res => {
      const userInfo = res
      console.log(userInfo)
      // 方案1: 通过重定向重新进页面,去掉url上的code 或 token
      location.href = redirectUri
      // 方案2: 或直接初始化数据再next(); 最后url会携带授权信息:code 或 token
      // initSystem(userInfo, to, next)
    }).catch(e => {
      //  提示登录失败信息
      console.log(e)
      next()
    })
  })
})
  1. step2 在请求头加上授权信息
  // aixos instance
  import uaa from 'uaa-core-api'
  const instance = axios.create({
    baseURL: '', //  配置后端服务地址
  })
  // 在拦截器上为每个请求加上授权信息
  instance.interceptors.request.use((config) => {
    config.headers['Authorization'] = uaa.getToken()
    return config
  }, error => {
    // 请求失败的处理
    return Promise.reject(error)
  })
  // 在请求响应拦截器上处理401错误请求
  instance.interceptors.response.use((res) => {
    // 在这里对返回的数据进行处理
    return res
  }, error => {
    if(error.response && error.response.status === 401) {
      // 未授权、或授权时效时,调用uaa.logout()清除token并跳到登录页面
      console.log('授权已过期,请重新登录')
      // 退出登录
      const getBaseUrl = () => {
        const baseUrl = process.env.BASE_URL
        if (baseUrl.includes('http://') || baseUrl.includes('https://')) {
          return baseUrl
        } else {
          return window.location.protocol + '//' + window.location.host + baseUrl
        }
      }
      const redirectUri = getBaseUrl()
      uaa.logout(redirectUri)
    }
  })