mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 04:04:21 -04:00
Add login indicator
This commit is contained in:
parent
d67bd62393
commit
fdc6a88317
@ -56,8 +56,9 @@ export const loginFunc = async (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getToken = async (): Promise<string | null> => {
|
export const getToken = async (cookies?: string): Promise<string | null> => {
|
||||||
const tokenStr = await getSecureItem("auth");
|
// @ts-ignore Web only.
|
||||||
|
const tokenStr = await getSecureItem("auth", cookies);
|
||||||
if (!tokenStr) return null;
|
if (!tokenStr) return null;
|
||||||
const token = JSON.parse(tokenStr) as Token;
|
const token = JSON.parse(tokenStr) as Token;
|
||||||
|
|
||||||
|
@ -32,7 +32,6 @@ import { KyooErrors } from "./kyoo-errors";
|
|||||||
import { Page, Paged } from "./page";
|
import { Page, Paged } from "./page";
|
||||||
import { Platform } from "react-native";
|
import { Platform } from "react-native";
|
||||||
import { getToken } from "./login";
|
import { getToken } from "./login";
|
||||||
import { getSecureItem } from "./secure-store.web";
|
|
||||||
|
|
||||||
export const kyooUrl =
|
export const kyooUrl =
|
||||||
Platform.OS !== "web"
|
Platform.OS !== "web"
|
||||||
@ -195,7 +194,7 @@ export const fetchQuery = async (queries: QueryIdentifier[], cookies?: string) =
|
|||||||
// we can't put this check in a function because we want build time optimizations
|
// we can't put this check in a function because we want build time optimizations
|
||||||
// see https://github.com/vercel/next.js/issues/5354 for details
|
// see https://github.com/vercel/next.js/issues/5354 for details
|
||||||
if (typeof window !== "undefined") return {};
|
if (typeof window !== "undefined") return {};
|
||||||
const authToken = getSecureItem("auth", cookies);
|
const authToken = await getToken(cookies);
|
||||||
|
|
||||||
const client = createQueryClient();
|
const client = createQueryClient();
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
|
@ -29,3 +29,4 @@ export * from "./studio";
|
|||||||
export * from "./episode";
|
export * from "./episode";
|
||||||
export * from "./season";
|
export * from "./season";
|
||||||
export * from "./watch-item";
|
export * from "./watch-item";
|
||||||
|
export * from "./user";
|
||||||
|
42
front/packages/models/src/resources/user.ts
Normal file
42
front/packages/models/src/resources/user.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Kyoo - A portable and vast media library solution.
|
||||||
|
* Copyright (c) Kyoo.
|
||||||
|
*
|
||||||
|
* See AUTHORS.md and LICENSE file in the project root for full license information.
|
||||||
|
*
|
||||||
|
* Kyoo is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* Kyoo is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { z } from "zod";
|
||||||
|
import { ResourceP } from "../traits/resource";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The library that will contain Shows, Collections...
|
||||||
|
*/
|
||||||
|
export const UserP = ResourceP.extend({
|
||||||
|
/**
|
||||||
|
* The name of this user.
|
||||||
|
*/
|
||||||
|
username: z.string(),
|
||||||
|
/**
|
||||||
|
* The user email address.
|
||||||
|
*/
|
||||||
|
email: z.string(),
|
||||||
|
/**
|
||||||
|
* The list of permissions of the user. The format of this is implementation dependent.
|
||||||
|
*/
|
||||||
|
permissions: z.array(z.string()),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type User = z.infer<typeof UserP>;
|
@ -21,14 +21,13 @@
|
|||||||
export const setSecureItem = async (key: string, value: string): Promise<null> => {
|
export const setSecureItem = async (key: string, value: string): Promise<null> => {
|
||||||
const d = new Date();
|
const d = new Date();
|
||||||
// A year
|
// A year
|
||||||
d.setTime(d.getTime() + 356 * 24 * 60 * 60 * 1000);
|
d.setTime(d.getTime() + 365 * 24 * 60 * 60 * 1000);
|
||||||
const expires = "expires=" + d.toUTCString();
|
const expires = "expires=" + d.toUTCString();
|
||||||
document.cookie = key + "=" + value + ";" + expires + ";path=/";
|
document.cookie = key + "=" + value + ";" + expires + ";path=/";
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getSecureItem = async (key: string, cookies?: string): Promise<string | null> => {
|
export const getSecureItem = async (key: string, cookies?: string): Promise<string | null> => {
|
||||||
if (typeof window === "undefined") return null;
|
|
||||||
const name = key + "=";
|
const name = key + "=";
|
||||||
const decodedCookie = decodeURIComponent(cookies ?? document.cookie);
|
const decodedCookie = decodeURIComponent(cookies ?? document.cookie);
|
||||||
const ca = decodedCookie.split(";");
|
const ca = decodedCookie.split(";");
|
||||||
|
@ -24,12 +24,14 @@ import { useYoshiki, px, Stylable } from "yoshiki/native";
|
|||||||
import { Icon } from "./icons";
|
import { Icon } from "./icons";
|
||||||
import AccountCircle from "@material-symbols/svg-400/rounded/account_circle-fill.svg";
|
import AccountCircle from "@material-symbols/svg-400/rounded/account_circle-fill.svg";
|
||||||
import { YoshikiStyle } from "yoshiki/dist/type";
|
import { YoshikiStyle } from "yoshiki/dist/type";
|
||||||
|
import { P } from "@expo/html-elements";
|
||||||
|
|
||||||
export const Avatar = ({
|
export const Avatar = ({
|
||||||
src,
|
src,
|
||||||
alt,
|
alt,
|
||||||
size = px(24),
|
size = px(24),
|
||||||
color,
|
color,
|
||||||
|
placeholder,
|
||||||
isLoading = false,
|
isLoading = false,
|
||||||
fill = false,
|
fill = false,
|
||||||
...props
|
...props
|
||||||
@ -37,6 +39,7 @@ export const Avatar = ({
|
|||||||
src?: string | null;
|
src?: string | null;
|
||||||
alt?: string;
|
alt?: string;
|
||||||
size?: YoshikiStyle<number | string>;
|
size?: YoshikiStyle<number | string>;
|
||||||
|
placeholder?: string;
|
||||||
color?: string;
|
color?: string;
|
||||||
isLoading?: boolean;
|
isLoading?: boolean;
|
||||||
fill?: boolean;
|
fill?: boolean;
|
||||||
@ -53,12 +56,21 @@ export const Avatar = ({
|
|||||||
fill && {
|
fill && {
|
||||||
bg: col,
|
bg: col,
|
||||||
},
|
},
|
||||||
|
placeholder &&
|
||||||
|
!src &&
|
||||||
|
!isLoading && {
|
||||||
|
bg: theme.accent,
|
||||||
|
justifyContent: "center",
|
||||||
|
alignItems: "center",
|
||||||
|
},
|
||||||
],
|
],
|
||||||
props,
|
props,
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
{src || isLoading ? (
|
{src || isLoading ? (
|
||||||
<Image src={src} alt={alt} layout={{ width: size, height: size }} />
|
<Image src={src} alt={alt} layout={{ width: size, height: size }} />
|
||||||
|
) : placeholder ? (
|
||||||
|
<P {...css({})}>{placeholder[0]}</P>
|
||||||
) : (
|
) : (
|
||||||
<Icon icon={AccountCircle} size={size} color={fill ? col : theme.colors.white} />
|
<Icon icon={AccountCircle} size={size} color={fill ? col : theme.colors.white} />
|
||||||
)}
|
)}
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { ReactElement } from "react";
|
import { ReactElement } from "react";
|
||||||
import { Navbar } from "./navbar";
|
import { MeQuery, Navbar } from "./navbar";
|
||||||
import { useYoshiki } from "yoshiki/native";
|
import { useYoshiki } from "yoshiki/native";
|
||||||
import { Main } from "@kyoo/primitives";
|
import { Main } from "@kyoo/primitives";
|
||||||
|
|
||||||
@ -51,4 +51,4 @@ export const DefaultLayout = ({ page, transparent }: { page: ReactElement, trans
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
DefaultLayout.getFetchUrls = () => [Navbar.query()];
|
DefaultLayout.getFetchUrls = () => [Navbar.query(), MeQuery];
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Library, LibraryP, Page, Paged, QueryIdentifier } from "@kyoo/models";
|
import { Library, LibraryP, Page, Paged, QueryIdentifier, User, UserP } from "@kyoo/models";
|
||||||
import {
|
import {
|
||||||
Input,
|
Input,
|
||||||
IconButton,
|
IconButton,
|
||||||
@ -82,18 +82,33 @@ const SearchBar = forwardRef<
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const MeQuery: QueryIdentifier<User> = {
|
||||||
|
path: ["auth", "me"],
|
||||||
|
parser: UserP,
|
||||||
|
};
|
||||||
|
|
||||||
export const NavbarProfile = () => {
|
export const NavbarProfile = () => {
|
||||||
const { css, theme } = useYoshiki();
|
const { css, theme } = useYoshiki();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
// TODO: show logged in user.
|
||||||
return (
|
return (
|
||||||
<Link
|
<Fetch query={MeQuery}>
|
||||||
href="/login"
|
{({ username }) => (
|
||||||
{...tooltip(t("navbar.login"))}
|
<Link
|
||||||
{...css({ marginLeft: ts(1), justifyContent: "center" })}
|
href="/login"
|
||||||
>
|
{...tooltip(username ?? t("navbar.login"))}
|
||||||
<Avatar alt={t("navbar.login")} size={30} color={theme.colors.white} />
|
{...css({ marginLeft: ts(1), justifyContent: "center" })}
|
||||||
</Link>
|
>
|
||||||
|
<Avatar
|
||||||
|
placeholder={username}
|
||||||
|
alt={t("navbar.login")}
|
||||||
|
size={30}
|
||||||
|
color={theme.colors.white}
|
||||||
|
/>
|
||||||
|
</Link>
|
||||||
|
)}
|
||||||
|
</Fetch>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
export const NavbarRight = () => {
|
export const NavbarRight = () => {
|
||||||
@ -125,9 +140,9 @@ export const NavbarRight = () => {
|
|||||||
onPress={
|
onPress={
|
||||||
Platform.OS === "web"
|
Platform.OS === "web"
|
||||||
? () => {
|
? () => {
|
||||||
setSearch(true);
|
setSearch(true);
|
||||||
setTimeout(() => ref.current?.focus(), 0);
|
setTimeout(() => ref.current?.focus(), 0);
|
||||||
}
|
}
|
||||||
: () => push("/search")
|
: () => push("/search")
|
||||||
}
|
}
|
||||||
{...tooltip(t("navbar.search"))}
|
{...tooltip(t("navbar.search"))}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user