🪟 Windows TippsSeptemberaktion: Office 2024 für 28 Euro & Win 11 ab 10 Euro(17.09.2026 um 13:28 Uhr)
🪟 Windows ServerAuch Druckerprobleme nach September-Updates - Swiss IT Magazine(17.09.2026 um 16:32 Uhr)
🪟 Windows ServerWindows Update sperrt Domänen-Nutzer aus | Nau.ch(17.09.2026 um 16:36 Uhr)
🪟 Windows ServerKB5124008 Domain Trust Fehler: Ursache und Fix - WindowsPower.de(17.09.2026 um 17:18 Uhr)
🪟 Windows TippsSeptemberaktion: Office 2024 für 28 Euro & Win 11 ab 10 Euro(17.09.2026 um 13:28 Uhr)
🪟 Windows ServerAuch Druckerprobleme nach September-Updates - Swiss IT Magazine(17.09.2026 um 16:32 Uhr)
🪟 Windows ServerWindows Update sperrt Domänen-Nutzer aus | Nau.ch(17.09.2026 um 16:36 Uhr)
🪟 Windows ServerKB5124008 Domain Trust Fehler: Ursache und Fix - WindowsPower.de(17.09.2026 um 17:18 Uhr)
🔧 Programmierung 🕛 vor 1 Jahr 5 Min Lesezeit
0

Secure Your Nuxt 3 App

↗ Quelle (dev.to)
🗣️ Stimme:


What is @workmate/nuxt-auth?

@workmate/nuxt-auth is a package designed to make authentication in Nuxt.js apps seamless. This package provides a straightforward way to add authentication flows, such as login, registration, and user session management, to your Nuxt app.



Installing @workmate/nuxt-auth

The first step is to install the package. Open your terminal and navigate to the root of your Nuxt project. Then, run the following command to install the @workmate/nuxt-auth package:




CODE
npm install --save @workmate/nuxt-auth






or if you are using yarn:




CODE
yarn add @workmate/nuxt-auth






Setting Up the Package in Your Nuxt Project

Once the package is installed, you need to configure it in your Nuxt application. Open your nuxt.config.js file and add @workmate/nuxt-auth to the modules array:




CODE
// nuxt.config.ts
export default defineNuxtConfig({
modules: [
"@workmate/nuxt-auth"
],
...
});






Set up your auth providers




CODE
// nuxt.config.ts
const BACKEND_URL = process.env.BACKEND_BASE_URL || "http://localhost:9000";

export default defineNuxtConfig({
modules: [ "@workmate/nuxt-auth",],

auth: {
global: true,
redirects: {
redirectIfLoggedIn: "/dashboard",
redirectIfNotLoggedIn: "/register", // default is /login
},
apiClient: {
baseURL: BACKEND_URL,
},
//token: {
// type: "Bearer",
// maxAge: 1000 * 60 * 60 * 24 * 30,
// cookiesNames: {
// accessToken: "auth:token",
// refreshToken: "auth:refreshToken",
// authProvider: "auth:provider",
// tokenType: "auth:tokenType",
//}
};
providers: {
local: {
endpoints: {
signIn: {
path: `${BACKEND_URL}/api/auth/login`,
method: "POST",
tokenKey: "token",
body: {
principal: "email",
password: "password",
},
},
user: {
path: `${BACKEND_URL}/api/auth/user`,
userKey: "data",
},
signOut: {
path: `${BACKEND_URL}/api/auth/user`,
method: "POST",
},
},
},
},
},
});






Brief explanation of the config

Let's go through the above real quick, the global:true line ensures that the auth middleware is set on every page of your website. And since you won't be authenticated by default, you want to turn it off on the home route like so




CODE
// pages/index.vue
<template>
<main>Landing page</main>
</template>

<script lang="ts" setup>
definePageMeta({
auth: false,
});
<script>






You can also set the auth to false on pages you want your users to access regardless of whether they are logged in or not like the help page or the about us page.



In the case where you don't want to use the global middleware, add the auth middleware to the pages you want to protect, eg the dashboard




CODE
// pages/dashboard.vue
<template>
<main>Dashboard page</main>
</template>

<script lang="ts" setup>
definePageMeta({
middleware: "auth",
});
<script>






The signIn endpoint

In the above config, the application would make a post request to the endpoint ${BACKEND_URL}/api/auth/login with the body




CODE
{
"email": "[email protected]",
"password": "password"
}






and would expect data with the format




CODE
{
"token": "auth token",
}






for nested data, use 'period' separated keys eg nested.token.data.



The same foes for the user endpoint and the sign out endpoint.



The api client

By default, nuxt would want you to send requests to your frontend domain, meanwhile you might want to send it to another (eg. the backend). This package automatically handles this and also adds the authorization headers to the api client so you don't have to manually do that.



To use the api client, add the apiClient's baseurl to the config as shown in the code above. Then instead of calling useFetch or $fetch in nuxt use




CODE
useAuthFetch(url, options)
const { $authFetch } = useNuxtApp();

$authFetch(url, options)






And they have the exact same interface with the useFetch and $fetch apis.



Logging In




CODE
// pages/index.vue
<template>
<form @submit.prevent="submit" class="login-form">
<div class="form-group">
<label for="email">Email</label>
<input
type="email"
id="email"
v-model="email"
placeholder="Enter your email"
required
/>
</div>

<div class="form-group">
<label for="password">Password</label>
<input
type="password"
id="password"
v-model="password"
placeholder="Enter your password"
required
/>
</div>

<button type="submit">Login</button>
</form>
</template>

<script lang="ts" setup>
definePageMeta({
middleware: 'auth-guest',
});

const email = ref();
const password = ref();

const { login } = useAuth();

function submit() {
login("local", {
principal: email.value,
password: password.value,
}).catch(err => {
//handle error
})
}
<script>






The auth guest middleware allows only unauthenticated users to visit a page, which is what we need in the case of the login.



Auth Data

In order to get the auth data, you can use the useAuth composable




CODE
const {
loggedIn,
user,
token,
refreshToken,
login,
logout,
refreshUser,
refreshTokens,
} = useAuth();

// or
const { $auth } = useNuxtApp();
const {
loggedIn,
user,
token,
refreshToken,
login,
logout,
refreshUser,
refreshTokens,
} = $auth;






In order to logout, you can use the logout from the above code snippet




CODE
logout().then(() => {
// show logout notification
});


Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Avision AD7100 & AD7100N - Dreifach kontrolliert gegen Doppelblätter und Papierstau
1 Quelle
Windows Server 2022: Mainstream-Support endet am 13. Oktober - ad-hoc-news.de
1 Quelle
Lenovo ThinkAgile VX850 V4: Neue Infrastruktur für KI und Virtualisierung - ad-hoc-news.de
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Secure Your Nuxt 3 App

Thematisch verwandte Begriffe: Secure, Your, Nuxt · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...