Skip to content

Commit

Permalink
Merge pull request #317 from abhinavkrin/new/added-loginWithRocketCha…
Browse files Browse the repository at this point in the history
…tOAuth

added loginWithRocketChatOAuth
  • Loading branch information
abhinavkrin authored Nov 19, 2023
2 parents 754db19 + ad9c390 commit 0d8994d
Show file tree
Hide file tree
Showing 12 changed files with 373 additions and 236 deletions.
5 changes: 4 additions & 1 deletion packages/api/playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<input type="password" id="password" placeholder="Password">
<button id="loginWithPassword">Sign in</button>
</div>
<div class="input-group">
<button id="loginWithRocketChatOAuth">Sign in with RocketChat OAuth</button>
</div>
<div class="input-group">
<label for="userId">User Id</label>
<input type="text" id="userId" disabled>
Expand Down Expand Up @@ -80,4 +83,4 @@ <h4>API response</h4>
</div>
<script type="module" src="./playground.js" ></script>
</body>
</html>
</html>
33 changes: 31 additions & 2 deletions packages/api/playground/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,25 @@ async function loginWithPassword() {
}
}

async function loginWithRocketChatOAuth() {
try {
await api.auth.loginWithRocketChatOAuth();
} catch (e) {
printResult(e);
}
}

async function printResult(result) {
window.document.getElementById("output").innerHTML = "\n" + JSON.stringify(result, null, 2);
const outputElement = window.document.getElementById("output");
if (result instanceof Error) {
if (outputElement) {
const escapedMessage = escapeHTML(result.message);
const escapedStack = escapeHTML(result.stack);
outputElement.innerHTML = `<code>\nError: ${escapedMessage}\nStack: ${escapedStack}</co>`;
}
} else {
outputElement.innerHTML = "\n" + JSON.stringify(result, null, 2);
}
}

const msgListener = msg => {
Expand Down Expand Up @@ -82,7 +99,10 @@ const callApi = async (e) => {
window.addEventListener('DOMContentLoaded', () => {
console.log('Ready')
document.getElementById("loginWithPassword").addEventListener("click", loginWithPassword)

document
.getElementById("loginWithRocketChatOAuth")
.addEventListener("click", loginWithRocketChatOAuth);

const hostInput = document.getElementById("hostUrl")
const roomInput = document.getElementById("roomId")
const host = hostInput.value;
Expand All @@ -95,3 +115,12 @@ window.addEventListener('DOMContentLoaded', () => {
document.getElementById("call-api").addEventListener("click", callApi)
})

function escapeHTML(str) {
return str.replace(
/[&<>'"]/g,
(tag) =>
({ "&": "&amp;", "<": "&lt;", ">": "&gt;", "'": "&#39;", '"': "&quot;" }[
tag
] || tag)
);
}
5 changes: 4 additions & 1 deletion packages/auth/playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
</textarea>
<button id="loginWithOAuth">Sign in</button>
</div>
<div class="input-group">
<button id="loginWithRocketChatOAuth">Sign in with RocketChat OAuth</button>
</div>
</div>
<div class="playground-output">
<div>
Expand All @@ -58,4 +61,4 @@
</div>
<script type="module" src="./playground.js" ></script>
</body>
</html>
</html>
31 changes: 29 additions & 2 deletions packages/auth/playground/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,31 @@ async function loginWithOAuth() {
}
}

async function loginWithRocketChatOAuth() {
try {
await auth.loginWithRocketChatOAuth();
} catch (e) {
printResult(e);
}
}

async function printResult(result) {
window.document.getElementById("output").innerHTML = "\n" + JSON.stringify(result, null, 2);
const outputElement = window.document.getElementById("output");
if (result instanceof Error) {
if (outputElement) {
const escapedMessage = escapeHTML(result.message);
const escapedStack = escapeHTML(result.stack);
outputElement.innerHTML = `<code>\nError: ${escapedMessage}\nStack: ${escapedStack}</co>`;
}
} else {
outputElement.innerHTML = "\n" + JSON.stringify(result, null, 2);

Check warning

Code scanning / CodeQL

Exception text reinterpreted as HTML Medium

Exception text
is reinterpreted as HTML without escaping meta-characters.
}
}

window.document.body.onload = () => {
document.getElementById("loginWithPassword").addEventListener("click", loginWithPassword)
document.getElementById("loginWithOAuth").addEventListener("click", loginWithOAuth)

document.getElementById("loginWithRocketChatOAuth").addEventListener("click", loginWithRocketChatOAuth);
const hostInput = document.getElementById("hostUrl")
const host = hostInput.value;
auth = rocketChatAuth({
Expand All @@ -66,3 +83,13 @@ window.document.body.onload = () => {
document.getElementById("logoutBtn").addEventListener("click", () => auth.logout())
}


function escapeHTML(str) {
return str.replace(
/[&<>'"]/g,
(tag) =>
({ "&": "&amp;", "<": "&lt;", ">": "&gt;", "'": "&#39;", '"': "&quot;" }[
tag
] || tag)
);
}
Loading

0 comments on commit 0d8994d

Please sign in to comment.