UNPKG

@xsprtd/nuxt-api

Version:

Nuxt API Authentication and Http Client

42 lines (41 loc) 1.29 kB
import { useAuth } from "./useAuth.js"; import { useApiOptions } from "./useApiOptions.js"; export const useAuthMiddleware = () => { const { isLoggedIn } = useAuth(); const options = useApiOptions(); const checkAuth = (to) => { if (isLoggedIn.value) { return { isAuthenticated: true, redirectTo: null }; } const loginPath = options.redirect.login; if (loginPath) { const redirect = { path: loginPath }; if (options.redirect.intendedEnabled) { redirect.query = { redirect: to.fullPath }; } return { isAuthenticated: false, redirectTo: redirect }; } return { isAuthenticated: false, redirectTo: null }; }; const checkGuest = (to) => { if (!isLoggedIn.value) { return { isAuthenticated: false, redirectTo: null }; } const { intendedEnabled, postLogin } = options.redirect; if (intendedEnabled) { const requestedRoute = to.query.redirect; if (requestedRoute && requestedRoute !== to.path) { return { isAuthenticated: true, redirectTo: requestedRoute }; } } if (postLogin) { return { isAuthenticated: true, redirectTo: postLogin }; } return { isAuthenticated: true, redirectTo: null }; }; return { isLoggedIn, checkAuth, checkGuest }; };