ddt 1.0.0
ddt: ^1.0.0 copied to clipboard
This package helps to generate and verify authentication token.
import 'package:ddt/tokenizer.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final String key = "datadirr";
final dynamic _payload = {"name": "datadirr"};
String? _token = "";
dynamic _tokenData = "";
bool _expired = false;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("payload: $_payload"),
ElevatedButton(
onPressed: () {
_generateToken();
},
child: const Text("Generate Token"),
),
Text("token: $_token"),
ElevatedButton(
onPressed: () {
_verifyToken();
},
child: const Text("Verify Token"),
),
Text("tokenData: $_tokenData"),
ElevatedButton(
onPressed: () {
_checkTokenExpired();
},
child: const Text("Check Token Expired"),
),
Text("expired: $_expired"),
],
),
),
),
),
);
}
void _generateToken() async {
_token = await DDT.generateToken(_payload, key: key, seconds: 60);
debugPrint(_token);
setState(() {});
}
void _verifyToken() async {
try {
final result = await DDT.verifyToken(_token, key: key);
_tokenData = result.payload;
setState(() {});
} catch (e) {
debugPrint(e.toString());
}
}
void _checkTokenExpired() async {
try {
_expired = await DDT.isTokenExpired(_token, key: key);
setState(() {});
} catch (e) {
debugPrint(e.toString());
}
}
}