Pular para o conteúdo principal

Nuxt

The nuxt-auth repo demonstrates Casdoor integration in Nuxt. The flow is similar to the Next.js example.

Passo 1: Implementar o Casdoor

Deploy Casdoor in production mode. Confirm the login page works (e.g. at http://localhost:8000 with admin / 123 in dev).

Step 2: Add middleware

Create a .js or .ts file in the middleware directory. The filename becomes the middleware name (e.g. myMiddleware.jsmyMiddleware). Reference it in nuxt.config.js.

Example:

const protectedRoutes = ["/profile"];

export default function ({ route, redirect }) {
if (protectedRoutes.includes(route.path)) {
redirect('/login');
}
}

Enable in nuxt.config.js:

export default {
router: {
middleware: ['myMiddleware'] // your middleware name
},
}

See Nuxt middleware.

Passo 3: Usar o SDK do Casdoor

Install

npm install casdoor-js-sdk
# or: yarn add casdoor-js-sdk

Initialize

Provide these six string parameters:

ParameterObrigatórioDescrição
serverUrlSimCasdoor server URL (e.g. http://localhost:8000).
clientIdSimApplication client ID.
clientSecretSimApplication client secret.
organizationNameSimOrganization name.
appNameSimApplication name.
redirectPathSimCallback path (e.g. /callback).

Example:

const sdkConfig = {
serverUrl: "https://door.casdoor.com",
clientId: "294b09fbc17f95daf2fe",
clientSecret: "dd8982f7046ccba1bbd7851d5c1ece4e52bf039d",
organizationName: "casbin",
appName: "app-vue-python-example",
redirectPath: "/callback",
};
cuidado

Replace with your own Casdoor instance: serverUrl, clientId, and clientSecret.

Add the callback URL (e.g. http://localhost:8080/callback) in the application’s Redirect URLs.

Redirect to sign-in and handle callback

const CasdoorSDK = new Sdk(sdkConfig);
CasdoorSDK.signin_redirect();

After sign-in, exchange the code for a token and optionally store the user in a cookie:

CasdoorSDK.exchangeForAccessToken()
.then((res) => {
if (res && res.access_token) {
return CasdoorSDK.getUserInfo(res.access_token);
}
})
.then((res) => {
Cookies.set("casdoorUser", JSON.stringify(res));
});

See How to use Casdoor SDK.

Step 4: Protect routes in middleware

Check the Casdoor user cookie and redirect unauthenticated users from protected routes:

import Cookies from "js-cookie";

const protectedRoutes = ["/profile"];

export default function ({ route, redirect }) {
const casdoorUserCookie = Cookies.get('casdoorUser');
const isAuthenticated = !!casdoorUserCookie;

if (!isAuthenticated && protectedRoutes.includes(route.path)) {
redirect('/login');
}
}