Skip to content

Commit

Permalink
feat: support for using token authentication on room package (#82)
Browse files Browse the repository at this point in the history
* feat: added apikey as aditional paramter on room sdk

* doc: update documentation regarding authentication

* feat: add auth on end room request
  • Loading branch information
ghaniswara authored Dec 5, 2023
1 parent ba42e11 commit 3d22efe
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 10 deletions.
22 changes: 20 additions & 2 deletions packages/room/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@ import { Room, RoomEvent } from '@inlivedev/inlive-js-sdk';
// Or if you prefer to load only the room module
import { Room, RoomEvent } from '@inlivedev/inlive-js-sdk/dist/room.js';

const room = Room()
const room = Room({
api : {
// Aditional parameter, required for some function
apiKey : YOUR_API_KEY
}
})
```

### Authentication
Some function in the Room Object require the apiKey parameter to be defined, since the SDK is designed to be used on Client and Server Side

If the Library is used on the client side you might not need to pass the `apiKey` parameter

The following function require apiKey to be defined :
* `Room.createRoom()`
* `Room.getRoom()`

### Room object

The room object is the object created when the `Room` module is initialized. It consists methods that relates to the room scope. Some methods that directly interact to the room API can run on both client and server sides.
Expand Down Expand Up @@ -54,12 +68,16 @@ await room.endRoom(roomData.data.roomId);

#### Methods

- `room.createRoom(name?: string | undefined, id?: string | undefined)`
- `room.createRoom(name?: string | undefined, id?: string | undefined)`

> 🔐 Require ApiKey
A method to create a new room. If the optional `name` and `id` parameters are passed, the room will be created under those name and id. This method will return a promise.

- `room.getRoom(roomId: string)`

> 🔐 Require ApiKey
A method to get the room data. It expects a `roomId` as a parameter. This method will return a promise.

- `room.createClient(roomId: string, config?: object)`
Expand Down
9 changes: 7 additions & 2 deletions packages/room/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const createApi = ({ fetcher }) => {
createRoom = async (name = '', id = '') => {
/** @type {import('./api-types.js').RoomAPIType.CreateRoomResponseBody} */
const response = await this._fetcher.post(`/rooms/create`, {
headers: { Authorization: 'Bearer ' + this._fetcher.getApiKey() },
body: JSON.stringify({ name, id }),
})

Expand Down Expand Up @@ -54,7 +55,9 @@ export const createApi = ({ fetcher }) => {
}

/** @type {import('./api-types.js').RoomAPIType.GetRoomResponseBody} */
const response = await this._fetcher.get(`/rooms/${roomId}`)
const response = await this._fetcher.get(`/rooms/${roomId}`, {
headers: { Authorization: 'Bearer ' + this._fetcher.getApiKey() },
})

const data = response.data || {}
const bitrates = data.bitrates_config || {}
Expand Down Expand Up @@ -439,7 +442,9 @@ export const createApi = ({ fetcher }) => {
}

/** @type {import('./api-types.js').RoomAPIType.BaseResponseBody} */
const response = await this._fetcher.put(`/rooms/${roomId}/end`)
const response = await this._fetcher.put(`/rooms/${roomId}/end`, {
headers: { Authorization: 'Bearer ' + this._fetcher.getApiKey() },
})

const result = {
code: response.code || 500,
Expand Down
23 changes: 18 additions & 5 deletions packages/room/api/fetcher.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export const createFetcher = () => {
const Fetcher = class {
_apiKey
_baseUrl

/** @param {string} baseUrl */
constructor(baseUrl) {
/**
* @param {string} apiKey
* @param {string} baseUrl
*/
constructor(baseUrl, apiKey) {
this._baseUrl = baseUrl
this._apiKey = apiKey
}

/**
Expand Down Expand Up @@ -63,6 +68,10 @@ export const createFetcher = () => {
return this._baseUrl
}

getApiKey = () => {
return this._apiKey
}

/**
* @param {string} endpoint
* @param {RequestInit | undefined} [options]
Expand Down Expand Up @@ -120,11 +129,15 @@ export const createFetcher = () => {
}

return {
/** @param {string} baseUrl */
createInstance: (baseUrl) => {
const fetcher = new Fetcher(baseUrl)
/**
* @param {string} baseUrl
* @param {string} apiKey
*/
createInstance: (baseUrl, apiKey) => {
const fetcher = new Fetcher(baseUrl, apiKey)

return {
getApiKey: fetcher.getApiKey,
getBaseUrl: fetcher.getBaseUrl,
get: fetcher.get,
post: fetcher.post,
Expand Down
1 change: 1 addition & 0 deletions packages/room/config/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const api = {
baseUrl: 'https://hub.inlive.app',
version: 'v1',
apiKey: '',
}

export const webrtc = {
Expand Down
3 changes: 2 additions & 1 deletion packages/room/facade/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const createFacade = ({
merge(config, userConfig)

const baseUrl = `${config.api.baseUrl}/${config.api.version}`
const fetcher = createFetcher().createInstance(baseUrl)
const apiKey = config.api.apiKey
const fetcher = createFetcher().createInstance(baseUrl, apiKey)
const api = createApi({
fetcher,
}).createInstance()
Expand Down
2 changes: 2 additions & 0 deletions packages/room/room-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export declare namespace RoomType {
api: {
baseUrl: string
version: string
apiKey: string
}
webrtc: {
iceServers: RTCIceServer[]
Expand Down Expand Up @@ -31,6 +32,7 @@ export declare namespace RoomType {
api?: {
baseUrl?: string
version?: string
apiKey?: string
}
webrtc?: {
iceServers?: RTCIceServer[]
Expand Down

0 comments on commit 3d22efe

Please sign in to comment.