gazelle_jwt 0.1.3 copy "gazelle_jwt: ^0.1.3" to clipboard
gazelle_jwt: ^0.1.3 copied to clipboard

Plugin for JSON Web Token (JWT) authentication in Gazelle, providing functionality to sign, verify, and handle JWT tokens during HTTP request processing.

example/gazelle_jwt_example.dart

import 'package:gazelle_core/gazelle_core.dart';
import 'package:gazelle_jwt/gazelle_jwt.dart';
import 'package:http/http.dart' as http;

void main() async {
  // Initialize your Gazelle app.
  final app = GazelleApp(port: 3000);
  // Register GazelleJwtPlugin.
  await app.registerPlugin(GazelleJwtPlugin(SecretKey("supersecret")));

  // Setup your routes.
  app
    ..post(
      "/login",
      (request, response) async {
        // Use the request to get data sent from the client.
        return response.copyWith(
          statusCode: 200,
          // Sign a token and send it back to the client.
          body: app.getPlugin<GazelleJwtPlugin>().sign({"test": "123"}),
        );
      },
    )
    ..get(
      "/hello_world",
      (request, response) async {
        return response.copyWith(
          statusCode: 200,
          body: "Hello, World!",
        );
      },
      // Add the authentication hook provided by the plugin to guard your routes.
      preRequestHooks: [app.getPlugin<GazelleJwtPlugin>().authenticationHook],
    );

  // Start your server.
  await app.start();

  // CLIENT SIDE
  final baseUrl = "http://${app.address}:${app.port}";

  // Ask for a token.
  final token =
      await http.post(Uri.parse("$baseUrl/login")).then((e) => e.body);

  // Authenticate your requests.
  final result = await http.get(Uri.parse("$baseUrl/hello_world"), headers: {
    "Authorization": "Bearer $token",
  });

  print(result.body); // Prints "Hello, World!"

  await app.stop(force: true);
}
2
likes
130
pub points
0%
popularity

Publisher

verified publisherintales.it

Plugin for JSON Web Token (JWT) authentication in Gazelle, providing functionality to sign, verify, and handle JWT tokens during HTTP request processing.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

dart_jsonwebtoken, gazelle_core

More

Packages that depend on gazelle_jwt