Login page of "subscribe dialog", still WIP, but looking nice
This commit is contained in:
parent
1599793de2
commit
6d343c0f1a
8 changed files with 366 additions and 11 deletions
|
@ -23,7 +23,10 @@ class Api {
|
|||
async auth(baseUrl, topic, user) {
|
||||
const url = topicUrlAuth(baseUrl, topic);
|
||||
console.log(`[Api] Checking auth for ${url}`);
|
||||
const response = await fetch(url);
|
||||
const headers = this.maybeAddAuthorization({}, user);
|
||||
const response = await fetch(url, {
|
||||
headers: headers
|
||||
});
|
||||
if (response.status >= 200 && response.status <= 299) {
|
||||
return true;
|
||||
} else if (!user && response.status === 404) {
|
||||
|
@ -33,6 +36,14 @@ class Api {
|
|||
}
|
||||
throw new Error(`Unexpected server response ${response.status}`);
|
||||
}
|
||||
|
||||
maybeAddAuthorization(headers, user) {
|
||||
if (user) {
|
||||
const encoded = new Buffer(`${user.username}:${user.password}`).toString('base64');
|
||||
headers['Authorization'] = `Basic ${encoded}`;
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
}
|
||||
|
||||
const api = new Api();
|
||||
|
|
|
@ -4,11 +4,11 @@ import Subscriptions from "./Subscriptions";
|
|||
export class Repository {
|
||||
loadSubscriptions() {
|
||||
console.log(`[Repository] Loading subscriptions from localStorage`);
|
||||
|
||||
const subscriptions = new Subscriptions();
|
||||
const serialized = localStorage.getItem('subscriptions');
|
||||
if (serialized === null) return subscriptions;
|
||||
|
||||
if (serialized === null) {
|
||||
return subscriptions;
|
||||
}
|
||||
try {
|
||||
const serializedSubscriptions = JSON.parse(serialized);
|
||||
serializedSubscriptions.forEach(s => {
|
||||
|
@ -26,7 +26,6 @@ export class Repository {
|
|||
|
||||
saveSubscriptions(subscriptions) {
|
||||
console.log(`[Repository] Saving ${subscriptions.size()} subscription(s) to localStorage`);
|
||||
|
||||
const serialized = JSON.stringify(subscriptions.map( (id, subscription) => {
|
||||
return {
|
||||
baseUrl: subscription.baseUrl,
|
||||
|
@ -37,6 +36,30 @@ export class Repository {
|
|||
}));
|
||||
localStorage.setItem('subscriptions', serialized);
|
||||
}
|
||||
|
||||
loadUsers() {
|
||||
console.log(`[Repository] Loading users from localStorage`);
|
||||
const serialized = localStorage.getItem('users');
|
||||
if (serialized === null) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
return JSON.parse(serialized);
|
||||
} catch (e) {
|
||||
console.log(`[Repository] Unable to deserialize users: ${e.message}`);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
saveUser(baseUrl, username, password) {
|
||||
console.log(`[Repository] Saving users to localStorage`);
|
||||
const users = this.loadUsers();
|
||||
users[baseUrl] = {
|
||||
username: username,
|
||||
password: password
|
||||
};
|
||||
localStorage.setItem('users', users);
|
||||
}
|
||||
}
|
||||
|
||||
const repository = new Repository();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue