preserve existing user

This commit is contained in:
Tobe O 2018-06-27 13:17:44 -04:00
parent a36b191650
commit 013c77aba2
5 changed files with 37 additions and 8 deletions

View file

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="preserve existing user in callback_test.dart" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true" nameIsGenerated="true">
<option name="filePath" value="$PROJECT_DIR$/test/callback_test.dart" />
<option name="scope" value="GROUP_OR_TEST_BY_NAME" />
<option name="testName" value="preserve existing user" />
<method />
</configuration>
</component>

View file

@ -1,3 +1,6 @@
# 1.1.1+3
* `authenticate` returns the current user, if one is present.
# 1.1.1+2 # 1.1.1+2
* `_apply` now always sends a `token` cookie. * `_apply` now always sends a `token` cookie.

View file

@ -262,7 +262,11 @@ class AngelAuth<T> {
(AuthStrategy x) => x.name == name, (AuthStrategy x) => x.name == name,
orElse: () => orElse: () =>
throw new ArgumentError('No strategy "$name" found.')); 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) if (result == true)
return result; return result;
else if (result != false) { else if (result != false) {
@ -292,11 +296,10 @@ class AngelAuth<T> {
res.redirect(options.successRedirect, code: HttpStatus.OK); res.redirect(options.successRedirect, code: HttpStatus.OK);
return false; return false;
} else if (options?.canRespondWithJson != false && } else if (options?.canRespondWithJson != false &&
req.headers.value("accept") != null && req.accepts('application/json')) {
(req.headers.value("accept").contains("application/json") || var user = hasExisting
req.headers.value("accept").contains("*/*") || ? result as T
req.headers.value("accept").contains("application/*"))) { : await deserializer(await serializer(result as T));
var user = await deserializer(await serializer(result as T));
_onLogin.add(user); _onLogin.add(user);
return {"data": user, "token": jwt}; return {"data": user, "token": jwt};
} }

View file

@ -1,6 +1,6 @@
name: angel_auth name: angel_auth
description: A complete authentication plugin for Angel. description: A complete authentication plugin for Angel.
version: 1.1.1+2 version: 1.1.1+3
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_auth homepage: https://github.com/angel-dart/angel_auth
environment: environment:

View file

@ -2,6 +2,7 @@ import 'dart:io';
import 'package:angel_auth/angel_auth.dart'; import 'package:angel_auth/angel_auth.dart';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/common.dart'; import 'package:angel_framework/common.dart';
import 'package:dart2_constant/convert.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:test/test.dart'; import 'package:test/test.dart';
@ -24,7 +25,7 @@ main() {
angelHttp = new AngelHttp(app, useZone: false); angelHttp = new AngelHttp(app, useZone: false);
app.use('/users', new TypedService<User>(new MapService())); app.use('/users', new TypedService<User>(new MapService()));
await app User jdoe = await app
.service('users') .service('users')
.create({'username': 'jdoe1', 'password': 'password'}); .create({'username': 'jdoe1', 'password': 'password'});
@ -53,6 +54,12 @@ main() {
..end(); ..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(); client = new http.Client();
server = await angelHttp.startServer(); server = await angelHttp.startServer();
url = 'http://${server.address.address}:${server.port}'; url = 'http://${server.address.address}:${server.port}';
@ -72,4 +79,12 @@ main() {
print('Response: ${response.body}'); print('Response: ${response.body}');
expect(response.body, equals('Hello!')); 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'));
});
} }