hey, so i'm using `@ory/kratos-client` v1.2.0 bas...
# ory-selfhosting
h
hey, so i'm using
@ory/kratos-client
v1.2.0 based on the
react-spa
TS code. when I do the
.toSession()
check where it requests
/sessions/whoami
I have 2 requests in my developer console saying
401 (Unauthorized)
and the network tab gives me 401 XHR type. then looking at kratos logs I see this:
Copy code
{
    "audience": "application",
    "error": {
        "debug": "",
        "message": "The request could not be authorized",
        "reason": "No valid session credentials found in the request.",
        "stack_trace": "\<http://ngithub.com/ory/kratos/session.(*ManagerHTTP).FetchFromRequest|ngithub.com/ory/kratos/session.(*ManagerHTTP).FetchFromRequest>\n\t/project/session/manager_http.go:236\ngithub.com/ory/kratos/session.(*Handler).whoami\n\t/project/session/handler.go:215\ngithub.com/ory/kratos/x.(*RouterPublic).Handle.NoCacheHandle.func1\n\t/project/x/nocache.go:21\ngithub.com/julienschmidt/httprouter.(*Router).ServeHTTP\n\t/go/pkg/mod/github.com/julienschmidt/httprouter@v1.3.0/router.go:387\ngithub.com/ory/nosurf.(*CSRFHandler).handleSuccess\n\t/go/pkg/mod/github.com/ory/nosurf@v1.2.7/handler.go:234\ngithub.com/ory/nosurf.(*CSRFHandler).ServeHTTP\n\t/go/pkg/mod/github.com/ory/nosurf@v1.2.7/handler.go:185\ngithub.com/ory/kratos/cmd/daemon.servePublic.MaxBytesHandler.func4\n\t/usr/local/go/src/net/http/server.go:3841\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2166\ngithub.com/urfave/negroni.(*Negroni).UseHandler.Wrap.func1\n\t/go/pkg/mod/github.com/urfave/negroni@v1.0.0/negroni.go:46\ngithub.com/urfave/negroni.HandlerFunc.ServeHTTP\n\t/go/pkg/mod/github.com/urfave/negroni@v1.0.0/negroni.go:29\ngithub.com/urfave/negroni.middleware.ServeHTTP\n\t/go/pkg/mod/github.com/urfave/negroni@v1.0.0/negroni.go:38\ngithub.com/ory/kratos/x.init.func1\n\t/project/x/clean_url.go:15\ngithub.com/urfave/negroni.HandlerFunc.ServeHTTP\n\t/go/pkg/mod/github.com/urfave/negroni@v1.0.0/negroni.go:29\ngithub.com/urfave/negroni.middleware.ServeHTTP\n\t/go/pkg/mod/github.com/urfave/negroni@v1.0.0/negroni.go:38\ngithub.com/rs/cors.(*Cors).ServeHTTP\n\t/go/pkg/mod/github.com/rs/cors@v1.8.2/cors.go:266\ngithub.com/ory/kratos/cmd/daemon.servePublic.func1\n\t/project/cmd/daemon/serve.go:114\ngithub.com/urfave/negroni.HandlerFunc.ServeHTTP\n\t/go/pkg/mod/github.com/urfave/negroni@v1.0.0/negroni.go:29\ngithub.com/urfave/negroni.middleware.ServeHTTP\n\t/go/pkg/mod/github.com/urfave/negroni@v1.0.0/negroni.go:38\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2166\ngithub.com/prometheus/client_golang/prometheus/promhttp.InstrumentHandlerResponseSize.func1\n\t/go/pkg/mod/github.com/prometheus/client_golang@v1.13.0/prometheus/promhttp/instrument_server.go:284\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2166\ngithub.com/prometheus/client_golang/prometheus/promhttp.InstrumentHandlerCounter.func1\n\t/go/pkg/mod/github.com/prometheus/client_golang@v1.13.0/prometheus/promhttp/instrument_server.go:142\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2166\ngithub.com/prometheus/client_golang/prometheus/promhttp.InstrumentHandlerDuration.func1\n\t/go/pkg/mod/github.com/prometheus/client_golang@v1.13.0/prometheus/promhttp/instrument_server.go:92\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2166\ngithub.com/prometheus/client_golang/prometheus/promhttp.InstrumentHandlerDuration.func2\n\t/go/pkg/mod/github.com/prometheus/client_golang@v1.13.0/prometheus/promhttp/instrument_server.go:104\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2166\ngithub.com/prometheus/client_golang/prometheus/promhttp.InstrumentHandlerRequestSize.func1\n\t/go/pkg/mod/github.com/prometheus/client_golang@v1.13.0/prometheus/promhttp/instrument_server.go:234\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2166\ngithub.com/ory/x/prometheusx.Metrics.Instrument.Metrics.instrumentHandlerStatusBucket.func1\n\t/go/pkg/mod/github.com/ory/x@v0.0.623/prometheusx/metrics.go:115\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2166\ngithub.com/ory/x/prometheusx.(*MetricsManager).ServeHTTP\n\t/go/pkg/mod/github.com/ory/x@v0.0.623/prometheusx/middleware.go:41",
        "status": "Unauthorized",
        "status_code": 401
    },
    "http_request": {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-encoding": "gzip, deflate, br, zstd",
            "accept-language": "en-US,en;q=0.5",
            "connection": "close",
            "origin": "<https://kratos.foo.bar>",
            "referer": "<https://kratos.foo.bar/>",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-site",
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:129.0) Gecko/20100101 Firefox/129.0",
            "x-forwarded-for": "192.168.228.1",
            "x-real-ip": "192.168.228.1"
        },
        "host": "auth.foo.bar",
        "method": "GET",
        "path": "/sessions/whoami",
        "query": null,
        "remote": "192.168.228.9:36054",
        "scheme": "https"
    },
    "http_response": {
        "status_code": 401
    },
    "level": "info",
    "msg": "An error occurred while handling a request",
    "service_name": "Ory Kratos",
    "service_version": "v1.2.0",
    "time": "2024-08-19T13:53:13.362036096Z"
}
Is something wrong in my kratos settings/config file? 🤔
t
Your request doesn't contain cookies. You should create an Axios instance with option: {withCredentials: true}. then you browser cookies can be included in request header.
h
🤔
that does make sense
i just look @tall-actor-27021 my
sdk.tsx
does have the baseOptions.
Copy code
export const sdk = new FrontendApi(
  new Configuration({
    //<https://vitejs.dev/guide/env-and-mode.html#env-files>
    basePath: import.meta.env.VITE_ORY_SDK_URL,
    // we always want to include the cookies in each request
    // cookies are used for sessions and CSRF protection
    baseOptions: {
      withCredentials: true,
    },
  }),
)
basically copy pasta of the example
i do see the cookie in the request
t
then that's probably Same-Site config
Cookie can be sent with the same domain and subdomain.
h
ya, i'm aware.. i will look at that config
oka, so my kratos config has:
Copy code
session:
  cookie:
    domain: <my top level>
    same_site: None
And I managed to set:
{cookie: req.header('cookie')}
on my
.toSession()
request and I see the
cookie
header in my kratos logs. yet same problem 🤔
h
didn't look like i needed to do those "random" stuff i did @tall-actor-27021 i just got login etc to work. I think I need to think of the business logic compared to what the ory example are doing things.