I setup a supertokens node in my application A. Bu...
# support-questions
s
I setup a supertokens node in my application A. But I wanted to verify session in another application that is an http proxy for combining microservices for local development purposes you can consider like an api gateway. I am trying to verify session just before proxying to other microservices so I can add custom header that contains user information. But when I try to connect supertokens node on that proxy application, my main application A's overrided methods are not working also it does not send emails via application A. What kind of approach should I apply on that scenario?
r
hey @syntaxerror do you have any sort of path that is stripped away by the proxy server before forwarding the request to the actual server?
s
no ``` supertokens.init({ framework: "express", supertokens: { connectionURI: "http://localhost:3567", }, appInfo: { appName: "myapp", apiDomain: "http://localhost:8000", websiteDomain: "http://localhost:8080", apiBasePath: "/auth", websiteBasePath: "/" }, recipeList: [ Passwordless.init({ flowType: "USER_INPUT_CODE", contactMethod: "EMAIL_OR_PHONE" }), Session.init() ] }); app.use(cors( { credentials: true, origin: "http://localhost:8080", allowedHeaders: ["content-type", ...supertokens.getAllCORSHeaders()], } )) app.use(ex.middleware()); app.use(ex.errorHandler()) app.use(expressContext()) const addHeaders = async (proxyReq, req, res) => { if (req.context.userId === undefined) { console.log("session undefined") }else{ console.log("session defined") proxyReq.setHeader("X-User-ID", req.context.userId); } } app.use(async (req, res, next) => { try { let session = await Session.getSession(req, res); if (session === undefined) { throw Error("Should never come here") } let userId = session.getUserId(); req.context.userId = userId console.log("userId", req.context.userId) next(); } catch (err) { next(err); } }) ``
that is the setup what i only added to that express proxy
r
can you enable backend debug logs and show the output when you call one of the APIs which you have added override on?
s
Actually, I inited another application too.
Core is working on docker side
But I wanted connect the my main application A via that second application like a client so I can verify the session.
r
i don't think i am understanding the setup
s
May be my approach is wrong, let me show what I'm trying: APP-A(has overrided methods) -> SuperTokens Core(runs under docker) APP-PROXY-> trying to verify session and adding header to just before proxying to microservices. But I want to connect via APP-A. In summary User -> app-proxy -> App-A -> Supertokens-core
r
right. So how are you verifying the session in APP-PROXY? Are you using getSession / verifySession function?
s
app.use(async (req, res, next) => { try { let session = await Session.getSession(req, res); if (session === undefined) { throw Error("Should never come here") } let userId = session.getUserId(); req.context.userId = userId console.log("userId", req.context.userId) next(); } catch (err) { next(err); } })
It works okay, but It seems that express proxy application creates its own instance
r
"express proxy application creates its own instance" -> instance of?
s
So that It does not connect to App-A I have already created logics on it.
r
Ah i see.
Yea well.. it won't connecto to App-A
what overrides are you doing in App-A?
i mean which functions / APIs
s
post-signin post-signup consumecode override and I have own businessrules while consuming code etc.
I may say lots of customization on top of it.
What I looking for I should connect my customized app-a and that proxy application should only act like a client to verify user credentials.
I can't commonize that business rule for that proxy app, because its just for development purposes and I can't connect any db on that instance. Also languages are different, golang and node.js
So I stucked there.
r
right. So when calling APIs related to supertokens (sign in etc..) don't call the getSession in proxy-app and just pass the request to app-A. For other requests, call the getSession and pass it to wherever your application's APIs are defined.
s
yeah it is quite reasonable. I will try and give you the feedback.
@rp It seems working now, I just register the session middlewares right after the my main application proxy configs.
But I have an additional question, Can I follow similar method on AWS apigateway too?
r
We have guides for AWS integration - should check those out
s
I think I can bind my custom authorizer on necessary paths right
r
yea. custom authorizer works - we also have code snippet for that
s
thanks 👍
Thank you for your great support again.