Files
dart_jsonwebtoken/example/example.dart
Jonas Roussel 75473b7e4b v0.1.0
2020-05-31 16:10:51 +02:00

38 lines
727 B
Dart

import 'package:jsonwebtoken/jsonwebtoken.dart';
main() {
String token;
/* Sign */ {
// Create a json web token
final jwt = JWT(
payload: {
'id': 123,
'server': {
'id': '3e4fc296',
'loc': 'euw-2',
}
},
issuer: 'https://github.com/jonasroussel/jsonwebtoken',
);
// Sign it
token = jwt.sign('secret-key');
print('Signed token: $token\n');
}
/* Verify */ {
try {
// Verify a token
final jwt = JWT.verify(token, 'secret-key');
print('Payload: ${jwt.payload}');
} on JWTExpiredError {
print('jwt expired');
} on JWTError catch (ex) {
print(ex.message); // ex: invalid signature
}
}
}