diff --git a/.idea/runConfigurations/preserve_existing_user_in_callback_test_dart.xml b/.idea/runConfigurations/preserve_existing_user_in_callback_test_dart.xml new file mode 100644 index 00000000..d1726e07 --- /dev/null +++ b/.idea/runConfigurations/preserve_existing_user_in_callback_test_dart.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 93902309..9a3119e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 1.1.1+3 +* `authenticate` returns the current user, if one is present. + # 1.1.1+2 * `_apply` now always sends a `token` cookie. diff --git a/lib/src/plugin.dart b/lib/src/plugin.dart index a53b05db..5051bd41 100644 --- a/lib/src/plugin.dart +++ b/lib/src/plugin.dart @@ -262,7 +262,11 @@ class AngelAuth { (AuthStrategy x) => x.name == name, orElse: () => throw new ArgumentError('No strategy "$name" found.')); - var result = await strategy.authenticate(req, res, options); + + var hasExisting = req.properties.containsKey('user'); + var result = hasExisting + ? req.properties['user'] + : await strategy.authenticate(req, res, options); if (result == true) return result; else if (result != false) { @@ -292,11 +296,10 @@ class AngelAuth { res.redirect(options.successRedirect, code: HttpStatus.OK); return false; } else if (options?.canRespondWithJson != false && - req.headers.value("accept") != null && - (req.headers.value("accept").contains("application/json") || - req.headers.value("accept").contains("*/*") || - req.headers.value("accept").contains("application/*"))) { - var user = await deserializer(await serializer(result as T)); + req.accepts('application/json')) { + var user = hasExisting + ? result as T + : await deserializer(await serializer(result as T)); _onLogin.add(user); return {"data": user, "token": jwt}; } diff --git a/pubspec.yaml b/pubspec.yaml index 4bb86fb9..b748867a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: angel_auth description: A complete authentication plugin for Angel. -version: 1.1.1+2 +version: 1.1.1+3 author: Tobe O homepage: https://github.com/angel-dart/angel_auth environment: diff --git a/test/callback_test.dart b/test/callback_test.dart index e1796cc5..e412fc44 100644 --- a/test/callback_test.dart +++ b/test/callback_test.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:angel_auth/angel_auth.dart'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/common.dart'; +import 'package:dart2_constant/convert.dart'; import 'package:http/http.dart' as http; import 'package:test/test.dart'; @@ -24,7 +25,7 @@ main() { angelHttp = new AngelHttp(app, useZone: false); app.use('/users', new TypedService(new MapService())); - await app + User jdoe = await app .service('users') .create({'username': 'jdoe1', 'password': 'password'}); @@ -53,6 +54,12 @@ main() { ..end(); }))); + app.chain((RequestContext req) { + req.properties['user'] = + new User(username: req.params['name']?.toString()); + return true; + }).post('/existing/:name', auth.authenticate('local')); + client = new http.Client(); server = await angelHttp.startServer(); url = 'http://${server.address.address}:${server.port}'; @@ -72,4 +79,12 @@ main() { print('Response: ${response.body}'); expect(response.body, equals('Hello!')); }); + + test('preserve existing user', () async { + final response = await client.post('$url/existing/foo', + body: {'username': 'jdoe1', 'password': 'password'}, + headers: {'accept': 'application/json'}); + print('Response: ${response.body}'); + expect(json.decode(response.body)['data']['username'], equals('foo')); + }); }