From: Adam Dullage Date: Wed, 12 Oct 2022 12:13:05 +0000 (+0100) Subject: Handle auth type of none in the frontend X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=a72be966a4f51000aacfdefdfcca41aa2ade38e7;p=flatnotes.git Handle auth type of none in the frontend --- diff --git a/flatnotes/auth_type.py b/flatnotes/auth_type.py index c3ad958..5644d89 100644 --- a/flatnotes/auth_type.py +++ b/flatnotes/auth_type.py @@ -1,7 +1,7 @@ from enum import Enum -class AuthType(Enum): +class AuthType(str, Enum): NONE = "none" PASSWORD = "password" # TOTP = "totp" # Not yet implemented diff --git a/flatnotes/main.py b/flatnotes/main.py index 63dbe1f..ddeb1f4 100644 --- a/flatnotes/main.py +++ b/flatnotes/main.py @@ -13,7 +13,13 @@ from error_responses import ( title_exists_response, ) from flatnotes import Flatnotes, InvalidTitleError, Note -from models import LoginModel, NoteModel, NotePatchModel, SearchResultModel +from models import ( + ConfigModel, + LoginModel, + NoteModel, + NotePatchModel, + SearchResultModel, +) app = FastAPI() flatnotes = Flatnotes(config.data_path) @@ -128,4 +134,10 @@ async def search( ] +@app.get("/api/config", response_model=ConfigModel) +async def get_config(): + """Retrieve server-side config required for the UI.""" + return ConfigModel.dump(config) + + app.mount("/", StaticFiles(directory="flatnotes/dist"), name="dist") diff --git a/flatnotes/models.py b/flatnotes/models.py index 348a7f1..fac3752 100644 --- a/flatnotes/models.py +++ b/flatnotes/models.py @@ -1,8 +1,9 @@ from typing import Dict, List, Optional -from helpers import CamelCaseBaseModel - +from config import Config from flatnotes import Note, SearchResult +from helpers import CamelCaseBaseModel +from auth_type import AuthType class LoginModel(CamelCaseBaseModel): @@ -47,3 +48,13 @@ class SearchResultModel(CamelCaseBaseModel): "contentHighlights": search_result.content_highlights, "tagMatches": search_result.tag_matches, } + + +class ConfigModel(CamelCaseBaseModel): + auth_type: AuthType + + @classmethod + def dump(self, config: Config) -> Dict: + return { + "authType": config.auth_type.value, + } diff --git a/flatnotes/src/components/App.js b/flatnotes/src/components/App.js index 0553ed9..fb7087f 100644 --- a/flatnotes/src/components/App.js +++ b/flatnotes/src/components/App.js @@ -8,9 +8,10 @@ import Logo from "./Logo"; import Mousetrap from "mousetrap"; import NavBar from "./NavBar"; import NoteViewerEditor from "./NoteViewerEditor"; -import RecentlyModified from "./RecentlyModified" +import RecentlyModified from "./RecentlyModified"; import SearchInput from "./SearchInput"; import SearchResults from "./SearchResults"; +import api from "../api"; export default { name: "App", @@ -28,6 +29,8 @@ export default { data: function() { return { + authType: null, + views: { login: 0, home: 1, @@ -42,6 +45,13 @@ export default { }, methods: { + loadConfig: function() { + let parent = this; + api.get("/api/config").then(function(response) { + parent.authType = constants.authTypes[response.data.authType]; + }); + }, + route: function() { let path = window.location.pathname.split("/"); let basePath = `/${path[1]}`; @@ -155,6 +165,8 @@ export default { return false; }); + this.loadConfig(); + let token = localStorage.getItem("token"); if (token != null) { sessionStorage.setItem("token", token); diff --git a/flatnotes/src/components/App.vue b/flatnotes/src/components/App.vue index 25654a0..f821609 100644 --- a/flatnotes/src/components/App.vue +++ b/flatnotes/src/components/App.vue @@ -12,12 +12,17 @@ v-if="currentView != views.login" class="w-100 mb-5" :show-logo="currentView != views.home" + :show-log-out="authType != null && authType != constants.authTypes.none" @logout="logout()" @search="openSearch()" > - +
diff --git a/flatnotes/src/components/NavBar.vue b/flatnotes/src/components/NavBar.vue index 1d3186f..6a939cc 100644 --- a/flatnotes/src/components/NavBar.vue +++ b/flatnotes/src/components/NavBar.vue @@ -5,14 +5,21 @@ :href="constants.basePaths.home" @click.prevent="navigate(constants.basePaths.home, $event)" > - +
+ + + New - - - A-Z