https://supertokens.com/ logo
Supertokens with graphql
a

aV

04/21/2023, 5:11 AM
hello, i am using gofiber + ent + graphql + supertokens. I am not able to check whether user is authorized or not. here is my gofiber handler:
go
// RegisterRoutes registers the routes
func (r *Router) RegisterRoutes() {
    r.App.All("/query", gqlQueryHandler(r.DB.Ent))
    r.App.Get("/playground", gqlPlaygroundHandler("Playground", "/query"))
}

// gqlQueryHandler handles graphql queries
func gqlQueryHandler(ent *ent.Client) fiber.Handler {
    return func(ctx *fiber.Ctx) error {
        h := handler.NewDefaultServer(graph.NewSchema(ctx, ent))
        adaptor.HTTPHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            h.ServeHTTP(w, r)
        })(ctx)
        return nil
    }
}

// gqlPlaygroundHandler handles graphql playground
func gqlPlaygroundHandler(title string, endpoint string) fiber.Handler {
    return adaptor.HTTPHandlerFunc(playground.Handler(title, endpoint))
}
here is my createAccount mutation resolver:
go
// CreateAccount is the resolver for the createAccount field.
func (r *mutationResolver) CreateAccount(ctx context.Context, input ent.CreateAccountInput) (*ent.Account, error) {
    // Only authenticated user's can send request
    if !supertokens.IsAuthed(ctx) {
        return nil, gqlerror.Errorf("Unauthorized")
    }

    // TODO: validate params

    // Create account
    acc, err := r.ent.Account.Create().SetInput(input).Save(ctx)
    if err != nil {
        log.Error().Err(err).Msg("Error creating account")
        if ent.IsConstraintError(err) {
            return nil, gqlerror.Errorf("Account exists")
        }
        return nil, err
    }

    log.Debug().Msg("Account created")
    return acc, nil
}
and this is my IsAuthed function
go
// IsAuthed returns whether the request is being made by an authenticated user
func IsAuthed(ctx context.Context) bool {
    return session.GetSessionFromRequestContext(ctx) != nil
}
i am testing the createAccount mutation from insomnia. After logging in to supertokens, whenever I try to use createAccount mutatuon I get Unauthorized error
In IsAuthed function I also tried to pass fiber context but still the same
r

rp

04/21/2023, 5:27 AM
are you calling verifySession / getSession anywhere at all? If you aren't please do so - see our docs. If you are, please enable backend debug logs and show the output.
a

aV

04/21/2023, 5:49 AM
r.App.All("/query", supertokens.VerifySession(nil), gqlQueryHandler(r.DB.Ent))
r

rp

04/21/2023, 5:50 AM
can you enable backend debug logs and show the output?
r

rp

04/21/2023, 5:52 AM
not the core logs
the backend debug logs
a

aV

04/21/2023, 5:55 AM
oops
wait
r

rp

04/21/2023, 6:12 AM
session verification did succeed!
perhaps the way you have integrated it is wrong. See our example app: https://github.com/supertokens/supertokens-golang/blob/master/examples/with-fiber/main.go