summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErnst Widerberg <ernstwi@kth.se>2021-10-22 08:28:31 +0200
committerErnst Widerberg <ernstwi@kth.se>2021-10-22 08:28:31 +0200
commitaa5fd309549e84fde4eca7ad120dbc3b59d33673 (patch)
treed9d22aec1757d1820c2772cc1deafba0b14ede4c
parent4fdefd6752854c0c5fcfe848f8770aa444fd2750 (diff)
Enable login screen with mocked JWT server
-rw-r--r--README.md5
-rw-r--r--jwt_mock.go30
-rw-r--r--src/components/App.js4
-rw-r--r--src/components/Login.js3
4 files changed, 37 insertions, 5 deletions
diff --git a/README.md b/README.md
index b961dfe..ed1a91e 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,10 @@
## Development setup
-Run `soc_collector` and set env var `COLLECTOR_URL`.
+Env vars:
+- `COLLECTOR_URL`
+- `JWT_URL`
+- `PER_PAGE`
The commit pointed to by the `soc_collector` submodule is compatible with `main` in this repo.
diff --git a/jwt_mock.go b/jwt_mock.go
new file mode 100644
index 0000000..e4a46f5
--- /dev/null
+++ b/jwt_mock.go
@@ -0,0 +1,30 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+)
+
+func main() {
+ http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Access-Control-Allow-Origin", "*")
+ w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, PUT")
+ w.Header().Set("Access-Control-Allow-Headers", "Authorization")
+
+ if r.Method == "OPTIONS" {
+ w.WriteHeader(http.StatusNoContent)
+ return
+ }
+
+ auth := r.Header.Get("Authorization")
+ if auth != "Basic dXNyOnB3ZA==" { // btoa("usr:pwd")
+ w.WriteHeader(http.StatusUnauthorized)
+ return
+ }
+
+ fmt.Fprint(w, "{\"access_token\": \"JWT_TOKEN_PLACEHOLDER\"}")
+ })
+
+ log.Fatal(http.ListenAndServe(":8080", nil))
+}
diff --git a/src/components/App.js b/src/components/App.js
index 9ead9c5..636c672 100644
--- a/src/components/App.js
+++ b/src/components/App.js
@@ -56,8 +56,8 @@ class App extends React.Component {
clearToken={this.clearToken}
/>
);
- // if (this.state.token === null)
- // return <Login setToken={this.setToken} setError={this.setError} />;
+ if (this.state.token === null)
+ return <Login setToken={this.setToken} setError={this.setError} />;
return (
<Router>
<Header clearToken={this.clearToken} />
diff --git a/src/components/Login.js b/src/components/Login.js
index 209871e..9b45817 100644
--- a/src/components/Login.js
+++ b/src/components/Login.js
@@ -26,8 +26,7 @@ class Login extends React.Component {
// NOTE: btoa() limits email, password to ASCII
login(email, password) {
- const url = process.env.JWT_URL + "/api/v1.0/auth";
- fetch(url, {
+ fetch(process.env.JWT_URL, {
method: "POST",
headers: { Authorization: "Basic " + btoa(email + ":" + password) }
})