If you want to revoke all tokens given a token, grab the user ID from the authorization token and run a delete query for that user's tokens:
class TokenManagerController extends ResourceController {
@Operation.delete()
Future<Response> deleteTokens() async {
final userId = request.authorization.ownerID;
final query = Query<ManagedAuthToken>(context)
..where((token) => token.resourceOwner).identifiedBy(userId);
final count = await query.delete();
return Response.ok({"userId": userId, "tokensDeleted": count});
}
}
And make sure you link an authorizer:
router.route("/tokens")
.link(() => Authorizer.bearer(authServer))
.link(() => TokenManagerController(context));
FWIW, I recommend having a scope specifically for this action that is only granted for this scenario through an additional login. The UX is that the user has to enter their password again.
If you just want to delete one token, just run a delete query where access_token
= the token in the authorization header.
class LogoutController extends ResourceController {
@Operation.delete()
Future<Response> deleteTokens(@Bind.header('authorization') String authHeader) async {
final parser = AuthorizationBearerParser();
final userToken = parser.parse(authHeader);
final query = Query<ManagedAuthToken>(context)
..where((token) => token.accessToken).equalTo(userToken);
final count = await query.delete();
final userId = request.authorization.ownerID;
return Response.ok({"userId": userId, "tokensDeleted": count});
}
}