10 examples of 'npm jsonwebtoken' in JavaScript

Every line of 'npm jsonwebtoken' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
28function _generateToken(user: User): string {
29 // Only add essential information to the JWT
30 const jwtUser: IJsonWebTokenContents = {
31 id: user._id,
32 roles: user.roles
33 }
34
35 return JWT.sign(jwtUser, env.JWT_SECRET, {
36 expiresIn: '7d'
37 })
38}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

9function generateToken(params = {}) {
10 return jwt.sign(params, process.env.SECRET_KEY, {
11 expiresIn: 86400,
12 });
13}
54public get token() {
55 return jwt.sign({
56 id: this.id,
57 role: this.role
58 }, process.env.JWT_KEY);
59}
6static generateToken(user: IUser, isMediaToken: boolean = false) {
7 const data: any = {_id: user._id.toString()};
8 if (isMediaToken) {
9 // This isMediaToken property signifies that the token is only meant for media access, i.e. for images, videos, PDFs and so on.
10 // In the rest of the codebase (API back-end & App front-end) the special token with this property is called the mediaToken.
11 // It is explicitly NOT valid for any regular user operations, like viewing pages (that require the user to be logged-in).
12 // Thus, in combination with randomized unguessable filenames, URLs that have a mediaToken attached are technically sharable.
13 // Since the token will also expire eventually, any accidental leak of an URL with mediaToken should be contained automatically.
14 // (Unless the affected files are replicated and distributed by other means prior to the expiration of course.)
15 data.isMediaToken = true;
16 }
17 return sign(
18 data,
19 config.secret,
20 {
21 expiresIn: 10080 // in seconds
22 }
23 );
24}
49export function generateToken (userId) {
50 return jwt.sign({ userId }, getAppKey())
51}
79public createAccessToken(identity: string | number) {
80 if (!this.secret) {
81 throw new Error('密匙不可为空');
82 }
83 let exp: number = Math.floor(Date.now() / 1000) + this.accessExp;
84 return jwtGenerator.sign(
85 {
86 exp: exp,
87 identity: identity,
88 scope: 'lin',
89 type: TokenType.ACCESS
90 },
91 this.secret
92 );
93}
5function signToken(user: Object) {
6 const roleinfo = user.roles[0].name;
7 const payload: AuthToken = {
8 issuer: 'boldr',
9 subject: user.id,
10 algorithms: ['HS256'],
11 expiresIn: '7 days',
12 email: user.email,
13 role: roleinfo,
14 };
15 return jwt.sign(payload, config.token.secret);
16}
21static generateToken(payload) {
22 return jwt.sign(payload, config.get('auth.key'), {
23 expiresIn: config.get('auth.tokenExpiresIn')
24 });
25}
49function createRefreshToken(userId) {
50 return jwt.sign(
51 {
52 userId,
53 type: "refresh"
54 },
55 AUTH_SECRET,
56 {
57 expiresIn: REFRESH_TOKEN_EXPIRE_SECONDS,
58 issuer: JWT_ISSUER
59 }
60 );
61}
40static generateToken(user) {
41 const token = jwt.sign(
42 {
43 id: user.userId,
44 role: user.role,
45 email: user.email.toLowerCase(),
46 },
47 process.env.SECRET,
48 {
49 expiresIn: 172800
50 }
51 );
52
53 return token;
54}

Related snippets