How to use the encode function from jwt-simple

Find comprehensive JavaScript jwt-simple.encode code examples handpicked from public code repositorys.

405
406
407
408
409
410
411
412
413
414
415
	// Login
	app.post('/local/login', (req, res)=>{
		const username = req.body.username;
		if(!username) return;


		const payload = jwt.encode({ username: username, issued: new Date }, config.get('secret'));
		return res.json(payload);
	});
}

fork icon297
star icon862
watch icon36

+ 9 other calls in file

3
4
5
6
7
8
9
10
11
12
13
14
15
const jwt = require('jwt-simple');


const USE_SERVICE_ACCOUNT = Cypress.env('AUTHENTICATE_WITH_SERVICE_ACCOUNT') === 1;


const decodeJWT = (token, secret = null) => jwt.decode(token, secret, secret === null);
const encodeJWT = (data, secret = 'dummy_secret') => jwt.encode(data, secret);


const forgeAccessTokenWithFakeRoles = (token, fakeRoles) => {
  const accessData = decodeJWT(token);
  accessData.roles = fakeRoles;
fork icon8
star icon8
watch icon6

+ 4 other calls in file

8
9
10
11
12
13
14
15
16
17
18
19
const MAIN_ROUTE = '/v1/users';


beforeAll(async () => {
  const response = await app.services.user.save({ name: 'user account', email: `${Date.now()}@gmail.com`, password: 'password' });
  user = { ...response[0] };
  user.token = jwt.encode(user, 'secret');
});


it('Should get all users', async () => {
  await request(app).get(MAIN_ROUTE)
fork icon0
star icon1
watch icon1

+ 4 other calls in file

46
47
48
49
50
51
52
53
54
55
56
    const payload = {
        usuarioId: user.id,//id
        createdAt: moment().unix(),//Fecha de creación
        expiredAt: moment().add(6, "hours").unix()//Duración
    }
    return jwt.encode(payload, "Frase para probar .env")
    //Este token lo recibiré en cliente y lo guardaré (en localStorage)
}


const login = async (body) => {
fork icon0
star icon1
watch icon1

+ 4 other calls in file

302
303
304
305
306
307
308
  const payload = {
    exp: getUnixTime(addYears(Date.now(), config.auth.jwtExpirationInterval)),
    iat: getUnixTime(Date.now()),
    id: encoder,
  }
  return jwt.encode(payload, config.auth.jwtSecret)
}
fork icon0
star icon1
watch icon1

+ 4 other calls in file

197
198
199
200
201
202
203
204
205
206
      email: userDocument.email,
      username: userDocument.username,
      avatar: userDocument.avatar,
      bookmarks: userDocument.bookmarks,
    },
    token: jwt.encode({ id: userDocument._id }, process.env.JWT_SECRET),
  });
}

const existingUser = await User.findOne({
fork icon0
star icon0
watch icon1

+ 39 other calls in file

7
8
9
10
11
12
13
14
15
16
17
18
let user


beforeAll(async () => {
  const res = await app.services.user.save({ name: 'User account', mail: `${Date.now()}@mail.com`, password: '123456' })
  user = { ...res[0] }
  user.token = jwt.encode(user, 'Segredo!')
})


test('Deve listar todos os users', () => {
  return request(app).get(MAIN_ROUTE)
fork icon0
star icon0
watch icon1

+ 4 other calls in file

154
155
156
157
158
159
160
161
162
163
164
    const payload = {
        userId: user.id,
        createdAt: moment().unix(),
        expiredAt: moment().add(60,'minutes').unix()
    }
    return jwt.encode(payload, 'frase secreta');
}


const decodeToken = (token) => {
    const payload = jwt.decode(token, 'frase secreta');
fork icon0
star icon0
watch icon0

+ 4 other calls in file