Skip to content

Commit 044eee3

Browse files
Add CSRF token route
1 parent 145b074 commit 044eee3

4 files changed

Lines changed: 67 additions & 49 deletions

File tree

backend/langpro_annotator/settings.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424
# SECURITY WARNING: don't run with debug turned on in production!
2525
DEBUG = int(os.getenv("DJANGO_DEBUG", 0)) == 1
2626

27+
# CSRF trusted origins for cross-origin requests
28+
CSRF_TRUSTED_ORIGINS = []
29+
2730
ALLOWED_HOSTS = ["la-backend"]
31+
2832
if DEBUG:
2933
ALLOWED_HOSTS.append("localhost")
30-
31-
# CSRF trusted origins for cross-origin requests
32-
CSRF_TRUSTED_ORIGINS = [
33-
"http://localhost:5000",
34-
"http://127.0.0.1:5000",
35-
]
34+
CSRF_TRUSTED_ORIGINS.extend([
35+
"http://localhost:5000",
36+
"http://127.0.0.1:5000",
37+
])
3638

3739
# Application definition
3840

backend/langpro_annotator/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from rest_framework import routers
2323

2424
from annotation.views import LabelAnnotationView
25+
from langpro_annotator.views import csrf_token
2526
from problem.views.problem import ProblemView
2627

2728
from .index import index
@@ -53,6 +54,7 @@
5354
),
5455
),
5556
path("api/i18n/", i18n),
57+
path("api/csrf", csrf_token),
5658
path("users/", include("user.urls")),
5759
# spa_url, # catch-all; unknown paths to be handled by a SPA
5860
]

backend/langpro_annotator/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.views.decorators.csrf import ensure_csrf_cookie
2+
from django.http import JsonResponse
3+
4+
@ensure_csrf_cookie
5+
def csrf_token(request):
6+
return JsonResponse({"detail": "CSRF cookie set"})

frontend/src/app/app.component.ts

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,51 @@
1-
import { Component, Inject, afterRender } from "@angular/core";
2-
import { DOCUMENT } from "@angular/common";
3-
import { RouterOutlet } from "@angular/router";
4-
import { MenuComponent } from "./menu/menu.component";
5-
import { FooterComponent } from "./footer/footer.component";
6-
import { DarkModeService } from "./services/dark-mode.service";
7-
import { ToastContainerComponent } from "./toast-container/toast-container.component";
8-
9-
@Component({
10-
selector: "la-root",
11-
standalone: true,
12-
imports: [
13-
RouterOutlet,
14-
MenuComponent,
15-
FooterComponent,
16-
ToastContainerComponent,
17-
],
18-
templateUrl: "./app.component.html",
19-
styleUrl: "./app.component.scss",
20-
})
21-
export class AppComponent {
22-
title = "LangPro Annotator";
23-
24-
constructor(
25-
@Inject(DOCUMENT) private document: Document,
26-
private darkModeService: DarkModeService
27-
) {
28-
// Using the DOM API to only render on the browser instead of on the server
29-
afterRender(() => {
30-
const style = this.document.createElement("link");
31-
style.rel = "stylesheet";
32-
this.document.head.append(style);
33-
34-
this.darkModeService.theme$.subscribe((theme) => {
35-
this.document.documentElement.setAttribute(
36-
"data-bs-theme",
37-
theme
38-
);
39-
style.href = `${theme}.css`;
40-
});
41-
});
42-
}
43-
}
1+
import { Component, DestroyRef, Inject, afterRender, inject } from "@angular/core";
2+
import { DOCUMENT } from "@angular/common";
3+
import { RouterOutlet } from "@angular/router";
4+
import { MenuComponent } from "./menu/menu.component";
5+
import { FooterComponent } from "./footer/footer.component";
6+
import { DarkModeService } from "./services/dark-mode.service";
7+
import { ToastContainerComponent } from "./toast-container/toast-container.component";
8+
import { HttpClient } from "@angular/common/http";
9+
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
10+
11+
@Component({
12+
selector: "la-root",
13+
standalone: true,
14+
imports: [
15+
RouterOutlet,
16+
MenuComponent,
17+
FooterComponent,
18+
ToastContainerComponent,
19+
],
20+
templateUrl: "./app.component.html",
21+
styleUrl: "./app.component.scss",
22+
})
23+
export class AppComponent {
24+
title = "LangPro Annotator";
25+
http = inject(HttpClient);
26+
destroyRef = inject(DestroyRef);
27+
28+
constructor(
29+
@Inject(DOCUMENT) private document: Document,
30+
private darkModeService: DarkModeService
31+
) {
32+
// Using the DOM API to only render on the browser instead of on the server
33+
afterRender(() => {
34+
const style = this.document.createElement("link");
35+
style.rel = "stylesheet";
36+
this.document.head.append(style);
37+
38+
this.darkModeService.theme$.subscribe((theme) => {
39+
this.document.documentElement.setAttribute(
40+
"data-bs-theme",
41+
theme
42+
);
43+
style.href = `${theme}.css`;
44+
});
45+
});
46+
47+
this.http.get("/api/csrf").pipe(
48+
takeUntilDestroyed(this.destroyRef)
49+
).subscribe()
50+
}
51+
}

0 commit comments

Comments
 (0)