-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Screenshot 📸 <img width="500" alt="image" src="https://github.com/dotkom/monoweb/assets/24441708/d551e090-61ae-4689-8c9e-7a52dc08e1cc"> # No auth endpoints To test it locally you go to - `http://localhost:3000/api/cal/all` - ~~`http://localhost:3000/api/cal/user/01HB64XF7WXBPGVQKFKFGJBH4D` (or another id ofc)~~ - `http://localhost:3000/api/cal/event/01HB64TWZK1N8ABMH8JAE12101` # Auth (new :)))))) To get access to a users calendar, that signed in user must go to `/api/cal/sign` and then use the returned token in `/api/cal/user/${token}` ```sh # not authed $ curl http://localhost:3000/api/cal/sign {"message":"Not signed in"} # getting the token $ curl 'http://localhost:3000/api/cal/sign' -H 'Cookie: next-auth.session-token=...' {"token":"eyJhbGciOiJIUzI1NiJ9.MDFIUTZSNkdFWVcyWjNWV1ZCU0FYWFRLQlo.XklF4gPVajozOE8ZsImhHFZXzZc-fF6qQgeX5xNvOoM"} # the contents $ base64 -d <<< $(echo MDFIUTZSNkdFWVcyWjNWV1ZCU0FYWFRLQlo) 01HQ6R6GEYW2Z3VWVBSAXXTK # downloading the calendar (can add this link to calendar app, google calendar, etc) $ curl http://localhost:3000/api/cal/user/eyJhbGciOiJIUzI1NiJ9.MDFIUTZSNkdFWVcyWjNWV1ZCU0FYWFRLQlo.XklF4gPVajozOE8ZsImhHFZXzZc-fF6qQgeX5xNvOoM BEGIN:VCALENDAR VERSION:2.0 PRODID:-//sebbo.net//ical-generator//EN NAME:01HQ6R6GEYW2Z3VWVBSAXXTKBZ online kalender <...> ```
- Loading branch information
Showing
13 changed files
with
1,877 additions
and
3,493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { CalendarAll } from "@dotkomonline/gateway-edge-nextjs" | ||
|
||
export default CalendarAll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { CalendarEvent } from "@dotkomonline/gateway-edge-nextjs" | ||
|
||
export default CalendarEvent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { CalendarSign } from "@dotkomonline/gateway-edge-nextjs" | ||
|
||
export default CalendarSign |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { CalendarUser } from "@dotkomonline/gateway-edge-nextjs" | ||
|
||
export default CalendarUser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { type NextApiRequest, type NextApiResponse } from "next" | ||
import ical, { type ICalEventData } from "ical-generator" | ||
import { type Event } from "@dotkomonline/types" | ||
import { createServerSideHelpers } from "@trpc/react-query/server" | ||
import { appRouter, createContextInner, transformer } from "@dotkomonline/gateway-trpc" | ||
import { getServerSession } from "next-auth" | ||
import jwt from "jsonwebtoken" | ||
import { authOptions } from "../../../auth/src/web.app" | ||
|
||
const helpers = createServerSideHelpers({ | ||
router: appRouter, | ||
ctx: await createContextInner({ | ||
auth: null, | ||
}), | ||
transformer, // optional - adds superjson serialization | ||
}) | ||
|
||
function eventUrl(req: NextApiRequest, event: Pick<Event, "id">) { | ||
const proto = req.headers["x-forwarded-proto"] || "http" | ||
const host = req.headers["x-forwarded-host"] || "online.ntnu.no" | ||
|
||
// a better to to get/configure the url? | ||
return `${proto}://${host}/events/${event.id}` | ||
} | ||
|
||
// ALL events | ||
export async function CalendarAll(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method !== "GET") { | ||
res.status(405).end() | ||
return | ||
} | ||
|
||
const instance = ical({ name: "Online Linjeforening Arrangementer" }) | ||
|
||
const events = await helpers.event.all.fetch() | ||
|
||
for (const event of events) { | ||
instance.createEvent(toICal(req, event)) | ||
} | ||
|
||
res.status(200).send(instance.toString()) | ||
} | ||
|
||
// a single event | ||
export async function CalendarEvent(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method !== "GET") { | ||
res.status(405).end() | ||
return | ||
} | ||
|
||
const eventid = req.query.eventid as string | ||
if (!eventid) { | ||
res.status(400).json({ message: "Missing eventid" }) | ||
return | ||
} | ||
|
||
const event = (await helpers.event.get.fetch(eventid)).event | ||
|
||
const instance = ical() | ||
instance.createEvent(toICal(req, event)) | ||
|
||
res.status(200).send(instance.toString()) | ||
} | ||
|
||
// all events a user is attending | ||
export async function CalendarUser(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method !== "GET") { | ||
res.status(405).end() | ||
return | ||
} | ||
|
||
const token = req.query.user as string | ||
if (!token) { | ||
res.status(400).json({ message: "Missing token" }) | ||
return | ||
} | ||
|
||
const cal_key = process.env.CAL_KEY | ||
if (!cal_key) { | ||
res.status(500).json({ message: "Missing key" }) | ||
} | ||
|
||
let userid = "" | ||
try { | ||
userid = jwt.verify(token, cal_key as string) as string | ||
} catch { | ||
res.status(400).json({ message: "bad key" }) | ||
} | ||
|
||
const events = await helpers.event.allByUserId.fetch({ id: userid }) | ||
const instance = ical({ name: `${userid} online kalender` }) | ||
|
||
for (const event of events) { | ||
instance.createEvent(toICal(req, event)) | ||
instance.createEvent(toICal(req, toRegistration(event))) | ||
} | ||
|
||
res.status(200).send(instance.toString()) | ||
} | ||
|
||
// make a token for the signed in user | ||
export async function CalendarSign(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method !== "GET") { | ||
res.status(405).end() | ||
return | ||
} | ||
|
||
const session = await getServerSession(req, res, authOptions) | ||
const authed_id = session?.user.id | ||
if (!authed_id) { | ||
res.status(400).json({ message: "Not signed in" }) | ||
} | ||
|
||
const cal_key = process.env.CAL_KEY | ||
if (!cal_key) { | ||
res.status(500).json({ message: "Missing key" }) | ||
} | ||
|
||
const token = jwt.sign(authed_id as string, cal_key as string, {}) | ||
|
||
res.status(200).json({ token }) | ||
} | ||
|
||
function toICal( | ||
req: NextApiRequest, | ||
event: Pick<Event, "description" | "end" | "id" | "location" | "start" | "title"> | ||
): ICalEventData { | ||
return { | ||
start: event.start, | ||
end: event.end, | ||
summary: event.title, | ||
description: event.description, | ||
location: event.location, | ||
url: eventUrl(req, event), | ||
} | ||
} | ||
|
||
function toRegistration( | ||
event: Pick<Event, "description" | "id" | "start" | "title"> | ||
): Pick<Event, "description" | "end" | "id" | "location" | "start" | "title"> { | ||
// 5 days before | ||
// TODO when db has this, we can use the actual start value, this is just for testing | ||
const start = new Date(event.start.getTime() - 5 * 24 * 60 * 60 * 1000) | ||
|
||
const title = `Påmelding for ${event.title}` | ||
const location = "På OW" | ||
|
||
return { | ||
start, | ||
end: start, // 0 length | ||
title, | ||
description: event.description, | ||
location, | ||
id: event.id, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./stripe/stripe-webhook" | ||
export * from "./cal/cal" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.