W
Wasp-lang•6mo ago
abehod

How to overwrite system api endpoint?

Hello, I wanna use create custom auth/me endpoint handler, which would handle this request somehow differently, I have added this api to my main.wasp file, but when I make request to auth/me old handler is used. Is there a way to kind of overwrite system api endpoints? My api in main.wasp:
api authMe {
fn: import { authMe } from "@src/server/auth/authMe.js",
entities: [User],
httpRoute: (GET, "/auth/me")
}
api authMe {
fn: import { authMe } from "@src/server/auth/authMe.js",
entities: [User],
httpRoute: (GET, "/auth/me")
}
My example authMe file:
import { serialize as superjsonSerialize } from 'superjson'
import { handleRejection } from 'wasp/server/utils'
import { throwInvalidCredentialsError } from 'wasp/auth/utils'

export const authMe = handleRejection(async (req, res) => {
console.log("Custom authMe route");
if (req.user) {
return res.json(superjsonSerialize(req.user))
} else {
throwInvalidCredentialsError()
}
})
import { serialize as superjsonSerialize } from 'superjson'
import { handleRejection } from 'wasp/server/utils'
import { throwInvalidCredentialsError } from 'wasp/auth/utils'

export const authMe = handleRejection(async (req, res) => {
console.log("Custom authMe route");
if (req.user) {
return res.json(superjsonSerialize(req.user))
} else {
throwInvalidCredentialsError()
}
})
6 Replies
MEE6
MEE6•6mo ago
Wohooo @abehod, you just became a Waspeteer level 1!
kapa.ai
kapa.ai•6mo ago
Hi @abehod. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry, but based on the provided knowledge sources, there is no information available on how to overwrite system API endpoints in Wasp. The sources do provide information on how to define custom API endpoints and how to use middleware to customize the behavior of certain paths, but there is no explicit mention of overwriting system API endpoints. For custom API endpoints, you can define them in your main.wasp file using the api declaration. This includes specifying the function to handle the request, the HTTP route, and any entities that the API uses. Here is an example from the API Reference (https://wasp-lang.dev/docs/advanced/apis#api-reference):
api fooBar {
fn: import { fooBar } from "@src/apis",
httpRoute: (GET, "/foo/bar"),
entities: [Task],
auth: true,
middlewareConfigFn: import { apiMiddleware } from "@src/apis"
}
api fooBar {
fn: import { fooBar } from "@src/apis",
httpRoute: (GET, "/foo/bar"),
entities: [Task],
auth: true,
middlewareConfigFn: import { apiMiddleware } from "@src/apis"
}
For customizing behavior of certain paths, you can use middleware. This is done by defining a middlewareConfigFn on an apiNamespace. Here is an example from the Middleware Config section (https://wasp-lang.dev/docs/advanced/middleware-config#3-customize-per-path-middleware):
apiNamespace fooBar {
middlewareConfigFn: import { fooBarNamespaceMiddlewareFn } from "@src/apis",
path: "/foo/bar"
}
apiNamespace fooBar {
middlewareConfigFn: import { fooBarNamespaceMiddlewareFn } from "@src/apis",
path: "/foo/bar"
}
However, these examples do not cover overwriting system API endpoints. I would recommend checking the official Wasp documentation or reaching out to the Wasp community for more specific guidance on this topic. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @abehod, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho•6mo ago
Overwriting built in endpoint is not supported at the moment! We haven't really worked on that angle to be honest, we are trying to provide escape hatches to tweak the auth in different ways. Could you maybe explain a bit what are you trying to achieve and maybe I can suggest a different option for you?
abehod
abehodOP•6mo ago
thank you for your answer! I am trying to build 2 separated logins in my app: 1) For businesses 2) For their employees (something like app.com/business-slug/login) So my idea to implement this was to create 2 separate entites: one for business and one for employee. So the employee account flow would be: 1) Business creates an account for an employee -> employee instance is created in the Employee table with foreign key to Business as employer id, and new AuthIdentity created for business which represents this employee 2) When an employee logs in to the system, it basically passes the same flow as during the regular log in: checks AuthIdentity, creates Session and returns it to FE, where for now employee becomes logged in as a business and I would like to change it and make an employee logged an as an user with less rights. Let me know if you have any ideas how to achieve it
miho
miho•6mo ago
Maybe if you designed it around having a single User but with different roles e.g. role=BusinessManager or role=Employee and then build a different post-signup flow in some way (here's an example in the docs https://wasp-lang.dev/docs/auth/social-auth/overview#example-allowing-user-to-set-their-username) The benefit is that you keep the same exact auth system in Wasp but build on top of it without needing to modify it completely. You can then separate based on the role what is allowed for each User. I hope this makes sense, if not, let me know and let's brainstorm some more 😄
Overview | Wasp
Social login options (e.g., Log in with Google) are a great (maybe even the best) solution for handling user accounts.
abehod
abehodOP•6mo ago
That's much better approach, thank you very much for your time!
Want results from more Discord servers?
Add your server