diff --git a/package.json b/package.json index d55472b..d171719 100644 --- a/package.json +++ b/package.json @@ -17,11 +17,10 @@ "build": "run-p type-check build-only build-style && run-p build-minify", "build-minify": "uglifyjs dist/servicestack-vue.mjs --compress --mangle -o dist/servicestack-vue.min.mjs", "build-style": "postcss src/tailwind.css -o dist/styles.css", - "build-copy": "npm run build && npm run copy-src && npm run copy-coffeeshop", + "build-copy": "npm run build && npm run copy-src", "copy-src": "shx cp dist/servicestack-vue.min.mjs ../ServiceStack/ServiceStack/src/ServiceStack/js/servicestack-vue.mjs && shx cp dist/servicestack-vue.mjs ../ServiceStack/ServiceStack/tests/NorthwindAuto/wwwroot/lib/mjs/", "copy-diffusion": "shx cp dist/servicestack-vue.m* ../../netcore/VueDiffusion/MyApp/wwwroot/lib/mjs/ && shx cp dist/index.d.ts ../../netcore/VueDiffusion/MyApp/wwwroot/lib/typings/@servicestack/vue/", "copy-creatorkit": "shx cp dist/servicestack-vue.m* ../../netcore/CreatorKit/CreatorKit/wwwroot/lib/mjs/ && shx cp dist/index.d.ts ../../netcore/CreatorKit/CreatorKit/wwwroot/lib/typings/@servicestack/vue/", - "copy-coffeeshop": "shx cp dist/servicestack-vue.m* ../../netcore/CoffeeShop/CoffeeShop/wwwroot/lib/mjs/ && shx cp dist/index.d.ts ../../netcore/CoffeeShop/CoffeeShop/wwwroot/lib/typings/@servicestack/vue/", "preview": "vite preview", "build-only": "vite build -l error", "type-check": "vue-tsc --noEmit", diff --git a/src/use/auth.ts b/src/use/auth.ts index baaf0c6..494f16c 100644 --- a/src/use/auth.ts +++ b/src/use/auth.ts @@ -1,10 +1,17 @@ import type { AuthenticateResponse, MetadataOperationType } from "@/types" import { computed } from "vue" import { Sole } from "./config" +import { sanitize } from "@servicestack/client" + +function toAuth(auth?:AuthenticateResponse) { + return auth && (auth as any).SessionId + ? sanitize(auth) + : auth +} /** Sign In the currently Authenticated User */ function signIn(user:AuthenticateResponse) { - Sole.user.value = user + Sole.user.value = toAuth(user)! Sole.events.publish('signIn', user) } @@ -14,14 +21,19 @@ function signOut() { Sole.events.publish('signOut', null) } +/** @returns {string[]} */ +const getRoles = (user:any) => user.roles || [] +/** @returns {string[]} */ +const getPermissions = (user:any) => user.permissions || [] + /** Check if the Authenticated User has a specific role */ function hasRole(role:string) { - return (Sole.user.value?.roles || []).indexOf(role) >= 0 + return getRoles(Sole.user.value).indexOf(role) >= 0 } /** Check if the Authenticated User has a specific permission */ function hasPermission(permission:string) { - return (Sole.user.value?.permissions || []).indexOf(permission) >= 0 + return getPermissions(Sole.user.value).indexOf(permission) >= 0 } /** Check if the Authenticated User has the Admin role */ @@ -39,7 +51,7 @@ export function canAccess(op?:MetadataOperationType|null) { return false if (isAdmin()) return true - let [roles, permissions] = [auth.roles || [], auth.permissions || []] + let [roles, permissions] = [getRoles(auth), getPermissions(auth)] let [requiredRoles, requiredPermissions, requiresAnyRole, requiresAnyPermission] = [ op.requiredRoles || [], op.requiredPermissions || [], op.requiresAnyRole || [], op.requiresAnyPermission || []] if (!requiredRoles.every(role => roles.indexOf(role) >= 0)) @@ -62,8 +74,8 @@ export function invalidAccessMessage(op:MetadataOperationType) { } if (isAdmin()) return null; - let [roles, permissions] = [auth.roles || [], auth.permissions || []] - let [requiredRoles, requiredPermissions, requiresAnyRole, requiresAnyPermission] = [ + let [roles, permissions] = [getRoles(auth), getPermissions(auth)] + let [requiredRoles, requiredPermissions, requiresAnyRole, requiresAnyPermission] = [ op.requiredRoles || [], op.requiredPermissions || [], op.requiresAnyRole || [], op.requiresAnyPermission || []] let missingRoles = requiredRoles.filter(x => roles.indexOf(x) < 0) if (missingRoles.length > 0) @@ -87,5 +99,5 @@ export function useAuth() { /** Check if the current user is Authenticated in a reactive Ref */ const isAuthenticated = computed(() => Sole.user.value != null) - return { signIn, signOut, user, isAuthenticated, hasRole, hasPermission, isAdmin, canAccess, invalidAccessMessage } + return { signIn, signOut, user, toAuth, isAuthenticated, hasRole, hasPermission, isAdmin, canAccess, invalidAccessMessage } } diff --git a/src/use/metadata.ts b/src/use/metadata.ts index 73724a5..ebd4929 100644 --- a/src/use/metadata.ts +++ b/src/use/metadata.ts @@ -302,9 +302,10 @@ export function isValid(metadata:AppMetadata|null|undefined) { } /** Get get AppMetadata instance */ -export function getMetadata(opt?:{assert?:boolean}):any { // use 'any' to avoid type explosion - if (opt?.assert && !Sole.metadata.value) +export function getMetadata(opt?:{assert?:boolean}):any { // use 'any' to avoid type explosion + if (!tryLoad() && opt?.assert && !Sole.metadata.value) throw new Error('useMetadata() not configured, see: https://docs.servicestack.net/vue/use-metadata') + return Sole.metadata.value }