Create MultiAccount type and utilities for managing multiple accounts

Signed-off-by: Marquis Kurt <software@marquiskurt.net>
This commit is contained in:
Marquis Kurt 2019-10-02 16:46:56 -04:00
parent aad0df1bac
commit e52ae2e332
No known key found for this signature in database
GPG Key ID: 725636D259F5402D
3 changed files with 97 additions and 14 deletions

View File

@ -156,16 +156,16 @@ export const styles = (theme: Theme) =>
pageProfileNameEmoji: {
minHeight: theme.typography.h4.fontSize,
fontWeight: theme.typography.fontWeightMedium,
'& img': {
height: theme.typography.h4.fontSize,
"& img": {
height: theme.typography.h4.fontSize
}
},
pageProfileBioEmoji: {
height: '0.875rem',
'& img': {
height: '0.875rem',
height: "0.875rem",
"& img": {
height: "0.875rem",
paddingLeft: 4,
paddingRight: 4,
paddingRight: 4
}
},
pageProfileStatsDiv: {

View File

@ -26,9 +26,32 @@ export type Account = {
bot: boolean | null;
};
/**
* Watered-down type for Mastodon accounts
*/
export type UAccount = {
id: string;
acct: string;
display_name: string;
avatar_static: string;
};
/**
* Account type for use with multi-account support
*/
export type MultiAccount = {
/**
* The host name of the account (ex.: mastodon.social)
*/
host: string;
/**
* The username of the account (@test)
*/
username: string;
/**
* The access token generated from the login
*/
access_token: string;
};

View File

@ -1,14 +1,10 @@
import Mastodon from "megalodon";
import { MultiAccount } from "../types/Account";
export function userLoggedIn(): boolean {
if (
localStorage.getItem("baseurl") &&
localStorage.getItem("access_token")
) {
return true;
} else {
return false;
}
return !!(
localStorage.getItem("baseurl") && localStorage.getItem("access_token")
);
}
export function refreshUserAccountData() {
@ -31,3 +27,67 @@ export function refreshUserAccountData() {
);
});
}
/**
* Set the access token and base URL to a given multi-account user.
* @param account The multi-account from localStorage to use
*/
export function loginWithAccount(account: MultiAccount) {
if (localStorage.getItem("access_token") !== null) {
console.info(
"Existing login detected. Removing and using assigned token..."
);
}
localStorage.setItem("access_token", account.access_token);
localStorage.setItem("baseurl", account.host);
}
/**
* Gets the account registry.
* @returns A list of accounts
*/
function getAccountRegistry(): MultiAccount[] {
let accountRegistry: MultiAccount[] = [];
let accountRegistryString = localStorage.getItem("registry");
if (accountRegistryString !== null) {
accountRegistry = JSON.parse(accountRegistryString);
}
return accountRegistry;
}
/**
* Add an account to the multi-account registry if it doesn't exist already.
* @param base_url The base URL of the user (eg., the instance)
* @param access_token The access token for the user
* @param username The username of the user
*/
export function addAccountToRegistry(
base_url: string,
access_token: string,
username: string
) {
const newAccount: MultiAccount = {
host: base_url,
username,
access_token
};
let accountRegistry = getAccountRegistry();
if (!accountRegistry.includes(newAccount)) {
accountRegistry.push(newAccount);
}
localStorage.setItem("registry", JSON.stringify(accountRegistry));
}
export function removeAccountFromRegistry(index: number) {
let accountRegistry = getAccountRegistry();
if (accountRegistry.length > index) {
accountRegistry.splice(index);
} else {
console.warn("Index of multi-account registry may be out of range.");
}
localStorage.setItem("registry", JSON.stringify(accountRegistry));
}