Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose jwt package #62

Merged
merged 8 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/pharaoh/lib/src/http/response.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:io';
import 'package:http_parser/http_parser.dart';
import '../utils/utils.dart';

import '../utils/exceptions.dart';
import '../shelf_interop/shelf.dart' as shelf;
Expand Down Expand Up @@ -182,12 +183,13 @@ class Response extends Message<shelf.Body?> implements $Response {
);
}

/// [data] should be json-encodable
@override
Response json(Object? data) {
late Object result;
try {
if (data is Set) data = data.toList();
result = jsonEncode(data);
result = jsonEncode(data, toEncodable: customEncoder);
samtuga1 marked this conversation as resolved.
Show resolved Hide resolved
} catch (_) {
final errStr = jsonEncode(makeError(message: _.toString()).toJson);
return Response(
Expand Down
9 changes: 9 additions & 0 deletions packages/pharaoh/lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,12 @@ String hashData(dynamic sess) {
if (sess is! String) sess = jsonEncode(sess);
return sha1.convert(utf8.encode(sess)).toString();
}

/// Custom encoder to serialize [data]
Object? customEncoder(Object? data) {
if (data == null) return null;
if (data is DateTime) {
return data.toUtc().toIso8601String();
}
return data;
}
samtuga1 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion packages/pharaoh/test/middleware/session_mw_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void main() {
test('should create a new session', () async {
final app = Pharaoh()
.use(session(secret: 'foo bar baz'))
.get('/', (req, res) => res.json(req.session));
.get('/', (req, res) => res.json(req.session?.toJson()));
samtuga1 marked this conversation as resolved.
Show resolved Hide resolved

await (await request(app))
.get('/')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import 'package:pharaoh/pharaoh.dart';
import 'package:pharaoh_jwt_auth/pharaoh_jwt_auth.dart';

Expand Down
2 changes: 1 addition & 1 deletion packages/pharaoh_jwt_auth/lib/pharaoh_jwt_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ library;

export 'src/pharaoh_jwt_auth_base.dart';

// TODO: Export any libraries intended for clients of this package.
export 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
1 change: 0 additions & 1 deletion packages/pharaoh_jwt_auth/test/pharaoh_jwt_auth_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:convert';

import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import 'package:pharaoh/pharaoh.dart';
import 'package:pharaoh_jwt_auth/pharaoh_jwt_auth.dart';
import 'package:spookie/spookie.dart';
Expand Down