File size: 1,546 Bytes
cd6f98e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type { Session } from "next-auth";
import { z } from "zod";

import { env } from "../../env/client.mjs";
import { get } from "../fetch-utils";

export default class OauthApi {
  readonly accessToken?: string;
  readonly organizationId?: string;

  constructor(accessToken?: string, organizationId?: string) {
    this.accessToken = accessToken;
    this.organizationId = organizationId;
  }

  static fromSession(session: Session | null) {
    return new OauthApi(session?.accessToken, session?.user?.organizations[0]?.id);
  }

  async install(provider: string, redirectUri?: string) {
    const url = `${env.NEXT_PUBLIC_VERCEL_URL}${redirectUri || ""}`;

    return await get(
      `/api/auth/${provider}?redirect=${encodeURIComponent(url)}`,
      z.string().url(),
      this.accessToken,
      this.organizationId
    );
  }

  async uninstall(provider: string) {
    return await get(
      `/api/auth/${provider}/uninstall`,
      z.object({
        success: z.boolean(),
      }),
      this.accessToken,
      this.organizationId
    );
  }
  // TODO: decouple this
  async get_info(provider: string) {
    return await get(
      `/api/auth/${provider}/info`,
      z
        .object({
          name: z.string(),
          id: z.string(),
        })
        .array(),
      this.accessToken,
      this.organizationId
    );
  }

  async get_info_sid() {
    return await get(
      `/api/auth/sid/info`,
      z.object({
        connected: z.boolean(),
      }),
      this.accessToken,
      this.organizationId
    );
  }
}