本文整理汇总了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);
}
});
示例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'});
});
示例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 });
});
});
示例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'
});
}
}
})
示例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)
})
示例6: function
userSchema.method('createJWT', function() {
return jwt.sign({
_id: this._id,
email: this.email,
isAdmin: this.isAdmin
}, process.env.JWT_SECRET);
});
示例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!
}
示例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);
}
示例9:
const success = (userId: string) => {
const payload: JsonWebTokenPayload = { userId };
return {
errors: null,
token: jwt.sign(payload, API_SECRET as jwt.Secret),
};
};
示例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;
}