Architecture · Security
Role-Based Access: Middleware Redirects, the Backend Decides
A Nuxt route middleware sends the user to the right page, but it does not secure anything. Here is how I built role checks in two layers on an education platform, and why I wrote the same rule twice.
01. Frontend checks are UX, not security
A role check in the frontend protects nothing on its own: everything in the browser is in the user's hands. You do not even need to open the page to bypass a middleware, you can call the API directly. The job of a frontend guard is to send an unauthorized user to their own area instead of showing a blank screen or a 403.
So I built two layers: redirects on the Nuxt side, real authorization on the Go API side. The same role rule lives in two places, on purpose.
02. Layer 1: a global route middleware
The role enum is a numeric hierarchy: Student = 2, Parent = 3, Educator = 6, SubBranchManager = 10, Rep = 15, BranchManager = 20, OrgManager = 30. A global middleware (acl.global.ts) reads the session and role on every navigation and sends the user to their own area. A parent, for example, can only reach a few paths and everything else falls back to /veli.
export default defineNuxtRouteMiddleware((to) => {
if (to.meta.public) return
const auth = useAuthStore()
if (!auth.isAuthenticated) return navigateTo('/login')
// A parent can only enter their own area
if (auth.user?.role === UserRole.Parent) {
const allowed = ['/veli', '/magaza-v2', '/yardim']
if (!allowed.some((p) => to.path.startsWith(p))) {
return navigateTo('/veli')
}
}
})
Note that this code only redirects. Keeping a parent away from the wrong page is part of the user experience; not leaking the wrong data to that parent is a different layer's job.
03. Layer 2: the backend makes the real decision
On the Go side the role check is a Fiber middleware: RequireRole(enum.UserRoleOrgManager) lets in that role and above. The numeric hierarchy pays off here: no need to enumerate a role list for every endpoint.
On the admin side the rules live in the database, not in code: a matrix of which page keys each role can reach. The service loads it into memory on first read behind a mutex and clears the cache on writes. SuperAdmin always passes.
func (s *AdminPermissionService) IsAllowed(
ctx context.Context, role enum.AdminRole, pageKey string,
) (bool, error) {
if role == enum.AdminRoleSuperAdmin {
return true, nil
}
if err := s.loadCache(ctx); err != nil {
return false, err
}
s.mu.RLock()
defer s.mu.RUnlock()
return s.cache[role][pageKey], nil
}
There is also a module-level check: IsGroupAllowed opens a group's endpoints (store, accounting, CRM) when the role has at least one page of that group enabled. Enabling a page in the panel therefore has a backend counterpart, the two are not updated separately.
04. Why write the same rule twice?
Because the two layers fail differently. A wrong frontend rule sends the user to the wrong page: bad but harmless. A wrong backend rule leaks data. Generating both from one source is tempting, but what the frontend needs to know (where do I redirect?) and what the backend needs to know (may this request go through?) are not the same question.
Rule of thumb: for every new page, write the backend endpoint's authorization first, then add the frontend redirect. Do it the other way round and you ship a screen that looks like it works but is unprotected.