當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript jsonwebtoken.sign函數代碼示例

本文整理匯總了TypeScript中jsonwebtoken.sign函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript sign函數的具體用法?TypeScript sign怎麽用?TypeScript sign使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了sign函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1:

app.post('/token', (req, res, next) => {
  if (req.body.email && req.body.password) {
    const email = req.body.email;
    const password = req.body.password;
    const user = users.find((value) => {
      return value.email === email && value.password === password;
    });

    if (user) {
      const tokenPayload = {
        id: user.id,
      };
      const signOptions: jwter.SignOptions = {
        expiresIn: 60,
      };
      const token = jwter.sign(tokenPayload, configs.JWTConfig.jwtSecret, signOptions);
      res.json({
        token,
      });
    } else {
      res.sendStatus(404);
    }
  } else {
    res.sendStatus(404);
  }
});
開發者ID:Monsoir,項目名稱:jwt-authentication-demo,代碼行數:26,代碼來源:app.ts

示例2: function

UserSchema.method('generateJWT', function() {
  return jwt.sign({
    username: this.username,
    roles: this.roles,
    permissions: this.roles.reduce((acc, role) => { return acc.concat(permission[role]); }, [])
  }, process.env.JWT_SECRET, {expiresIn: '2 days'});
});
開發者ID:bamtron5,項目名稱:webpackNGComponents,代碼行數:7,代碼來源:User.ts

示例3:

 r.table('players').filter({ email }).then((users) => {
     const options = { expiresIn: '365d' };
     const user = users[0];
     jwt.sign({ id: user.id }, secret, options, (err, token) => {
         res.status(200).send({ token, id: user.id, name: user.name });
     });
 });
開發者ID:radotzki,項目名稱:bullshit-server,代碼行數:7,代碼來源:index.ts

示例4:

 User.findOne({username: req.body.username}, (err: any, item: any) => {
         if (err) throw err;
         if(!item){
                 res.send({
                         isSuccess: false,
                         errOn: 'userName',
                         msg: 'user is not existed! please Sign up!'
                 })
         }else{
                if(!Crypto.bcryptCompare(req.body.password, item.password)){
                        res.send({
                                isSuccess: false,
                                errOn: 'password',
                                msg: 'password is wrong!'
                        });
                }else{
                        //generate token and return
                        let token: any = jwt.sign(req.body, config.JwtStrategy.secretOrKey, {});
                        res.send({
                                token: token,
                                isSuccess: true,
                                userName: item.username,
                                msg: 'Sign In Successfully'
                        });
                }
         }
 })
開發者ID:jcdby,項目名稱:SuwonCMIAPIServer,代碼行數:27,代碼來源:userController.ts

示例5: function

UserSchema.method('getToken', function():string{
  const payload:MEUserPayload = {
    _id: this._id,
    expired: new Date(Date.now()+86400000)
  }
  return jwt.sign(payload, TOKEN_SECRET)
})
開發者ID:memolog,項目名稱:mean-edge,代碼行數:7,代碼來源:user.ts

示例6: function

userSchema.method('createJWT', function() {
  return jwt.sign({
    _id: this._id,
    email: this.email,
    isAdmin: this.isAdmin
  }, process.env.JWT_SECRET);
});
開發者ID:stevenfogel,項目名稱:ELS,代碼行數:7,代碼來源:user.model.ts

示例7: getLocalToken

  getLocalToken(): string | null {
    if (!this.clusterSecret && !process.env.PRISMA_MANAGEMENT_API_SECRET) {
      return null
    }
    if (!this.cachedToken) {
      const grants = [{ target: `*/*`, action: '*' }]
      const secret =
        process.env.PRISMA_MANAGEMENT_API_SECRET || this.clusterSecret

      try {
        const algorithm = process.env.PRISMA_MANAGEMENT_API_SECRET
          ? 'HS256'
          : 'RS256'
        this.cachedToken = jwt.sign({ grants }, secret, {
          expiresIn: '5y',
          algorithm,
        })
      } catch (e) {
        throw new Error(
          `Could not generate token for cluster ${chalk.bold(
            this.getDeployEndpoint(),
          )}. Did you provide the env var PRISMA_MANAGEMENT_API_SECRET?
Original error: ${e.message}`,
        )
      }
    }

    return this.cachedToken!
  }
開發者ID:nunsie,項目名稱:prisma,代碼行數:29,代碼來源:Cluster.ts

示例8: createEnrollmentTokens

  public async createEnrollmentTokens(
    user: FrameworkUser,
    numTokens: number = 1
  ): Promise<string[]> {
    const tokens = [];
    const enrollmentTokensTtlInSeconds = this.framework.getSetting('enrollmentTokensTtlInSeconds');

    const enrollmentTokenExpiration = moment()
      .add(enrollmentTokensTtlInSeconds, 'seconds')
      .toJSON();
    const enrollmentTokenSecret = this.framework.getSetting('encryptionKey');

    while (tokens.length < numTokens) {
      const tokenData = {
        created: moment().toJSON(),
        expires: enrollmentTokenExpiration,
        randomHash: randomBytes(26).toString(),
      };

      tokens.push({
        expires_on: enrollmentTokenExpiration,
        token: signToken(tokenData, enrollmentTokenSecret),
      });
    }

    await Promise.all(
      chunk(tokens, 100).map(tokenChunk => this.adapter.insertTokens(user, tokenChunk))
    );

    return tokens.map(token => token.token);
  }
開發者ID:elastic,項目名稱:kibana,代碼行數:31,代碼來源:tokens.ts

示例9:

 const success = (userId: string) => {
   const payload: JsonWebTokenPayload = { userId };
   return {
     errors: null,
     token: jwt.sign(payload, API_SECRET as jwt.Secret),
   };
 };
開發者ID:este,項目名稱:este,代碼行數:7,代碼來源:userModel.ts

示例10: createToken

export function createToken(user: IUser, sessionToken: IToken): string {
    const payload: IUserSession = { id: user.id, _id: user._id, token_id: sessionToken.id };
    const token = jwt.sign(payload, conf.jwtSecret);
    sessionCache.set(sessionToken.id, sessionToken.usuario.toString());

    return token;
}
開發者ID:maticorv,項目名稱:mascotas2018_foro,代碼行數:7,代碼來源:passport.ts


注:本文中的jsonwebtoken.sign函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。