jsonwebtoken#SignOptions TypeScript Examples

The following examples show how to use jsonwebtoken#SignOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: user_failure.ts    From api-example with MIT License 6 votes vote down vote up
describe('login', () => {
  it('should return internal_server_error if jwt.sign fails with the error', async () => {
    const sign = jwt.sign;
    (jwt.sign as any) = (payload: string | Buffer | object,
      secretOrPrivateKey: Secret,
      options: SignOptions,
      callback: SignCallback) => {
        callback(new Error('failure'), undefined)
    }

    const dummy = await createDummy()
    await expect(user.login(dummy.email, dummy.password)).rejects.toEqual({
      error: {type: 'internal_server_error', message: 'Internal Server Error'}
    })
    jwt.sign = sign
  })

  it('should return JWT token, userId, expireAt to a valid login/password if cacheExternal rejects', async () => {
    (cacheExternal.setProp as jest.Mock).mockRejectedValueOnce(new Error('connection error'));
    const dummy = await createDummy()
    await expect(user.login(dummy.email, dummy.password)).resolves.toEqual({
      userId: dummy.userId,
      token: expect.stringMatching(/^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/),
      expireAt: expect.any(Date)
    })
  })
})
Example #2
Source File: token.service.ts    From relate with GNU General Public License v3.0 6 votes vote down vote up
static async sign(data: any, salt = '', options: SignOptions = {}): Promise<string> {
        const instanceSalt = await TokenService.getInstanceSalt();
        const jwtTokenSalt = `${instanceSalt}-${salt}`;
        const optionsToUse = _.merge({}, DEFAULT_JWT_SIGN_OPTIONS, options);

        return new Promise((resolve, reject) => {
            jwt.sign(data, jwtTokenSalt, optionsToUse, (err, token) => {
                if (err || !token) {
                    reject(err);
                    return;
                }

                resolve(token);
            });
        });
    }
Example #3
Source File: getSignedJwt.ts    From tezos-academy with MIT License 6 votes vote down vote up
getSignedJwt: GetSignedJwt = (_id, username, userRole) => {
  const expiresAt: Date = dayjs().add(30, 'day').toDate()

  const payload: JwtPayload = {
    _id,
    username,
    userRole,
    expiresAt,
  }

  const signOptions: SignOptions = {
    issuer: 'TezosAcademy',
    subject: username + '',
    audience: 'https://tezosacademy.io',
    expiresIn: '30d',
    algorithm: 'RS256',
  }

  const jwt: Jwt = jsonwebtoken.sign(payload, process.env.JWT_PRIVATE_KEY as Secret, signOptions)

  return jwt
}
Example #4
Source File: jwtUtils.ts    From nodejs-angular-typescript-boilerplate with Apache License 2.0 6 votes vote down vote up
generateToken = (payload: string | any | Buffer, options: SignOptions) => new Promise((resolve, reject) => {
    jwt.sign(payload, process.env.SECRET, options || { noTimestamp: true }, (err, token) => {
        if (err) {
            reject(err);
        } else {
            resolve(token);
        }
    });
})
Example #5
Source File: token.ts    From jibri-queue with Apache License 2.0 5 votes vote down vote up
export function recorderToken(options: SignOptions, privateKey: Buffer): string {
    const payload = {
        room: '*',
    };
    return sign(payload, privateKey, options);
}
Example #6
Source File: user.ts    From api-example with MIT License 5 votes vote down vote up
signOptions: SignOptions = {
  algorithm: 'RS256',
  expiresIn: '14d'
}
Example #7
Source File: constants.ts    From relate with GNU General Public License v3.0 5 votes vote down vote up
DEFAULT_JWT_SIGN_OPTIONS: SignOptions = {expiresIn: TWENTY_FOUR_HOURS_SECONDS}