Compare commits
6 Commits
a2860478a9
...
v0.1.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 5bda835566 | |||
| 6f6e0d7b76 | |||
| c1094239a3 | |||
| f9958701e6 | |||
| dbbb2f7009 | |||
| d0244eb6a3 |
7
.vscode/settings.json
vendored
7
.vscode/settings.json
vendored
@@ -1,10 +1,11 @@
|
|||||||
{
|
{
|
||||||
"files.associations": {
|
|
||||||
"*.css": "tailwindcss"
|
|
||||||
},
|
|
||||||
"editor.quickSuggestions": {
|
"editor.quickSuggestions": {
|
||||||
"strings": "on"
|
"strings": "on"
|
||||||
},
|
},
|
||||||
|
"files.associations": {
|
||||||
|
"*.css": "tailwindcss"
|
||||||
|
},
|
||||||
|
"graphql-config.load.rootDir": "wp-content/themes/moonshine",
|
||||||
"tailwindCSS.classAttributes": [
|
"tailwindCSS.classAttributes": [
|
||||||
"class",
|
"class",
|
||||||
"ui"
|
"ui"
|
||||||
|
|||||||
@@ -1,5 +1,20 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## v0.1.2
|
||||||
|
|
||||||
|
[compare changes](https://gitea.websimple.com/templates/wp-headless/compare/v0.1.1...v0.1.2)
|
||||||
|
|
||||||
|
### 🚀 Enhancements
|
||||||
|
|
||||||
|
- Initial Nuxt UI configuration (ca2e660)
|
||||||
|
- Initial layout with SiteHeader / SiteFooter (3d7a2b2)
|
||||||
|
- Update .gitignore and add Copilot instructions (f520db7)
|
||||||
|
- Optional SSL for dev server (9b6a86f)
|
||||||
|
- Typecheck npm script (33589d4)
|
||||||
|
- Initial theme setup (theme features, locale, main menu) (a286047)
|
||||||
|
- Initial GraphQL setup with remote WP schema (d0244eb)
|
||||||
|
- Initial authentication logic and UX (c109423)
|
||||||
|
|
||||||
## v0.1.1
|
## v0.1.1
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
13
wp-content/themes/moonshine/app/app.config.ts
Normal file
13
wp-content/themes/moonshine/app/app.config.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
export default defineAppConfig({
|
||||||
|
ui: {
|
||||||
|
colors: {
|
||||||
|
primary: "indigo",
|
||||||
|
neutral: "neutral",
|
||||||
|
},
|
||||||
|
button: {
|
||||||
|
slots: {
|
||||||
|
base: "cursor-pointer",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -1,2 +1,4 @@
|
|||||||
@import "tailwindcss";
|
@import "tailwindcss";
|
||||||
@import "@nuxt/ui";
|
@import "@nuxt/ui";
|
||||||
|
|
||||||
|
@import "./containers.css";
|
||||||
|
|||||||
47
wp-content/themes/moonshine/app/assets/css/containers.css
Normal file
47
wp-content/themes/moonshine/app/assets/css/containers.css
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
:root {
|
||||||
|
--ui-container: var(--breakpoint-2xl);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Container padding */
|
||||||
|
@utility px-container {
|
||||||
|
@apply px-4 sm:px-6 lg:px-8;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Container sizes */
|
||||||
|
@utility container { @apply mx-auto px-container max-w-(--breakpoint-2xl); }
|
||||||
|
@utility container-xl { @apply container max-w-(--breakpoint-xl); }
|
||||||
|
@utility container-lg { @apply container max-w-(--breakpoint-lg); }
|
||||||
|
@utility container-md { @apply container max-w-(--breakpoint-md); }
|
||||||
|
@utility container-sm { @apply container max-w-(--breakpoint-sm); }
|
||||||
|
@utility container-fluid { @apply container max-w-screen; }
|
||||||
|
@utility container-none { @apply w-full max-w-screen; }
|
||||||
|
|
||||||
|
/* Split containers */
|
||||||
|
:root {
|
||||||
|
--container-outside-margin: 0;
|
||||||
|
--container-width: 100vw;
|
||||||
|
@variant sm {
|
||||||
|
--container-outside-margin: calc(50vw - theme("screens.sm") / 2);
|
||||||
|
--container-width: theme("screens.sm");
|
||||||
|
}
|
||||||
|
@variant md {
|
||||||
|
--container-outside-margin: calc(50vw - theme("screens.md") / 2);
|
||||||
|
--container-width: theme("screens.md");
|
||||||
|
}
|
||||||
|
@variant lg {
|
||||||
|
--container-outside-margin: calc(50vw - theme("screens.lg") / 2);
|
||||||
|
--container-width: theme("screens.lg");
|
||||||
|
}
|
||||||
|
@variant xl {
|
||||||
|
--container-outside-margin: calc(50vw - theme("screens.xl") / 2);
|
||||||
|
--container-width: theme("screens.xl");
|
||||||
|
}
|
||||||
|
@variant 2xl {
|
||||||
|
--container-outside-margin: calc(50vw - theme("screens.2xl") / 2);
|
||||||
|
--container-width: theme("screens.2xl");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@utility container-left { @apply ml-(--container-outside-margin) px-container;}
|
||||||
|
@utility container-right { @apply mr-(--container-outside-margin) px-container;}
|
||||||
|
@utility container-half { width: calc(var(--container-width) / 2);}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
const { isLoggedIn } = useAuth();
|
||||||
|
const attrs = computed(() => {
|
||||||
|
return isLoggedIn.value
|
||||||
|
? {
|
||||||
|
label: "Déconnexion",
|
||||||
|
icon: "i-lucide-log-out",
|
||||||
|
}
|
||||||
|
: {
|
||||||
|
label: "Connexion",
|
||||||
|
icon: "i-lucide-log-in",
|
||||||
|
};
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<AuthState>
|
||||||
|
<UButton to="/connexion" v-bind="attrs" color="neutral" />
|
||||||
|
</AuthState>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
const { login } = useAuthConnexion();
|
||||||
|
const fields = [
|
||||||
|
{
|
||||||
|
name: "username",
|
||||||
|
type: "text" as const,
|
||||||
|
label: "Courriel",
|
||||||
|
placeholder: "Entrez votre courriel",
|
||||||
|
required: true,
|
||||||
|
}, {
|
||||||
|
name: "password",
|
||||||
|
label: "Mot de passe",
|
||||||
|
type: "password" as const,
|
||||||
|
placeholder: "Entrez votre mot de passe",
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<UAuthForm
|
||||||
|
:schema="authLoginFormSchema"
|
||||||
|
:fields="fields"
|
||||||
|
title="Connexion"
|
||||||
|
description="Veuillez vous identifier."
|
||||||
|
loading-auto
|
||||||
|
@submit="login"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
const { logout } = useAuthConnexion();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="w-full space-y-6">
|
||||||
|
<div class="flex flex-col text-center">
|
||||||
|
<div class="text-xl text-pretty font-semibold text-highlighted">
|
||||||
|
Déconnexion
|
||||||
|
</div>
|
||||||
|
<div class="mt-1 text-base text-pretty text-muted">
|
||||||
|
Veuillez confirmer la déconnexion.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<UButton
|
||||||
|
icon="i-lucide-log-out"
|
||||||
|
block
|
||||||
|
loading-auto
|
||||||
|
to="#"
|
||||||
|
label="Déconnexion"
|
||||||
|
@click="logout()"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<template>
|
||||||
|
<div class="w-full space-y-6">
|
||||||
|
<div class="flex flex-col text-center">
|
||||||
|
<div class="text-xl text-pretty font-semibold text-highlighted">
|
||||||
|
Redirection en cours
|
||||||
|
</div>
|
||||||
|
<div class="mt-1 text-base text-pretty text-muted">
|
||||||
|
Veuillez patienter...
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
const { isLoggedIn } = useAuth();
|
||||||
|
const { isRedirecting } = useAuthConnexion();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<section data-section-name="auth-connexion" class="py-12">
|
||||||
|
<div class="container-sm">
|
||||||
|
<AuthState>
|
||||||
|
<AuthRedirecting v-if="isRedirecting" />
|
||||||
|
<template v-else>
|
||||||
|
<AuthLogoutForm v-if="isLoggedIn" />
|
||||||
|
<AuthLoginForm v-else />
|
||||||
|
</template>
|
||||||
|
</AuthState>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</template>
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
const { data } = await useGraphQLQuery("GeneralSettings", undefined, { cache: { ttl: 0 } });
|
||||||
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
© {{ new Date().getFullYear() }}
|
© {{ new Date().getFullYear() }}
|
||||||
|
<span v-if="data.generalSettings?.title">{{ data.generalSettings.title }}</span>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -3,5 +3,9 @@ const title = "Moonshine";
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<UHeader :title="title" />
|
<UHeader :title="title">
|
||||||
|
<template #right>
|
||||||
|
<AuthConnexionButton />
|
||||||
|
</template>
|
||||||
|
</UHeader>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
6
wp-content/themes/moonshine/app/composables/useAuth.ts
Normal file
6
wp-content/themes/moonshine/app/composables/useAuth.ts
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
export function useAuth() {
|
||||||
|
const { loggedIn: isLoggedIn, session } = useUserSession();
|
||||||
|
const hasRole = (role: string) => session.value?.user?.roles?.includes(role) || false;
|
||||||
|
const isAdmin = computed(() => hasRole("administrator"));
|
||||||
|
return { isLoggedIn, hasRole, isAdmin };
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
import z from "zod";
|
||||||
|
import type { FormSubmitEvent } from "@nuxt/ui";
|
||||||
|
|
||||||
|
export const authLoginFormSchema = z.object({
|
||||||
|
username: z.email("Courriel invalide"),
|
||||||
|
password: z.string("Veuillez saisir votre mot de passe"),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type AuthLoginForm = z.infer<typeof authLoginFormSchema>;
|
||||||
|
|
||||||
|
const isRedirecting = ref(false);
|
||||||
|
|
||||||
|
export function useAuthConnexion() {
|
||||||
|
const { fetch: refreshUserSession } = useUserSession();
|
||||||
|
const routeRedirect = useRoute().query.redirect as string || undefined;
|
||||||
|
|
||||||
|
// Helper: Redirect after login / logout
|
||||||
|
async function redirectTo(to: string | undefined) {
|
||||||
|
isRedirecting.value = true;
|
||||||
|
await delay(1000);
|
||||||
|
await refreshUserSession();
|
||||||
|
await navigateTo(to || routeRedirect || "/");
|
||||||
|
isRedirecting.value = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Login
|
||||||
|
const { mutate: loginMutate } = useGraphQLMutation("AuthLogin");
|
||||||
|
async function login({ data: variables }: FormSubmitEvent<AuthLoginForm>, redirect?: string) {
|
||||||
|
try {
|
||||||
|
const { data } = await loginMutate(variables);
|
||||||
|
if (!data.login) {
|
||||||
|
throw new Error(`Échec de la connexion par mot de passe.`);
|
||||||
|
}
|
||||||
|
await redirectTo(redirect);
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logout
|
||||||
|
async function logout(redirect?: string) {
|
||||||
|
try {
|
||||||
|
const result = await $fetch("/api/logout", { method: "POST" });
|
||||||
|
if (!result.success) {
|
||||||
|
throw new Error("Échec de la déconnexion.");
|
||||||
|
}
|
||||||
|
await redirectTo(redirect);
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { isRedirecting, login, logout };
|
||||||
|
}
|
||||||
28
wp-content/themes/moonshine/app/error.vue
Normal file
28
wp-content/themes/moonshine/app/error.vue
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { fr } from "@nuxt/ui/locale";
|
||||||
|
import type { NuxtError } from "#app";
|
||||||
|
|
||||||
|
const props = defineProps<{ error: NuxtError }>();
|
||||||
|
const formattedError = computed(() => {
|
||||||
|
const error = {
|
||||||
|
statusCode: props.error.statusCode,
|
||||||
|
statusMessage: props.error.statusMessage,
|
||||||
|
message: props.error.message,
|
||||||
|
};
|
||||||
|
switch (error.statusCode) {
|
||||||
|
case 404:
|
||||||
|
error.statusMessage = "Page non trouvée";
|
||||||
|
break;
|
||||||
|
case 500:
|
||||||
|
error.message = "Erreur interne du serveur.";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return error;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<UApp :locale="fr">
|
||||||
|
<UError :error="formattedError" />
|
||||||
|
</UApp>
|
||||||
|
</template>
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
fragment AuthUser on User {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
roles {
|
||||||
|
nodes {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mutation AuthLogin($username: String!, $password: String!) {
|
||||||
|
login( input: { provider: PASSWORD, credentials: { username: $username, password: $password }}) {
|
||||||
|
authToken
|
||||||
|
refreshToken
|
||||||
|
user {
|
||||||
|
...AuthUser
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
mutation AuthRefreshToken($refreshToken: String!) {
|
||||||
|
refreshToken( input: { refreshToken: $refreshToken }) {
|
||||||
|
authToken
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
query GeneralSettings {
|
||||||
|
generalSettings {
|
||||||
|
title
|
||||||
|
description
|
||||||
|
}
|
||||||
|
}
|
||||||
9
wp-content/themes/moonshine/app/pages/connexion.vue
Normal file
9
wp-content/themes/moonshine/app/pages/connexion.vue
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div id="page-connexion">
|
||||||
|
<SectionAuthConnexion />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
4
wp-content/themes/moonshine/graphql.config.json
Normal file
4
wp-content/themes/moonshine/graphql.config.json
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"schema": "./server/graphql/schema.graphql",
|
||||||
|
"documents": "**/*.gql"
|
||||||
|
}
|
||||||
@@ -2,8 +2,10 @@
|
|||||||
export default defineNuxtConfig({
|
export default defineNuxtConfig({
|
||||||
|
|
||||||
modules: [
|
modules: [
|
||||||
|
"@lewebsimple/nuxt-graphql",
|
||||||
"@nuxt/eslint",
|
"@nuxt/eslint",
|
||||||
"@nuxt/ui",
|
"@nuxt/ui",
|
||||||
|
"nuxt-auth-utils",
|
||||||
],
|
],
|
||||||
|
|
||||||
components: {
|
components: {
|
||||||
@@ -20,13 +22,6 @@ export default defineNuxtConfig({
|
|||||||
colorMode: false,
|
colorMode: false,
|
||||||
},
|
},
|
||||||
|
|
||||||
devServer: {
|
|
||||||
https: {
|
|
||||||
key: process.env.LOCAL_HTTPS_KEY,
|
|
||||||
cert: process.env.LOCAL_HTTPS_CERT,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
compatibilityDate: "2026-01-01",
|
compatibilityDate: "2026-01-01",
|
||||||
|
|
||||||
eslint: {
|
eslint: {
|
||||||
@@ -40,4 +35,16 @@ export default defineNuxtConfig({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
graphql: {
|
||||||
|
context: "server/graphql/context.ts",
|
||||||
|
schemas: {
|
||||||
|
wp: {
|
||||||
|
type: "remote",
|
||||||
|
url: `${process.env.NUXT_WP_URL || "https://wp-headless.ledevsimple.ca"}/graphql`,
|
||||||
|
middleware: "server/graphql/wp-middleware.ts",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
saveSdl: "server/graphql/schema.graphql",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@lewebsimple/moonshine",
|
"name": "@lewebsimple/moonshine",
|
||||||
"description": "Headless WordPress theme based on Nuxt.",
|
"description": "Headless WordPress theme based on Nuxt.",
|
||||||
"version": "0.1.1",
|
"version": "0.1.2",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "nuxt build",
|
"build": "nuxt build",
|
||||||
"dev": "nuxt dev --host 0.0.0.0",
|
"dev": "nuxt dev",
|
||||||
"lint": "eslint --fix .",
|
"lint": "eslint --fix .",
|
||||||
"postinstall": "pnpm --sequential /postinstall:.*/",
|
"postinstall": "pnpm --sequential /postinstall:.*/",
|
||||||
"postinstall:nuxt": "nuxt prepare",
|
"postinstall:nuxt": "nuxt prepare",
|
||||||
@@ -16,11 +16,15 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@iconify-json/lucide": "^1.2.84",
|
"@iconify-json/lucide": "^1.2.84",
|
||||||
|
"@lewebsimple/nuxt-graphql": "^0.4.0",
|
||||||
"@nuxt/ui": "4.3.0",
|
"@nuxt/ui": "4.3.0",
|
||||||
|
"jwt-decode": "^4.0.0",
|
||||||
"nuxt": "^4.2.2",
|
"nuxt": "^4.2.2",
|
||||||
|
"nuxt-auth-utils": "^0.5.27",
|
||||||
"tailwindcss": "^4.1.18",
|
"tailwindcss": "^4.1.18",
|
||||||
"vue": "^3.5.26",
|
"vue": "^3.5.26",
|
||||||
"vue-router": "^4.6.4"
|
"vue-router": "^4.6.4",
|
||||||
|
"zod": "^4.3.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nuxt/eslint": "^1.12.1",
|
"@nuxt/eslint": "^1.12.1",
|
||||||
@@ -29,6 +33,12 @@
|
|||||||
"typescript": "^5.9.3",
|
"typescript": "^5.9.3",
|
||||||
"vue-tsc": "^3.2.2"
|
"vue-tsc": "^3.2.2"
|
||||||
},
|
},
|
||||||
|
"pnpm": {
|
||||||
|
"overrides": {
|
||||||
|
"@tiptap/core": "3.14.0",
|
||||||
|
"@tiptap/pm": "3.14.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"changelog": {
|
"changelog": {
|
||||||
"types": {
|
"types": {
|
||||||
"chore": false
|
"chore": false
|
||||||
|
|||||||
3082
wp-content/themes/moonshine/pnpm-lock.yaml
generated
3082
wp-content/themes/moonshine/pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
5
wp-content/themes/moonshine/pnpm-workspace.yaml
Normal file
5
wp-content/themes/moonshine/pnpm-workspace.yaml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
onlyBuiltDependencies:
|
||||||
|
- '@parcel/watcher'
|
||||||
|
- esbuild
|
||||||
|
- unrs-resolver
|
||||||
|
- vue-demi
|
||||||
12
wp-content/themes/moonshine/server/api/logout.post.ts
Normal file
12
wp-content/themes/moonshine/server/api/logout.post.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { defineEventHandler } from "h3";
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
try {
|
||||||
|
await handleLogout(event);
|
||||||
|
return { success: true, message: "Déconnexion réussie" };
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
const message = error instanceof Error ? error.message : "Une erreur est survenue lors de la déconnexion.";
|
||||||
|
return { success: false, message };
|
||||||
|
}
|
||||||
|
});
|
||||||
11
wp-content/themes/moonshine/server/graphql/context.ts
Normal file
11
wp-content/themes/moonshine/server/graphql/context.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { defineGraphQLContext } from "@lewebsimple/nuxt-graphql/helpers";
|
||||||
|
import type { AuthLoginMutation } from "#graphql/typed-documents";
|
||||||
|
|
||||||
|
export default defineGraphQLContext(async (event) => {
|
||||||
|
const authToken = await getAuthToken(event);
|
||||||
|
|
||||||
|
return {
|
||||||
|
authToken,
|
||||||
|
handleLogin: async (loginData: AuthLoginMutation) => handleLogin(event, loginData),
|
||||||
|
};
|
||||||
|
});
|
||||||
14008
wp-content/themes/moonshine/server/graphql/schema.graphql
Normal file
14008
wp-content/themes/moonshine/server/graphql/schema.graphql
Normal file
File diff suppressed because it is too large
Load Diff
20
wp-content/themes/moonshine/server/graphql/wp-middleware.ts
Normal file
20
wp-content/themes/moonshine/server/graphql/wp-middleware.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import type { AuthLoginMutation } from "#build/graphql/typed-documents";
|
||||||
|
import { defineRemoteExecMiddleware } from "@lewebsimple/nuxt-graphql/helpers";
|
||||||
|
|
||||||
|
export default defineRemoteExecMiddleware({
|
||||||
|
onRequest({ context, fetchOptions }) {
|
||||||
|
// Attach auth token from context to request headers
|
||||||
|
if (context.authToken) {
|
||||||
|
fetchOptions.headers.set("Authorization", `Bearer ${context.authToken}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async onResponse({ operationName, response, context }) {
|
||||||
|
// Save auth token in user session
|
||||||
|
if (operationName === "AuthLogin") {
|
||||||
|
const { data } = await response.json() as { data?: AuthLoginMutation };
|
||||||
|
if (data) {
|
||||||
|
await context.handleLogin(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
75
wp-content/themes/moonshine/server/utils/auth.ts
Normal file
75
wp-content/themes/moonshine/server/utils/auth.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import type { H3Event } from "h3";
|
||||||
|
import { GraphQLClient } from "graphql-request";
|
||||||
|
import { jwtDecode } from "jwt-decode";
|
||||||
|
import type { User } from "#auth-utils";
|
||||||
|
import { type AuthUserFragment, type AuthLoginMutation, AuthRefreshTokenDocument } from "#graphql/typed-documents";
|
||||||
|
|
||||||
|
// Handle login result and store user session
|
||||||
|
export async function handleLogin(event: H3Event, loginData: AuthLoginMutation) {
|
||||||
|
if (!loginData?.login) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { user, authToken, refreshToken } = loginData.login;
|
||||||
|
if (!user || !authToken || !refreshToken) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await setUserSession(event, {
|
||||||
|
user: getAuthUser(user),
|
||||||
|
secure: {
|
||||||
|
authToken,
|
||||||
|
refreshToken,
|
||||||
|
},
|
||||||
|
loggedInAt: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle user logout by clearing session
|
||||||
|
export async function handleLogout(event: H3Event) {
|
||||||
|
await clearUserSession(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert AuthUserFragment to nuxt-auth-utils User
|
||||||
|
function getAuthUser(user: AuthUserFragment): User {
|
||||||
|
return {
|
||||||
|
id: Number(user.id),
|
||||||
|
email: user.email!,
|
||||||
|
roles: extractNodes(user.roles).map(({ name }) => name!) || [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Refresh auth token by calling remote GraphQL endpoint directly
|
||||||
|
export async function refreshAuthToken(refreshToken: string): Promise<string | undefined> {
|
||||||
|
const client = new GraphQLClient(`${process.env.NUXT_WP_URL || "https://wp-headless.ledevsimple.ca"}/graphql`);
|
||||||
|
const data = await client.request(AuthRefreshTokenDocument, { refreshToken });
|
||||||
|
return data.refreshToken?.authToken || undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get auth token from user session (refresh if needed)
|
||||||
|
export async function getAuthToken(event: H3Event): Promise<string | undefined> {
|
||||||
|
// Retrieve user session, return if none
|
||||||
|
const session = await getUserSession(event);
|
||||||
|
if (!session.secure) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract tokens and check expiration
|
||||||
|
const { authToken, refreshToken } = session.secure;
|
||||||
|
const decoded = jwtDecode<{ exp: number }>(authToken);
|
||||||
|
const isExpired = decoded.exp * 1000 < Date.now();
|
||||||
|
if (isExpired) {
|
||||||
|
try {
|
||||||
|
const newAuthToken = await refreshAuthToken(refreshToken);
|
||||||
|
if (!newAuthToken) {
|
||||||
|
throw new Error("Impossible de rafraîchir le jeton d'authentification.");
|
||||||
|
}
|
||||||
|
session.secure.authToken = newAuthToken;
|
||||||
|
await setUserSession(event, session);
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
await clearUserSession(event);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return session.secure.authToken;
|
||||||
|
}
|
||||||
19
wp-content/themes/moonshine/shared/types/auth.d.ts
vendored
Normal file
19
wp-content/themes/moonshine/shared/types/auth.d.ts
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
// auth.d.ts
|
||||||
|
declare module "#auth-utils" {
|
||||||
|
interface User {
|
||||||
|
id: number;
|
||||||
|
email: string;
|
||||||
|
roles: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UserSession {
|
||||||
|
loggedInAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SecureSessionData {
|
||||||
|
authToken: string;
|
||||||
|
refreshToken: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { };
|
||||||
3
wp-content/themes/moonshine/shared/utils/delay.ts
Normal file
3
wp-content/themes/moonshine/shared/utils/delay.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export async function delay(ms: number) {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
4
wp-content/themes/moonshine/shared/utils/graphql.ts
Normal file
4
wp-content/themes/moonshine/shared/utils/graphql.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// Helper: Extracts nodes from a GraphQL connection object, returning an empty array if nodes are absent.
|
||||||
|
export function extractNodes<T>(connection: { nodes?: T[] } | null | undefined): T[] {
|
||||||
|
return connection?.nodes || [] as T[];
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user