notAuthenticated method

Future<HttpResponse> notAuthenticated()

Renders errors.401 template or plain 401.

Implementation

Future<HttpResponse> notAuthenticated() async {
  final engine = App().container.make<TemplateEngine>();

  try {
    final html = await engine.render("errors.401", {});
    response.headers.contentType = ContentType.html;
    final csrfCookie = cookies.firstWhereOrNull((c) => c.name == 'archery_csrf_token');
    final cookie = Cookie('archery_csrf_token', csrfCookie?.value ?? App.generateKey())
      ..httpOnly = true
      ..secure = true
      ..sameSite = SameSite.lax
      ..path = '/';

    return response
      ..statusCode = HttpStatus.unauthorized
      ..cookies.add(cookie)
      ..write(html)
      ..close();
  } catch (e) {
    return response
      ..statusCode = HttpStatus.unauthorized
      ..write("401 Unauthenticated")
      ..close();
  }
}