diff --git a/vite.config.ts b/vite.config.ts index 75ab0d0..3995f08 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -12,6 +12,7 @@ export default defineConfig({ '^/whip/.*': 'http://localhost:7777', '^/whep/.*': 'http://localhost:7777', '^/room/.*': 'http://localhost:4000', + '^/user/.*': 'http://localhost:4000', }, }, build: { diff --git a/webapp/components/join.tsx b/webapp/components/join.tsx index 3e90a60..f43d27b 100644 --- a/webapp/components/join.tsx +++ b/webapp/components/join.tsx @@ -5,7 +5,7 @@ import { meetingIdAtom, } from '../store/atom' import { setMeetingId } from '../lib/storage' -import { newRoom } from '../lib/api' +import { newRoom, newUser } from '../lib/api' export default function Join() { const [loc, setLoc] = useAtom(locationAtom) @@ -14,12 +14,14 @@ export default function Join() { const [tmpId, setTmpId] = useState("") const newMeeting = async () => { + await newUser() let meetingId = (await newRoom()).roomId enterMeeting(meetingId) } const joinMeeting = async () => { let meetingId = tmpId + await newUser() //await fetch(`/room/${meetingId}`, { // method: "PATCH" //}) diff --git a/webapp/lib/api.ts b/webapp/lib/api.ts index d0f456a..180f755 100644 --- a/webapp/lib/api.ts +++ b/webapp/lib/api.ts @@ -1,3 +1,10 @@ +const StreamIdKey = 'stream' + +function setStreamId(id: string) { + localStorage.setItem(StreamIdKey, id) +} + +let token = "" interface Room { roomId: string, @@ -7,7 +14,6 @@ interface Room { streams: any, } - interface Stream { name: string, audio: boolean, @@ -15,19 +21,40 @@ interface Stream { screen: boolean, } +function setToken(str: string) { + token = str +} + +async function newUser(): Promise { + const res = await (await fetch(`/user/`, { + method: "POST", + })).json() + setToken(res.token) + setStreamId(res.streamId) + return res.streamId +} + async function newRoom(): Promise { return (await fetch(`/room/`, { + headers: { + "Authorization": `Bearer ${token}`, + }, method: "POST", })).json() } async function getRoom(roomId: string): Promise { - return (await fetch(`/room/${roomId}`)).json() + return (await fetch(`/room/${roomId}`, { + headers: { + "Authorization": `Bearer ${token}`, + }, + })).json() } async function setRoom(roomId: string, data: any): Promise { return (await fetch(`/room/${roomId}`, { headers: { + "Authorization": `Bearer ${token}`, "Content-Type": "application/json", }, method: "PATCH", @@ -37,12 +64,18 @@ async function setRoom(roomId: string, data: any): Promise { async function delRoom(roomId: string): Promise { return (await fetch(`/room/${roomId}`, { + headers: { + "Authorization": `Bearer ${token}`, + }, method: "DELETE", })).json() } async function newStream(roomId: string): Promise { return (await fetch(`/room/${roomId}/stream`, { + headers: { + "Authorization": `Bearer ${token}`, + }, method: "POST", })).json() } @@ -50,6 +83,7 @@ async function newStream(roomId: string): Promise { async function setStream(roomId: string, streamId: string, data: any): Promise { return (await fetch(`/room/${roomId}/stream/${streamId}`, { headers: { + "Authorization": `Bearer ${token}`, "Content-Type": "application/json", }, method: "PATCH", @@ -59,11 +93,16 @@ async function setStream(roomId: string, streamId: string, data: any): Promise { return (await fetch(`/room/${roomId}/stream/${streamId}`, { + headers: { + "Authorization": `Bearer ${token}`, + }, method: "DELETE", })).json() } export { + newUser, + newRoom, getRoom, setRoom, diff --git a/webapp/lib/storage.ts b/webapp/lib/storage.ts index 6289678..2943131 100644 --- a/webapp/lib/storage.ts +++ b/webapp/lib/storage.ts @@ -1,3 +1,5 @@ +import { newUser } from "./api" + const MeetingIdKey = 'meeting' const StreamIdKey = 'stream' @@ -6,35 +8,16 @@ function setMeetingId(id: string) { if (id !== oldId) { localStorage.setItem(MeetingIdKey, id) localStorage.removeItem(StreamIdKey) + newUser() } } -function setStreamId(id: string) { - localStorage.setItem(StreamIdKey, id) -} - -async function serverGetStreamId(meetingId: string): Promise { - let res = await (await fetch(`/room/${meetingId}/stream`, { - method: "POST" - })).json() - return res.streamId -} - async function asyncGetStreamId(): Promise { const streamId = localStorage.getItem(StreamIdKey) - if (!!streamId) { - return streamId - } - - const meetingId = localStorage.getItem(MeetingIdKey) - const id = await serverGetStreamId(meetingId) - - setStreamId(id) - return id + return streamId || "" } export { setMeetingId, - setStreamId, asyncGetStreamId, }