accounts/stores: added useAccts
This commit is contained in:
parent
9f83b223c4
commit
8ae0768c42
2 changed files with 19 additions and 4 deletions
|
@ -1,6 +1,9 @@
|
|||
import { persistentAtom } from "@nanostores/persistent";
|
||||
import { useStore } from "@nanostores/solid";
|
||||
import { useNavigate } from "@solidjs/router";
|
||||
import { createOAuthAPIClient, createRestAPIClient } from "masto";
|
||||
import { action } from "nanostores";
|
||||
import { createRenderEffect } from "solid-js";
|
||||
|
||||
export type Account = {
|
||||
site: string;
|
||||
|
@ -25,7 +28,7 @@ interface OAuth2AccessToken {
|
|||
|
||||
async function oauth2TokenViaAuthCode(app: RegisteredApp, authCode: string) {
|
||||
const resp = await fetch(new URL("./oauth/token", app.site), {
|
||||
method: 'post',
|
||||
method: "post",
|
||||
body: JSON.stringify({
|
||||
grant_type: "authorization_code",
|
||||
code: authCode,
|
||||
|
@ -97,7 +100,7 @@ export const $registeredApps = persistentAtom<{
|
|||
|
||||
async function getAppAccessToken(app: RegisteredApp) {
|
||||
const resp = await fetch(new URL("./oauth/token", app.site), {
|
||||
method: 'post',
|
||||
method: "post",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
|
@ -172,3 +175,15 @@ export const getOrRegisterApp = action(
|
|||
return all[site];
|
||||
},
|
||||
);
|
||||
|
||||
export function useAccts() {
|
||||
const accts = useStore($accounts);
|
||||
const naviagte = useNavigate();
|
||||
|
||||
createRenderEffect(() => {
|
||||
if (accts().length > 0) return;
|
||||
naviagte("/accounts/sign-in");
|
||||
});
|
||||
|
||||
return accts;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue