mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: extend useAuth hook with role and permissions from session
This commit is contained in:
@@ -6,14 +6,18 @@ interface AuthState {
|
|||||||
authEnabled: boolean;
|
authEnabled: boolean;
|
||||||
isAuthenticated: boolean;
|
isAuthenticated: boolean;
|
||||||
mustChangePassword: boolean;
|
mustChangePassword: boolean;
|
||||||
|
role: string | null;
|
||||||
|
permissions: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useAuth(): AuthState {
|
export function useAuth() {
|
||||||
const [state, setState] = useState<AuthState>({
|
const [state, setState] = useState<AuthState>({
|
||||||
loading: true,
|
loading: true,
|
||||||
authEnabled: false,
|
authEnabled: false,
|
||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
mustChangePassword: false,
|
mustChangePassword: false,
|
||||||
|
role: null,
|
||||||
|
permissions: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -29,6 +33,21 @@ export function useAuth(): AuthState {
|
|||||||
authEnabled: false,
|
authEnabled: false,
|
||||||
isAuthenticated: true,
|
isAuthenticated: true,
|
||||||
mustChangePassword: false,
|
mustChangePassword: false,
|
||||||
|
role: "admin",
|
||||||
|
permissions: [
|
||||||
|
"tools:use",
|
||||||
|
"files:own",
|
||||||
|
"files:all",
|
||||||
|
"apikeys:own",
|
||||||
|
"apikeys:all",
|
||||||
|
"pipelines:own",
|
||||||
|
"pipelines:all",
|
||||||
|
"settings:read",
|
||||||
|
"settings:write",
|
||||||
|
"users:manage",
|
||||||
|
"teams:manage",
|
||||||
|
"branding:manage",
|
||||||
|
],
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -41,6 +60,8 @@ export function useAuth(): AuthState {
|
|||||||
authEnabled: true,
|
authEnabled: true,
|
||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
mustChangePassword: false,
|
mustChangePassword: false,
|
||||||
|
role: null,
|
||||||
|
permissions: [],
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -57,6 +78,8 @@ export function useAuth(): AuthState {
|
|||||||
authEnabled: true,
|
authEnabled: true,
|
||||||
isAuthenticated: true,
|
isAuthenticated: true,
|
||||||
mustChangePassword: mustChange,
|
mustChangePassword: mustChange,
|
||||||
|
role: session.user?.role ?? null,
|
||||||
|
permissions: session.user?.permissions ?? [],
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
localStorage.removeItem("stirling-token");
|
localStorage.removeItem("stirling-token");
|
||||||
@@ -65,6 +88,8 @@ export function useAuth(): AuthState {
|
|||||||
authEnabled: true,
|
authEnabled: true,
|
||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
mustChangePassword: false,
|
mustChangePassword: false,
|
||||||
|
role: null,
|
||||||
|
permissions: [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
@@ -74,6 +99,21 @@ export function useAuth(): AuthState {
|
|||||||
authEnabled: false,
|
authEnabled: false,
|
||||||
isAuthenticated: true,
|
isAuthenticated: true,
|
||||||
mustChangePassword: false,
|
mustChangePassword: false,
|
||||||
|
role: "admin",
|
||||||
|
permissions: [
|
||||||
|
"tools:use",
|
||||||
|
"files:own",
|
||||||
|
"files:all",
|
||||||
|
"apikeys:own",
|
||||||
|
"apikeys:all",
|
||||||
|
"pipelines:own",
|
||||||
|
"pipelines:all",
|
||||||
|
"settings:read",
|
||||||
|
"settings:write",
|
||||||
|
"users:manage",
|
||||||
|
"teams:manage",
|
||||||
|
"branding:manage",
|
||||||
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,5 +121,7 @@ export function useAuth(): AuthState {
|
|||||||
checkAuth();
|
checkAuth();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return state;
|
const hasPermission = (permission: string) => state.permissions.includes(permission);
|
||||||
|
|
||||||
|
return { ...state, hasPermission };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user