@gapi/auth

中文文档
1.8.263发布于 2026/11/16MITTypeScriptES Module近 30 天下载 4427

bash $ npm install @gapi/auth --save

原始使用文档

@Gapi authentication Module

For questions/issues you can write ticket here
This module is intended to be used with rxdi or gapi

Installation and basic examples:

To install this Gapi module, run:
$ npm install @gapi/auth --save

Consuming @gapi/auth

Import inside AppModule or CoreModule
import { Module } from '@rxdi/core';
import { AuthModule } from '@gapi/auth';

@Module({
  imports: [
    AuthModule.forRoot({
      algorithm: 'HS256',
      cert: 'dadada',
      cyper: {
        algorithm: 'dada',
        iv: '',
        privateKey: 'dadada',
      },
    }),
  ],
})
export class CoreModule {}
Create Auth Service and provide it
import { Service } from '@rxdi/core';
import * as Boom from 'boom';
import { AuthInterface, AuthInternalService, TokenData } from '@gapi/auth';

export interface UserInfo {
  scope: ['ADMIN', 'USER'];
  type: 'ADMIN' | 'USER';
  iat: number;
}

@Service()
export class AuthService implements AuthInterface {
  constructor(private authService: AuthInternalService) {}

  onSubOperation(message, params, webSocket) {
    return params;
  }

  onSubConnection(connectionParams): TokenData {
    if (connectionParams.token) {
      return this.validateToken(connectionParams.token, 'Subscription');
    } else {
      throw Boom.unauthorized();
    }
  }

  validateToken(
    token: string,
    requestType: 'Query' | 'Subscription' = 'Query'
  ): any {
    const user = <any>this.authService.verifyToken(token);
    user.type = user.scope[0];
    console.log(`${requestType} from: ${JSON.stringify(user)}`);
    if (user) {
      return user;
    } else {
      throw Boom.unauthorized();
    }
  }

  signJWTtoken(tokenData: TokenData): string {
    return this.authService.sign(tokenData);
  }

  issueJWTToken(tokenData: TokenData) {
    const jwtToken = this.authService.sign({
      email: '',
      id: 1,
      scope: ['ADMIN', 'USER'],
    });
    return jwtToken;
  }

  verifyToken(token: string): TokenData {
    return this.authService.verifyToken(token);
  }

  decryptPassword(password: string): string {
    return this.authService.decrypt(password);
  }

  encryptPassword(password: string): string {
    return this.authService.encrypt(password);
  }
}
Provide this particular Auth Service to authentication parameter inside CoreModule.forRoot
import { CoreModule, Module } from '@gapi/core';
import { AuthService } from './app/core/services/auth/auth.service';
import { AuthModule } from '@gapi/auth';
import { readFileSync } from 'fs';

@Module({
  imports: [
    AuthModule.forRoot({
      algorithm: 'HS256',
      cert: readFileSync('./cert.key'),
      cyper: {
        algorithm: 'aes256',
        iv: 'Jkyt1H3FA8JK9L3B',
        privateKey: '8zTVzr3p53VC12jHV54rIYu2545x47lA',
      },
    }),
    CoreModule.forRoot({
      pubsub: {
        authentication: AuthService,
      },
      graphql: {
        authentication: AuthService,
      },
    }),
  ],
})
export class FrameworkImports {}

Then Subscriptions, Mutations and Queries will be secured via single authentication method validateToken inside AuthService!

TODO: Better documentation...

Enjoy ! :)

版本与发布记录

@gapi/auth 共发布 539 个版本,首次发布于 2018/07/03;过去 12 个月发布了 116 个版本。

版本发布日期距今
1.8.263latest2026/11/16今天
1.8.2622026/11/132 天前
1.8.2612026/11/096 天前
1.8.2602026/11/097 天前
1.8.2592026/11/097 天前
1.8.2582026/11/078 天前

其他发布通道:nightly → 1.8.168-nightly.3。

关于 @gapi/auth 的常见问题

@gapi/auth 是什么?

bash $ npm install @gapi/auth --save

如何安装 @gapi/auth?

在项目目录运行 npm install @gapi/auth,或使用 pnpm add / yarn add / bun add。

@gapi/auth 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gapi/auth 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@gapi/auth 可以商用吗?

@gapi/auth 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gapi/auth 的下载量有多少?

根据 npm 官方下载统计,@gapi/auth 在 2026/10/16 – 2026/11/14 这 30 天内被下载 4,427 次,日均约 148 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gapi/auth 还在维护吗?

最新版本 1.8.263 发布于 2026/11/16(今天);至今共发布 539 个版本,其中过去 12 个月发布了 116 个;首次发布于 2018/07/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。