Refactored

This commit is contained in:
thomashii 2023-06-04 13:20:42 +08:00
parent 1e616f3f5c
commit 8e251e553a

View file

@ -66,19 +66,44 @@ class LocalAuthStrategy<User> extends AuthStrategy<User> {
return null; return null;
} }
// Allow non-null to pass through //Allow non-null to pass through
//return verificationResult; //return verificationResult;
} }
} else {
var body = await req
.parseBody()
.then((_) => req.bodyAsMap)
.catchError((_) => <String, dynamic>{});
if (_validateString(body[usernameField]?.toString()) &&
_validateString(body[passwordField]?.toString())) {
verificationResult = await verifier(
body[usernameField]?.toString(), body[passwordField]?.toString());
}
} }
if (verificationResult == null) { // User authentication succeeded
if (forceBasic) { if (verificationResult == true ||
res.headers['www-authenticate'] = 'Basic realm="$realm"'; (verificationResult is Map && verificationResult.isNotEmpty)) {
return null; return verificationResult;
} }
// Force basic if set
if (forceBasic) {
res.headers['www-authenticate'] = 'Basic realm="$realm"';
return null; return null;
} }
// Redirect failed authentication
if (localOptions.failureRedirect != null &&
localOptions.failureRedirect!.isNotEmpty) {
await res.redirect(localOptions.failureRedirect, code: 401);
return null;
}
_log.info('Not authenticated');
throw AngelHttpException.notAuthenticated();
/*
if (verificationResult is Map && verificationResult.isEmpty) { if (verificationResult is Map && verificationResult.isEmpty) {
if (localOptions.failureRedirect != null && if (localOptions.failureRedirect != null &&
localOptions.failureRedirect!.isNotEmpty) { localOptions.failureRedirect!.isNotEmpty) {
@ -99,5 +124,8 @@ class LocalAuthStrategy<User> extends AuthStrategy<User> {
_log.info('Not authenticated'); _log.info('Not authenticated');
throw AngelHttpException.notAuthenticated(); throw AngelHttpException.notAuthenticated();
} }
*/
} }
bool _validateString(String? str) => str != null && str.isNotEmpty;
} }