From 11e1312e3a6c831b35745b2e8aef657048fd470a Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Date: Fri, 18 Mar 2022 14:42:42 +0100 Subject: [PATCH] feat: inital commit --- .gitignore | 4 + README.md | 1 + next-env.d.ts | 5 + package.json | 27 ++++++ src/pages/api/auth/[...nextauth].ts | 33 +++++++ src/pages/api/jwt.ts | 12 +++ src/pages/api/protected.ts | 18 ++++ src/pages/api/session.ts | 8 ++ tsconfig.json | 23 +++++ yarn.lock | 137 ++++++++++++++++++++++++++++ 10 files changed, 268 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 next-env.d.ts create mode 100644 package.json create mode 100644 src/pages/api/auth/[...nextauth].ts create mode 100644 src/pages/api/jwt.ts create mode 100644 src/pages/api/protected.ts create mode 100644 src/pages/api/session.ts create mode 100644 tsconfig.json create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..2c84f790ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +dist +node_modules/ +.env +.next diff --git a/README.md b/README.md new file mode 100644 index 0000000000..32514baf22 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Cal.com Public API (Enterprise Only) diff --git a/next-env.d.ts b/next-env.d.ts new file mode 100644 index 0000000000..4f11a03dc6 --- /dev/null +++ b/next-env.d.ts @@ -0,0 +1,5 @@ +/// +/// + +// NOTE: This file should not be edited +// see https://nextjs.org/docs/basic-features/typescript for more information. diff --git a/package.json b/package.json new file mode 100644 index 0000000000..297108c12e --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "@calcom/api", + "version": "1.0.0", + "description": "Public API for Cal.com", + "main": "index.ts", + "repository": "git@github.com:calcom/api.git", + "author": "Cal.com Inc.", + "private": true, + "scripts": { + "dev": "PORT=3002 next", + "start": "next start", + "build": "next build", + "lint": "next lint", + "type-check": "tsc --pretty --noEmit", + "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist" + }, + "devDependencies": { + "@calcom/prisma": "*", + "@calcom/tsconfig": "*", + "scripts": "*", + "typescript": "^4.5.3" + }, + "dependencies": { + "@next-auth/prisma-adapter": "^1.0.1", + "next": "^12.1.0" + } +} \ No newline at end of file diff --git a/src/pages/api/auth/[...nextauth].ts b/src/pages/api/auth/[...nextauth].ts new file mode 100644 index 0000000000..543d94f7d1 --- /dev/null +++ b/src/pages/api/auth/[...nextauth].ts @@ -0,0 +1,33 @@ +import { PrismaAdapter } from "@next-auth/prisma-adapter"; +import NextAuth from "next-auth"; +import EmailProvider from "next-auth/providers/email"; + +import { defaultCookies } from "@calcom/lib/default-cookies"; +import { serverConfig } from "@calcom/lib/serverConfig"; +import prisma from "@calcom/prisma"; + +const WEBSITE_BASE_URL = process.env.WEBSITE_BASE_URL || ""; + + +export default NextAuth({ + adapter: PrismaAdapter(prisma), + providers: [ + EmailProvider({ + maxAge: 10 * 60 * 60, // Magic links are valid for 10 min only + // sendVerificationRequest, + }), + ], + secret: process.env.SECRET, + cookies: defaultCookies(WEBSITE_BASE_URL?.startsWith("https://")), + session: { + strategy: "jwt", + }, + jwt: { + // A secret to use for key generation (you should set this explicitly) + secret: process.env.SECRET, + // Set to true to use encryption (default: false) + // encryption: true, + }, + // Enable debug messages in the console if you are having problems + debug: true, +}); diff --git a/src/pages/api/jwt.ts b/src/pages/api/jwt.ts new file mode 100644 index 0000000000..f33002aad6 --- /dev/null +++ b/src/pages/api/jwt.ts @@ -0,0 +1,12 @@ +import type { NextApiRequest, NextApiResponse } from "next" +import { getToken } from "next-auth/jwt"; + +const secret = process.env.NEXTAUTH_SECRET; + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + // if using `NEXTAUTH_SECRET` env variable, we detect it, and you won't actually need to `secret` + // const token = await getToken({ req }) + const token = await getToken({ req, secret, raw: true }); + console.log("JSON Web Token", token); + res.end() +} \ No newline at end of file diff --git a/src/pages/api/protected.ts b/src/pages/api/protected.ts new file mode 100644 index 0000000000..27e06b9ea0 --- /dev/null +++ b/src/pages/api/protected.ts @@ -0,0 +1,18 @@ +// This is an example of to protect an API route +import { getSession } from "next-auth/react" +import type { NextApiRequest, NextApiResponse } from "next" + +export default async (req: NextApiRequest, res: NextApiResponse) => { + const session = await getSession({ req }) + + if (session) { + res.send({ + content: + "This is protected content. You can access this content because you are signed in.", + }) + } else { + res.send({ + error: "You must be signed in to view the protected content on this page.", + }) + } +}; diff --git a/src/pages/api/session.ts b/src/pages/api/session.ts new file mode 100644 index 0000000000..2a1551dd0e --- /dev/null +++ b/src/pages/api/session.ts @@ -0,0 +1,8 @@ +// This is an example of how to access a session from an API route +import { getSession } from "next-auth/react" +import type { NextApiRequest, NextApiResponse } from "next" + +export default async (req: NextApiRequest, res: NextApiResponse) => { + const session = await getSession({ req }) + res.send(JSON.stringify(session, null, 2)) +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000000..fd9c2c4aee --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,23 @@ +{ + "extends": "@calcom/tsconfig/base.json", + "compilerOptions": { + "lib": [ + "ES2015" + ], + "module": "CommonJS", + "outDir": "./dist", + "rootDir": "./src", + "target": "es5", + "allowJs": true, + "noEmit": true, + "incremental": true, + "resolveJsonModule": true, + "jsx": "preserve" + }, + "exclude": [ + "node_modules" + ], + "include": [ + "src" + ] +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000000..bc4aae706e --- /dev/null +++ b/yarn.lock @@ -0,0 +1,137 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@next/env@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/env/-/env-12.1.0.tgz#73713399399b34aa5a01771fb73272b55b22c314" + integrity sha512-nrIgY6t17FQ9xxwH3jj0a6EOiQ/WDHUos35Hghtr+SWN/ntHIQ7UpuvSi0vaLzZVHQWaDupKI+liO5vANcDeTQ== + +"@next/swc-android-arm64@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-android-arm64/-/swc-android-arm64-12.1.0.tgz#865ba3a9afc204ff2bdeea49dd64d58705007a39" + integrity sha512-/280MLdZe0W03stA69iL+v6I+J1ascrQ6FrXBlXGCsGzrfMaGr7fskMa0T5AhQIVQD4nA/46QQWxG//DYuFBcA== + +"@next/swc-darwin-arm64@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-darwin-arm64/-/swc-darwin-arm64-12.1.0.tgz#08e8b411b8accd095009ed12efbc2f1d4d547135" + integrity sha512-R8vcXE2/iONJ1Unf5Ptqjk6LRW3bggH+8drNkkzH4FLEQkHtELhvcmJwkXcuipyQCsIakldAXhRbZmm3YN1vXg== + +"@next/swc-darwin-x64@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-darwin-x64/-/swc-darwin-x64-12.1.0.tgz#fcd684497a76e8feaca88db3c394480ff0b007cd" + integrity sha512-ieAz0/J0PhmbZBB8+EA/JGdhRHBogF8BWaeqR7hwveb6SYEIJaDNQy0I+ZN8gF8hLj63bEDxJAs/cEhdnTq+ug== + +"@next/swc-linux-arm-gnueabihf@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-linux-arm-gnueabihf/-/swc-linux-arm-gnueabihf-12.1.0.tgz#9ec6380a27938a5799aaa6035c205b3c478468a7" + integrity sha512-njUd9hpl6o6A5d08dC0cKAgXKCzm5fFtgGe6i0eko8IAdtAPbtHxtpre3VeSxdZvuGFh+hb0REySQP9T1ttkog== + +"@next/swc-linux-arm64-gnu@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-12.1.0.tgz#7f4196dff1049cea479607c75b81033ae2dbd093" + integrity sha512-OqangJLkRxVxMhDtcb7Qn1xjzFA3s50EIxY7mljbSCLybU+sByPaWAHY4px97ieOlr2y4S0xdPKkQ3BCAwyo6Q== + +"@next/swc-linux-arm64-musl@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-12.1.0.tgz#b445f767569cdc2dddee785ca495e1a88c025566" + integrity sha512-hB8cLSt4GdmOpcwRe2UzI5UWn6HHO/vLkr5OTuNvCJ5xGDwpPXelVkYW/0+C3g5axbDW2Tym4S+MQCkkH9QfWA== + +"@next/swc-linux-x64-gnu@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-12.1.0.tgz#67610e9be4fbc987de7535f1bcb17e45fe12f90e" + integrity sha512-OKO4R/digvrVuweSw/uBM4nSdyzsBV5EwkUeeG4KVpkIZEe64ZwRpnFB65bC6hGwxIBnTv5NMSnJ+0K/WmG78A== + +"@next/swc-linux-x64-musl@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-12.1.0.tgz#ea19a23db08a9f2e34ac30401f774cf7d1669d31" + integrity sha512-JohhgAHZvOD3rQY7tlp7NlmvtvYHBYgY0x5ZCecUT6eCCcl9lv6iV3nfu82ErkxNk1H893fqH0FUpznZ/H3pSw== + +"@next/swc-win32-arm64-msvc@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-12.1.0.tgz#eadf054fc412085659b98e145435bbba200b5283" + integrity sha512-T/3gIE6QEfKIJ4dmJk75v9hhNiYZhQYAoYm4iVo1TgcsuaKLFa+zMPh4056AHiG6n9tn2UQ1CFE8EoybEsqsSw== + +"@next/swc-win32-ia32-msvc@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-12.1.0.tgz#68faeae10c89f698bf9d28759172b74c9c21bda1" + integrity sha512-iwnKgHJdqhIW19H9PRPM9j55V6RdcOo6rX+5imx832BCWzkDbyomWnlzBfr6ByUYfhohb8QuH4hSGEikpPqI0Q== + +"@next/swc-win32-x64-msvc@12.1.0": + version "12.1.0" + resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-12.1.0.tgz#d27e7e76c87a460a4da99c5bfdb1618dcd6cd064" + integrity sha512-aBvcbMwuanDH4EMrL2TthNJy+4nP59Bimn8egqv6GHMVj0a44cU6Au4PjOhLNqEh9l+IpRGBqMTzec94UdC5xg== + +"@types/node@^17.0.21": + version "17.0.21" + resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.21.tgz#864b987c0c68d07b4345845c3e63b75edd143644" + integrity sha512-DBZCJbhII3r90XbQxI8Y9IjjiiOGlZ0Hr32omXIZvwwZ7p4DMMXGrKXVyPfuoBOri9XNtL0UK69jYIBIsRX3QQ== + +caniuse-lite@^1.0.30001283: + version "1.0.30001317" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001317.tgz#0548fb28fd5bc259a70b8c1ffdbe598037666a1b" + integrity sha512-xIZLh8gBm4dqNX0gkzrBeyI86J2eCjWzYAs40q88smG844YIrN4tVQl/RhquHvKEKImWWFIVh1Lxe5n1G/N+GQ== + +nanoid@^3.1.30: + version "3.3.1" + resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.1.tgz#6347a18cac88af88f58af0b3594b723d5e99bb35" + integrity sha512-n6Vs/3KGyxPQd6uO0eH4Bv0ojGSUvuLlIHtC3Y0kEO23YRge8H9x1GCzLn28YX0H66pMkxuaeESFq4tKISKwdw== + +next@^12.1.0: + version "12.1.0" + resolved "https://registry.yarnpkg.com/next/-/next-12.1.0.tgz#c33d753b644be92fc58e06e5a214f143da61dd5d" + integrity sha512-s885kWvnIlxsUFHq9UGyIyLiuD0G3BUC/xrH0CEnH5lHEWkwQcHOORgbDF0hbrW9vr/7am4ETfX4A7M6DjrE7Q== + dependencies: + "@next/env" "12.1.0" + caniuse-lite "^1.0.30001283" + postcss "8.4.5" + styled-jsx "5.0.0" + use-subscription "1.5.1" + optionalDependencies: + "@next/swc-android-arm64" "12.1.0" + "@next/swc-darwin-arm64" "12.1.0" + "@next/swc-darwin-x64" "12.1.0" + "@next/swc-linux-arm-gnueabihf" "12.1.0" + "@next/swc-linux-arm64-gnu" "12.1.0" + "@next/swc-linux-arm64-musl" "12.1.0" + "@next/swc-linux-x64-gnu" "12.1.0" + "@next/swc-linux-x64-musl" "12.1.0" + "@next/swc-win32-arm64-msvc" "12.1.0" + "@next/swc-win32-ia32-msvc" "12.1.0" + "@next/swc-win32-x64-msvc" "12.1.0" + +object-assign@^4.1.1: + version "4.1.1" + resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" + integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= + +picocolors@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c" + integrity sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ== + +postcss@8.4.5: + version "8.4.5" + resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.5.tgz#bae665764dfd4c6fcc24dc0fdf7e7aa00cc77f95" + integrity sha512-jBDboWM8qpaqwkMwItqTQTiFikhs/67OYVvblFFTM7MrZjt6yMKd6r2kgXizEbTTljacm4NldIlZnhbjr84QYg== + dependencies: + nanoid "^3.1.30" + picocolors "^1.0.0" + source-map-js "^1.0.1" + +source-map-js@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c" + integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw== + +styled-jsx@5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-5.0.0.tgz#816b4b92e07b1786c6b7111821750e0ba4d26e77" + integrity sha512-qUqsWoBquEdERe10EW8vLp3jT25s/ssG1/qX5gZ4wu15OZpmSMFI2v+fWlRhLfykA5rFtlJ1ME8A8pm/peV4WA== + +use-subscription@1.5.1: + version "1.5.1" + resolved "https://registry.yarnpkg.com/use-subscription/-/use-subscription-1.5.1.tgz#73501107f02fad84c6dd57965beb0b75c68c42d1" + integrity sha512-Xv2a1P/yReAjAbhylMfFplFKj9GssgTwN7RlcTxBujFQcloStWNDQdc4g4NRWH9xS4i/FDk04vQBptAXoF3VcA== + dependencies: + object-assign "^4.1.1"