diff --git a/front/components/admin/header.vue b/front/components/admin/header.vue index 184a4fd..ea973ed 100644 --- a/front/components/admin/header.vue +++ b/front/components/admin/header.vue @@ -33,12 +33,31 @@ {{ menuItem.label }} + + + diff --git a/front/layouts/admin.vue b/front/layouts/admin.vue index e4e8990..6591e7e 100644 --- a/front/layouts/admin.vue +++ b/front/layouts/admin.vue @@ -1,8 +1,12 @@ - + diff --git a/front/middleware/toLogin.global.ts b/front/middleware/toLogin.global.ts new file mode 100644 index 0000000..62887e8 --- /dev/null +++ b/front/middleware/toLogin.global.ts @@ -0,0 +1,12 @@ +import { useAuthStore } from "~/stores/authStore" + +export default defineNuxtRouteMiddleware(async (to) => { + if ( + !process.server && + to.meta.layout === "admin" && + to.name !== "admin-login" && + !(await useAuthStore().isLogged) + ) { + return navigateTo("/admin/login") + } +}) diff --git a/front/pages/admin/login.vue b/front/pages/admin/login.vue new file mode 100644 index 0000000..0ab12ea --- /dev/null +++ b/front/pages/admin/login.vue @@ -0,0 +1,84 @@ + + + + + diff --git a/front/plugins/init-data.client.ts b/front/plugins/init-data.client.ts new file mode 100644 index 0000000..e1c9a8d --- /dev/null +++ b/front/plugins/init-data.client.ts @@ -0,0 +1,12 @@ +import { useAuthStore } from "~/stores/authStore"; + +export default defineNuxtPlugin((nuxtApp) => { + nuxtApp.hook("app:mounted", async () => { + // the data should already be fetched from SSR + // but if it's missing, we try again from the client + const authStore = useAuthStore() + if (authStore.logStatus === undefined) { + await authStore.updateLogStatus() + } + }) +}) diff --git a/front/plugins/init-data.server.ts b/front/plugins/init-data.server.ts new file mode 100644 index 0000000..2c93e19 --- /dev/null +++ b/front/plugins/init-data.server.ts @@ -0,0 +1,5 @@ +import { useAuthStore } from "~/stores/authStore"; + +export default defineNuxtPlugin((nuxtApp) => { + nuxtApp.hook("vue:setup", () => useAuthStore().updateLogStatus()) +}) diff --git a/front/stores/authStore.ts b/front/stores/authStore.ts new file mode 100644 index 0000000..6e306c3 --- /dev/null +++ b/front/stores/authStore.ts @@ -0,0 +1,28 @@ +import { defineStore } from "pinia" + +export const useAuthStore = defineStore("auth", { + state: () => + ({ + logStatus: undefined, + } as { logStatus: boolean | undefined }), + actions: { + async login(username: string, password: string) { + const res = await apiPost("auth/login/", { username, password }) + if (!res.error.value) this.logStatus = true + return res + }, + async logout() { + const res = await apiPost("auth/logout/") + if (!res.error.value) this.logStatus = false + return res + }, + async updateLogStatus() { + const res = await apiGet("auth/user/") + this.logStatus = !res.error.value + return res + }, + }, + getters: { + isLogged: (state) => state.logStatus, + }, +}) diff --git a/server/myapi/urls.py b/server/myapi/urls.py index 8a8623d..d1014ab 100644 --- a/server/myapi/urls.py +++ b/server/myapi/urls.py @@ -12,5 +12,5 @@ router.register(r"tmdb", TmdbViewSet, "tmdb") # Additionally, we include login URLs for the browsable API. urlpatterns = [ path("", include(router.urls)), - path("api-auth/", include("rest_framework.urls", namespace="rest_framework")), + path('auth/', include('dj_rest_auth.urls')), ] diff --git a/server/requirements.txt b/server/requirements.txt index 3368568..4affcb4 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -5,3 +5,4 @@ djangorestframework-camel-case==1.3.0 getconf~=1.11.1 tmdbv3api~=1.7.6 factory-boy==3.2.1 +dj-rest-auth==2.2.5 diff --git a/server/ulm_cine_club_api/settings/base.py b/server/ulm_cine_club_api/settings/base.py index 0ec01fe..687df5a 100644 --- a/server/ulm_cine_club_api/settings/base.py +++ b/server/ulm_cine_club_api/settings/base.py @@ -39,13 +39,14 @@ INSTALLED_APPS = [ "myapi.apps.MyapiConfig", "rest_framework", "corsheaders", + "dj_rest_auth", ] MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", - "django.middleware.common.CommonMiddleware", "corsheaders.middleware.CorsMiddleware", + "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", @@ -128,3 +129,4 @@ REST_FRAMEWORK = { } TMDB_API_KEY = config.getstr("tmdb.api_key") +REST_AUTH_TOKEN_MODEL = None