Skip to content

Commit

Permalink
Cleanup and roles fix
Browse files Browse the repository at this point in the history
  • Loading branch information
datajohnson committed Dec 2, 2024
1 parent d9724da commit 48e659f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion api/src/middleware/authz.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export async function loadUser(req: Request, res: Response, next: NextFunction)
};

await db.create(newUser);
req.user = { ...req.user, ...newUser };
req.user = { ...req.user, ...newUser, roles: [] };
}
} else {
console.log("Payload from Auth0 is strange or failed for", req.auth);
Expand Down
15 changes: 15 additions & 0 deletions api/src/services/user-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { User } from "../data/models";
import { db } from "../data";
import { isArray } from "lodash";

export class UserService {
async getAll(): Promise<User[]> {
Expand All @@ -8,19 +9,33 @@ export class UserService {

async getBySub(auth_subject: string): Promise<User | undefined> {
let user = await db<User>("users").where({ auth_subject }).first();

if (user && user.roles) {
if (!isArray(user.roles)) user.roles = user.roles.split(",");
}

return user;
}

async getById(id: number | string): Promise<User | undefined> {
let user = await db<User>("users")
.where({ id: parseInt(`${id}`) })
.first();

if (user && user.roles) {
if (!isArray(user.roles)) user.roles = user.roles.split(",");
}
return user;
}

async getByEmail(email: string): Promise<User | undefined> {
if (email) {
let user = await db<User>("users").where({ email }).first();

if (user && user.roles) {
if (!isArray(user.roles)) user.roles = user.roles.split(",");
}

return user;
}

Expand Down
10 changes: 1 addition & 9 deletions web/src/layouts/DefaultNoAuth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,14 @@
</template>

<script setup>
import { onMounted, ref } from "vue";
import { storeToRefs } from "pinia";
import { useDisplay } from "vuetify";
import { useAuth0 } from "@auth0/auth0-vue";
import { applicationName } from "@/config";
import { useInterfaceStore } from "@/store/InterfaceStore";
import { useAuth0 } from "@auth0/auth0-vue";
import useCurrentUser from "@/use/use-current-user";
const { loginWithRedirect, logout } = useAuth0();
const interfaceStore = useInterfaceStore();
const { isOffline } = storeToRefs(interfaceStore);
const { currentUser, isSystemAdmin } = useCurrentUser();
const { smAndUp } = useDisplay();
async function logoutClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export interface User {
is_active: boolean | string;
iss: string;

roles: string | string[];
roles: string[];
}

if (import.meta.hot) {
Expand Down
8 changes: 2 additions & 6 deletions web/src/use/use-current-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,8 @@ export function useCurrentUser<IsLoaded extends boolean = false>() {
function hasRole(roleName: string): boolean {
if (!state.currentUser) return false;
if (state.currentUser.roles) {
const roles = isArray(state.currentUser.roles || "")
? [...state.currentUser.roles]
: `${state.currentUser.roles}`.split(",");

const role = roles.find((r) => r == roleName);
if (role) return true;
const roles = state.currentUser.roles;
return roles.includes(roleName);
}
return false;
}
Expand Down

0 comments on commit 48e659f

Please sign in to comment.