Skip to content

Commit

Permalink
experimental unauthenticated mode 🥷
Browse files Browse the repository at this point in the history
  • Loading branch information
mackuba committed Oct 1, 2023
1 parent fb7cbb5 commit 6c67eef
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
23 changes: 15 additions & 8 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ class BlueskyAPI {
#refreshToken;
#userDID;

constructor() {
this.#accessToken = localStorage.getItem('accessToken');
this.#refreshToken = localStorage.getItem('refreshToken');
this.#userDID = localStorage.getItem('userDID');
constructor(host = 'bsky.social', useAuthentication = true) {
this.host = host;
this.useAuthentication = useAuthentication;

if (useAuthentication) {
this.#accessToken = localStorage.getItem('accessToken');
this.#refreshToken = localStorage.getItem('refreshToken');
this.#userDID = localStorage.getItem('userDID');
}

this.handleCache = new HandleCache();
this.profiles = {};
}
Expand All @@ -59,7 +65,7 @@ class BlueskyAPI {
}

async getRequest(method, params) {
let url = 'https://bsky.social/xrpc/' + method;
let url = `https://${this.host}/xrpc/${method}`;

if (params) {
url += '?' + Object.entries(params).map((x) => {
Expand All @@ -71,10 +77,11 @@ class BlueskyAPI {
}).join('&');
}

let response = await fetch(url, { headers: { 'Authorization': `Bearer ${this.#accessToken}` }});
let headers = this.useAuthentication ? { 'Authorization': `Bearer ${this.#accessToken}` } : {};
let response = await fetch(url, { headers: headers });
let json = await this.parseResponse(response);

if (this.isInvalidToken(response, json)) {
if (this.useAuthentication && this.isInvalidToken(response, json)) {
await this.refreshAccessToken();
response = await fetch(url, { headers: { 'Authorization': `Bearer ${this.#accessToken}` }});
json = await this.parseResponse(response);
Expand All @@ -88,7 +95,7 @@ class BlueskyAPI {
}

async postRequest(method, data, useRefreshToken, useAuthentication) {
let url = 'https://bsky.social/xrpc/' + method;
let url = `https://${this.host}/xrpc/${method}`;
let request = { method: 'POST', headers: {}};

if (!(useAuthentication === false)) {
Expand Down
5 changes: 5 additions & 0 deletions post_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ class PostComponent {
}

onHeartClick(heart) {
if (window.unauthed) {
alert('This action in unavailable in unauthenticated mode.');
return;
}

let count = heart.nextElementSibling;

if (!heart.classList.contains('liked')) {
Expand Down
10 changes: 8 additions & 2 deletions skythread.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ function init() {
e.target.classList.remove('click');
});

window.api = new BlueskyAPI();
let params = new URLSearchParams(location.search);
if (params.get('_u') == '1') {
window.unauthed = true;
window.api = new BlueskyAPI('api.bsky.app', false);
} else {
window.api = new BlueskyAPI();
}

if (api.isLoggedIn) {
if (api.isLoggedIn || window.unauthed) {
parseQueryParams();
} else {
showLogin();
Expand Down
5 changes: 5 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ function linkToPostById(handle, postId) {
let url = new URL(getLocation());
url.searchParams.set('author', handle);
url.searchParams.set('post', postId);

if (window.unauthed) {
url.searchParams.set('_u', 1);
}

return url.toString();
}

0 comments on commit 6c67eef

Please sign in to comment.