mirror of
https://github.com/HangarMC/Hangar.git
synced 2024-12-15 06:41:46 +08:00
733c151f5f
Signed-off-by: MiniDigger <admin@benndorf.dev>
43 lines
1.7 KiB
TypeScript
43 lines
1.7 KiB
TypeScript
import { Context } from '@nuxt/types';
|
|
import { UserPermissions } from 'hangar-api';
|
|
import { AuthState } from '~/store/auth';
|
|
|
|
export default async ({ store, params, $api, $auth, $cookies }: Context) => {
|
|
if ($cookies.get('HangarAuth_REFRESH', { parseJSON: false })) {
|
|
await $auth.refreshUser();
|
|
}
|
|
if (params.author && params.slug) {
|
|
if ($auth.isLoggedIn()) {
|
|
return $api
|
|
.request<UserPermissions>('permissions', true, 'get', {
|
|
author: params.author,
|
|
slug: params.slug,
|
|
})
|
|
.then((userPermissions) => {
|
|
store.commit('auth/SET_ROUTE_PERMISSIONS', userPermissions.permissionBinString);
|
|
})
|
|
.catch(() => {
|
|
store.commit('auth/SET_ROUTE_PERMISSIONS', null);
|
|
});
|
|
}
|
|
} else if (params.user) {
|
|
if ($auth.isLoggedIn()) {
|
|
return $api
|
|
.request<UserPermissions>('permissions', true, 'get', {
|
|
organization: params.user,
|
|
})
|
|
.then((userPermissions) => {
|
|
store.commit('auth/SET_ROUTE_PERMISSIONS', userPermissions.permissionBinString);
|
|
})
|
|
.catch(() => {
|
|
store.commit('auth/SET_ROUTE_PERMISSIONS', null);
|
|
});
|
|
}
|
|
} else if ($auth.isLoggedIn()) {
|
|
// Catch-all (just use global permissions)
|
|
store.commit('auth/SET_ROUTE_PERMISSIONS', (store.state.auth as AuthState).user!.headerData.globalPermission);
|
|
return;
|
|
}
|
|
store.commit('auth/SET_ROUTE_PERMISSIONS', null);
|
|
};
|