Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api): Session middleware improvements #331

Merged
merged 1 commit into from
May 12, 2022

Conversation

koistya
Copy link
Member

@koistya koistya commented May 12, 2022

  • Update api/auth/session.ts (session middleware) to allow authenticating using Google's ID token
  • Move JWT related utility functions to api/core/jwt.ts
  • Initialize Google/Facebook OAuth 2.0 clients on demand (memoize)
  • Add a unit test showing how to test protected routes / API endpoints
import request from "supertest";
import { createIdToken, db } from "../core";
import { api } from "../index";

test(`fetch me (current user)`, async () => {
  const res = await request(api)
    .post("/api")
    .auth(createIdToken({ id: "test1" }), { type: "bearer" })
    .send({
      query: `#graphql
        query {
          me { id email name }
        }
      `,
    })
    .expect(200)
    .expect("Content-Type", "application/json");

  expect(res.body).toMatchInlineSnapshot(`
    Object {
      "data": Object {
        "me": Object {
          "id": "VXNlcjp0ZXN0MQ==",
          "email": "jaylon.johns@example.com",
          "name": "Jaylon Johns",
        },
      },
    }
  `);
});

afterAll(() => db.destroy());

See api/queries/me.test.ts, db/seeds/users.json.

@koistya koistya self-assigned this May 12, 2022
@koistya koistya merged commit 962c54b into kriasoft:main May 12, 2022
@koistya koistya deleted the api-tests-auth branch May 12, 2022 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant