Skip to content

Commit

Permalink
Update Tauri OAuth example (#1112)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Sep 14, 2023
1 parent 1b24b5a commit ad11184
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
32 changes: 0 additions & 32 deletions examples/electron/github-oauth/app/src/renderer.ts

This file was deleted.

2 changes: 1 addition & 1 deletion examples/tauri/github-oauth/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<h1>GitHub OAuth with Lucia</h1>
<div>
<h2>Profile</h2>
<code><pre id="profile">(none)</pre></code>
<code><pre id="profile"></pre></code>
</div>
<div>
<button id="login-button">Sign in with GitHub</button>
Expand Down
30 changes: 22 additions & 8 deletions examples/tauri/github-oauth/app/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,50 @@
import { invoke } from "@tauri-apps/api/tauri";
import { getClient, ResponseType } from "@tauri-apps/api/http";

window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("DOMContentLoaded", async () => {
const loginButton = document.getElementById("login-button");
const profileBody = document.getElementById("profile");
const logoutButton = document.getElementById("logout-button");
if (!loginButton || !profileBody || !logoutButton) return;

const renderUserProfile = (user: User | null) => {
profileBody.textContent = user ? JSON.stringify(user, null, 2) : "(none)";
};

loginButton.addEventListener("click", async () => {
try {
const sessionToken = await invoke<string>("authenticate");
localStorage.setItem("session_token", sessionToken);
const user = await getUser(sessionToken);
profileBody.textContent = JSON.stringify(user, null, 2);
renderUserProfile(user);
} catch (e) {
console.log(e);
}
});
logoutButton.addEventListener("click", async () => {
const sessionToken = localStorage.getItem("session_token");
if (!sessionToken) return
if (!sessionToken) return;
const client = await getClient();
const response = await client.request({
url: "http://localhost:3000/logout",
method: "POST",
url: "http://localhost:3000/logout",
method: "POST",
headers: {
Authorization: `Bearer ${sessionToken}`
}
});
if (!response.ok) return
localStorage.removeItem("session_token");
profileBody.textContent = "(none)"
if (!response.ok) return;
localStorage.removeItem("session_token");
renderUserProfile(null);
});

const sessionToken = localStorage.getItem("session_token");
if (sessionToken) {
const user = await getUser(sessionToken);
if (!user) {
localStorage.removeItem("session_token");
}
renderUserProfile(user);
}
});

const getUser = async (sessionToken: string): Promise<User | null> => {
Expand Down

0 comments on commit ad11184

Please sign in to comment.