@makeblock/passport-client
TypeScript icon, indicating that this package has built-in type declarations

1.5.9 • Public • Published

makeblock passport client

安装

yarn add @makeblock/passport-client

注意事项

  • @makeblock/passport-client 里面添集成了环境判断,默认是生成环境。若需要切换环境变量使用如下方式,使用 client.env 设置即可。client.env 是枚举类型,可从 @makeblock/passport-client 导出 CLIENT_ENV 即可,具体使用方式,参看下面文档。
  • 缓存用户 token 使用的是 cookie,所以需要注意 cookie 域可能存在问题,可以重新设置 client.cookieConfig

使用方式

// 引用
import client, { PAGE_MODE, CLIENT_ENV } from '@makeblock/passport-client';

// 设置环境,默认生成环境
client.env = CLIENT_ENV.TEST;
// 设置是否国际版本
client.intl = true;
// 设置 接入的平台标识
client.platform = '';
// 设置语言
client.lang = '';
// 设置 登录完成跳转回去的链接地址。
// 注:输入的网址不需要转义,我们会默认对其进行转义。
// 没有特殊情况不要设置该属性值。
client.redirect = '';
// 获取跳转登录页面的 URL
const url = client.url;

// 获取登录 URL
const registerUrl = client.getUrl('register')


// 根据获取的 URL 跳转即可。

获取用户登录 URL

const url = client.url;

获取学生账号登录 URL

const url = client.stuLoginUrl;

获取用户主页 URL

const url = client.profile;

获取用户 token

const utoken = client.token;

根据页面名称获取页面 URL

const url = client.getUrl('pathName');

网页内嵌方式登录(使用 iframe)

// 设置展示模式为窗口模式,默认页面形式
client.mode = PAGE_MODE.WIN;
// 设置登录成功回调,窗口模式才需要设置
client.close = (data: { type: string; token: string }) => {
    // to do something.
};
// 是否显示登陆注册切换
client.showSwitchLoginRegister = true;
// 是否显示第三方登陆
client.showSocialLogin = true;
// 是否仅手机号登录
client.onlyPhoneLogin = true;
// 是否使用 localstorage 存储策略
client.useLocalstorage = true;
// 重新登录,忽略已登录帐号
client.useReLogin = true;
// 使用自定义图片 logo
client.logo = 'https:'//xxxxx';
// 使用文字 logo
client.textLogo = 'xxxxx';
// 版权信息
client.copyright = 'xxxxx';
// 隐藏 Logo
client.hideLogo = true;
// 是否显示切换服务器的操作
client.showSwitchHost = true;
// 设置切换服务回调
client.switchHost = (isIntl: boolean) => {
    // to do something.
};
// 添加消息监听,返回出事件监听
const remove = client.listenMessage();

获取用信息

try {
    const user = await client.userInfo();
} catch (error) {
    // TODO: 未登录或登录失效
}

退出登录

try {
    await client.logout();
} catch (error) {
    // TODO: 退出登录失败
}

重置 cookeie 配置

client 会根据,是否是国际版自动切换 cookie 的域和过期时间。 中国版的 cookie 的域为 .makeblock.com。 国际版的 cookie 的域为 .mblock.cc。 默认有效期都是一年

修改配置方法

client.cookieConfig = {
    domain: 'xxxx', // 域
    expires: 60 // 过期时间
};

Readme

Keywords

none

Package Sidebar

Install

npm i @makeblock/passport-client

Weekly Downloads

0

Version

1.5.9

License

ISC

Unpacked Size

61.7 kB

Total Files

18

Last publish

Collaborators

  • makeblock-official
  • huange