Rubyae
Rubyae
HHono
Created by Rubyae on 5/18/2024 in #help
Combine JWT middleware with other middleware
Update, I did find a solution that does work, not exactly what I was looking for though, what I did now is the following:
const requireRoles = (roles: Array<UserRole> = []) => createMiddleware(async(c, next) => {
const jwtPayload = c.get('jwtPayload');

if(roles.length) {
const query = await db.select().from(users).where(eq(users.id, jwtPayload.id));
if(roles.length > 0 && !roles.includes(query[0].role as UserRole)) {
console.log('lolzz')
return c.text('Unauthorized', 403);
}
}
await next();
})

export const authorize = (roles: Array<UserRole> = []) => [jwt, requireRoles(roles)];
const requireRoles = (roles: Array<UserRole> = []) => createMiddleware(async(c, next) => {
const jwtPayload = c.get('jwtPayload');

if(roles.length) {
const query = await db.select().from(users).where(eq(users.id, jwtPayload.id));
if(roles.length > 0 && !roles.includes(query[0].role as UserRole)) {
console.log('lolzz')
return c.text('Unauthorized', 403);
}
}
await next();
})

export const authorize = (roles: Array<UserRole> = []) => [jwt, requireRoles(roles)];
and then I can use the following for my route:
app.post('/', ...authorize(), async (c) => {});
app.post('/', ...authorize([UserRole.Moderator, UserRole.Manager]), async (c) => {});
app.post('/', ...authorize(), async (c) => {});
app.post('/', ...authorize([UserRole.Moderator, UserRole.Manager]), async (c) => {});
7 replies
HHono
Created by Rubyae on 5/18/2024 in #help
Combine JWT middleware with other middleware
No worries, I've adjusted your code so that it works but then I'm getting handler is not a function, I'm really not sure why things aren't working
7 replies
HHono
Created by Rubyae on 4/14/2024 in #help
Hono Oauth Provider with JWT issue
Yeah I'm not sure, I initially resolved it like this ^ but now I noticed that if I move the import { sign } from "hono/jwt"; above the import { googleAuth } from '@hono/oauth-providers/google' it also seems to be fine
9 replies
HHono
Created by Rubyae on 4/14/2024 in #help
Hono Oauth Provider with JWT issue
I did some extra testing and I assume this might be some strange override thingy going on? As I just checked, in the file where I have my Google endpoint for hono oauth providers I also have the sign from hono/jwt in there, if I move the line from hono/jwt above the one from google oauth providers, my vscode is not upset anymore :PikaThink:
9 replies
HHono
Created by Rubyae on 4/14/2024 in #help
Hono Oauth Provider with JWT issue
I guess the thing that confuses me is that in hono/dist/types/middleware/jwt/index.d.ts is this:
declare module '../../context' {
interface ContextVariableMap {
jwtPayload: any;
}
}
declare module '../../context' {
interface ContextVariableMap {
jwtPayload: any;
}
}
so this is defining what you say and then the @hono/auth-providers has this in its @hono/oauth-providers/dist/providers/google/index.d.ts
declare module 'hono' {
interface ContextVariableMap extends OAuthVariables {
'user-google': Partial<GoogleUser> | undefined;
}
}
declare module 'hono' {
interface ContextVariableMap extends OAuthVariables {
'user-google': Partial<GoogleUser> | undefined;
}
}
so it feels like this should just work out of the box without me adding anything extra
9 replies
HHono
Created by Rubyae on 4/14/2024 in #help
Hono Oauth Provider with JWT issue
No worries, oh alright, I see thanks for this information/clarification ^^
9 replies
HHono
Created by Rubyae on 4/14/2024 in #help
Hono Oauth Provider with JWT issue
Update: I did found out that I can resolve it by adding the Variables type https://hono.dev/api/context#set-get
type Variables = {
message: string
}

const app = new Hono<{ Variables: Variables }>()
type Variables = {
message: string
}

const app = new Hono<{ Variables: Variables }>()
yet I'm not sure if this is the way it's supposed to work, it seems kinda odd to me because both already have these things defined, it's just that's it's not properly being picked up I guess
9 replies