Skip to content

Commit

Permalink
feat(webapp): add header authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Dec 31, 2023
1 parent 8a86b8f commit b948cbe
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default defineConfig({
'^/whip/.*': 'http://localhost:7777',
'^/whep/.*': 'http://localhost:7777',
'^/room/.*': 'http://localhost:4000',
'^/user/.*': 'http://localhost:4000',
},
},
build: {
Expand Down
4 changes: 3 additions & 1 deletion webapp/components/join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -14,12 +14,14 @@ export default function Join() {
const [tmpId, setTmpId] = useState<string>("")

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"
//})
Expand Down
43 changes: 41 additions & 2 deletions webapp/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
const StreamIdKey = 'stream'

function setStreamId(id: string) {
localStorage.setItem(StreamIdKey, id)
}

let token = ""

interface Room {
roomId: string,
Expand All @@ -7,27 +14,47 @@ interface Room {
streams: any,
}


interface Stream {
name: string,
audio: boolean,
video: boolean,
screen: boolean,
}

function setToken(str: string) {
token = str
}

async function newUser(): Promise<string> {
const res = await (await fetch(`/user/`, {
method: "POST",
})).json()
setToken(res.token)
setStreamId(res.streamId)
return res.streamId
}

async function newRoom(): Promise<Room> {
return (await fetch(`/room/`, {
headers: {
"Authorization": `Bearer ${token}`,
},
method: "POST",
})).json()
}

async function getRoom(roomId: string): Promise<Room> {
return (await fetch(`/room/${roomId}`)).json()
return (await fetch(`/room/${roomId}`, {
headers: {
"Authorization": `Bearer ${token}`,
},
})).json()
}

async function setRoom(roomId: string, data: any): Promise<Room> {
return (await fetch(`/room/${roomId}`, {
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json",
},
method: "PATCH",
Expand All @@ -37,19 +64,26 @@ async function setRoom(roomId: string, data: any): Promise<Room> {

async function delRoom(roomId: string): Promise<void> {
return (await fetch(`/room/${roomId}`, {
headers: {
"Authorization": `Bearer ${token}`,
},
method: "DELETE",
})).json()
}

async function newStream(roomId: string): Promise<Stream> {
return (await fetch(`/room/${roomId}/stream`, {
headers: {
"Authorization": `Bearer ${token}`,
},
method: "POST",
})).json()
}

async function setStream(roomId: string, streamId: string, data: any): Promise<Stream> {
return (await fetch(`/room/${roomId}/stream/${streamId}`, {
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json",
},
method: "PATCH",
Expand All @@ -59,11 +93,16 @@ async function setStream(roomId: string, streamId: string, data: any): Promise<S

async function delStream(roomId: string, streamId: string): Promise<void> {
return (await fetch(`/room/${roomId}/stream/${streamId}`, {
headers: {
"Authorization": `Bearer ${token}`,
},
method: "DELETE",
})).json()
}

export {
newUser,

newRoom,
getRoom,
setRoom,
Expand Down
25 changes: 4 additions & 21 deletions webapp/lib/storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { newUser } from "./api"

const MeetingIdKey = 'meeting'
const StreamIdKey = 'stream'

Expand All @@ -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<string> {
let res = await (await fetch(`/room/${meetingId}/stream`, {
method: "POST"
})).json()
return res.streamId
}

async function asyncGetStreamId(): Promise<string> {
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,
}

0 comments on commit b948cbe

Please sign in to comment.