Skip to content

Commit

Permalink
improve auth, improve queries
Browse files Browse the repository at this point in the history
  • Loading branch information
olexh committed Mar 23, 2024
1 parent 958abac commit 1d98c84
Show file tree
Hide file tree
Showing 128 changed files with 2,092 additions and 1,922 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/nextjs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ jobs:
# Use the 'appleboy/ssh-action' action for SSH deployment
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }} # server's IP address
username: ${{ secrets.USERNAME }} # server's username
key: ${{ secrets.SSH_PRIVATE_KEY }} # server's SSH private key
passphrase: ${{ secrets.SSH_PASSWORD }} # server's SSH key password
host: ${{ auths.HOST }} # server's IP address
username: ${{ auths.USERNAME }} # server's username
key: ${{ auths.SSH_PRIVATE_KEY }} # server's SSH private key
passphrase: ${{ auths.SSH_PASSWORD }} # server's SSH key password
script: |
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
Expand Down
2 changes: 1 addition & 1 deletion app/(api)/auth/activate/[token]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function GET(
) {
try {
const res = await activation({ token });
cookies().set('secret', res.secret);
cookies().set('auth', res.secret);
} catch (e) {
if ('code' in (e as API.Error)) {
if ((e as API.Error).code === 'auth-modal:activation_expired') {
Expand Down
2 changes: 1 addition & 1 deletion app/(api)/auth/google/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function GET(request: Request) {
provider: 'google',
});

cookies().set('secret', res.secret, {
cookies().set('auth', res.secret, {
maxAge: 60 * 60 * 24 * 7,
});
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion app/(api)/auth/logout/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cookies } from 'next/headers';

export async function GET() {
cookies().delete('secret');
cookies().delete('auth');

return Response.json({ result: true });
}
31 changes: 20 additions & 11 deletions app/(pages)/anime/(animeList)/_components/anime-list/anime-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@

import * as React from 'react';

import { useSearchParams } from 'next/navigation';
import { ReadonlyURLSearchParams } from 'next/navigation';

import EntryCard from '@/components/entry-card/entry-card';
import FiltersNotFound from '@/components/filters/_components/filters-not-found';
import { Button } from '@/components/ui/button';
import Pagination from '@/components/ui/pagination';
import useAnimeCatalog from '@/services/hooks/anime/useAnimeCatalog';
import { useAuthContext } from '@/services/providers/auth-provider';
import useAuth from '@/services/hooks/auth/useAuth';
import { useSettingsContext } from '@/services/providers/settings-provider';
import { MEDIA_TYPE } from '@/utils/constants';

import AnimeListSkeleton from './_components/anime-list-skeleton';
import { useNextPage, useUpdatePage } from './anime-list.hooks';
import EntryCard from '@/components/entry-card/entry-card';

interface Props {
searchParams: Record<string, string>;
}

const Component = () => {
const Component = ({ searchParams }: Props) => {
const { titleLanguage } = useSettingsContext();
const { secret } = useAuthContext();
const searchParams = useSearchParams();
const { auth } = useAuth();

const page = searchParams.get('page');
const iPage = searchParams.get('iPage');
const page = searchParams.page;
const iPage = searchParams.iPage;

const dataKeys = {
page: Number(page),
iPage: Number(iPage),
secret,
auth,
};

const {
Expand Down Expand Up @@ -67,13 +69,20 @@ const Component = () => {
}
key={anime.slug}
slug={anime.slug}
withContextMenu
content_type="anime"
leftSubtitle={anime.year ? String(anime.year) : undefined}
leftSubtitle={
anime.year ? String(anime.year) : undefined
}
rightSubtitle={
anime.media_type &&
MEDIA_TYPE[anime.media_type].title_ua
}
watch={anime.watch.length > 0 ? anime.watch[0] : undefined}
watch={
anime.watch.length > 0
? anime.watch[0]
: undefined
}
/>
);
})}
Expand Down
15 changes: 9 additions & 6 deletions app/(pages)/anime/(animeList)/_components/navbar/navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import clsx from 'clsx';
import { Suspense } from 'react';

import FiltersModal from '@/components/modals/anime-filters-modal';
import { cn } from '@/utils';

import Search from './_components/search';

interface Props {}
const Component = ({}: Props) => {

const Component = () => {
return (
<div
className={clsx(
'flex items-end gap-2 md:gap-4 border-b border-b-transparent bg-transparent transition',
className={cn(
'flex items-end gap-2 border-b border-b-transparent bg-transparent transition md:gap-4',
)}
>
<Search />
<Suspense>
<Search />
</Suspense>
<div className="lg:hidden">
<FiltersModal type="anime" />
</div>
Expand Down
3 changes: 1 addition & 2 deletions app/(pages)/anime/(animeList)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ReactNode } from 'react';

import Filters from '../../../../components/filters/anime-filters';

import NavBar from './_components/navbar';

interface Props {
Expand All @@ -24,4 +23,4 @@ const Component = async ({ children }: Props) => {
);
};

export default Component;
export default Component;
2 changes: 1 addition & 1 deletion app/(pages)/anime/(animeList)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Component = ({
);
}

return <List />;
return <List searchParams={searchParams} />;
};

export default Component;
7 changes: 5 additions & 2 deletions app/(pages)/anime/[slug]/(animeDetails)/comments/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Metadata, ResolvingMetadata } from 'next';

import { getCookie } from '@/app/actions';
import Comments from '@/components/comments/comments';

export async function generateMetadata(
Expand Down Expand Up @@ -30,7 +31,9 @@ interface Props {
}

const Component = async ({ params: { slug } }: Props) => {
return <Comments slug={slug} content_type="anime" />;
const auth = await getCookie('auth');

return <Comments auth={auth} slug={slug} content_type="anime" />;
};

export default Component;
export default Component;
4 changes: 2 additions & 2 deletions app/(pages)/anime/[slug]/_components/actions/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const Component = async ({ anime }: Props) => {
return null;
}

const secret = await getCookie('secret');
const auth = await getCookie('auth');

return (
<div className="flex flex-col gap-12">
<div className="flex flex-col gap-4">
<WatchListButton
disabled={!secret}
disabled={!auth}
additional
slug={String(anime.slug)}
/>
Expand Down
3 changes: 3 additions & 0 deletions app/(pages)/anime/[slug]/_components/characters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const Component = ({ extended }: Props) => {
>
{(extended ? main : main.slice(0, 5)).map((ch) => (
<EntryCard
slug={ch.character.slug}
withContextMenu
content_type="character"
key={ch.character.slug}
href={`/characters/${ch.character.slug}`}
poster={ch.character.image}
Expand Down
3 changes: 3 additions & 0 deletions app/(pages)/anime/[slug]/_components/franchise.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const Component = ({ extended }: Props) => {
const { list, fetchNextPage, hasNextPage, isFetchingNextPage, ref } =
useFranchise({ slug: String(params.slug) });

console.log(list);

if (!anime || !anime.has_franchise) {
return null;
}
Expand Down Expand Up @@ -55,6 +57,7 @@ const Component = ({ extended }: Props) => {
}
slug={anime.slug}
content_type="anime"
withContextMenu
href={`/anime/${anime.slug}`}
poster={anime.poster}
title={
Expand Down
3 changes: 3 additions & 0 deletions app/(pages)/anime/[slug]/_components/staff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const Component = ({ extended }: Props) => {
href={`/people/${staff.person.slug}`}
description={getRole(staff.roles)}
poster={staff.person.image}
slug={staff.person.slug}
content_type="person"
withContextMenu
title={
staff.person.name_ua ||
staff.person.name_en ||
Expand Down
9 changes: 5 additions & 4 deletions app/(pages)/anime/[slug]/_components/title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import H2 from '@/components/typography/h2';
import P from '@/components/typography/p';
import useAnimeInfo from '@/services/hooks/anime/useAnimeInfo';
import useIsMobile from '@/services/hooks/useIsMobile';
import { useAuthContext } from '@/services/providers/auth-provider';

import { useSettingsContext } from '@/services/providers/settings-provider';
import { ANIME_NAV_ROUTES } from '@/utils/constants';
import useAuth from '@/services/hooks/auth/useAuth';


const Component = () => {
const { titleLanguage } = useSettingsContext();
const isMobile = useIsMobile();
const pathname = usePathname();
const divRef = useRef<HTMLDivElement>(null);
const { secret } = useAuthContext();
const { auth } = useAuth();
const params = useParams();
const { data } = useAnimeInfo({ slug: String(params.slug) });

Expand Down Expand Up @@ -64,7 +65,7 @@ const Component = () => {
</span>
)}
</H2>
{secret && (
{auth && (
<EditButton
key={String(params.slug)}
slug={String(params.slug)}
Expand All @@ -85,7 +86,7 @@ const Component = () => {
<MaterialSymbolsStarRounded className="text-2xl" />
</div>
)}
{secret && (
{auth && (
<EditButton
key={String(params.slug)}
slug={String(params.slug)}
Expand Down
20 changes: 10 additions & 10 deletions app/(pages)/anime/[slug]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export async function generateMetadata(

const Component = async ({ params: { slug }, children }: Props) => {
const queryClient = getQueryClient();
const secret = await getCookie('secret');
const auth = await getCookie('auth');

const anime = await queryClient.fetchQuery({
queryKey: ['anime', slug],
Expand All @@ -99,33 +99,33 @@ const Component = async ({ params: { slug }, children }: Props) => {

await queryClient.prefetchInfiniteQuery({
queryKey: ['characters', slug],
queryFn: ({ pageParam }) =>
queryFn: ({ pageParam = 1 }) =>
getAnimeCharacters({ slug, page: pageParam }),
initialPageParam: 1,
});

await queryClient.prefetchInfiniteQuery({
queryKey: ['franchise', slug, { secret }],
queryFn: ({ pageParam }) =>
getAnimeFranchise({ slug, secret, page: pageParam }),
queryKey: ['franchise', slug, { auth }],
queryFn: ({ pageParam = 1 }) =>
getAnimeFranchise({ slug, auth, page: pageParam }),
initialPageParam: 1,
});

await queryClient.prefetchInfiniteQuery({
queryKey: ['staff', slug],
queryFn: ({ pageParam }) => getAnimeStaff({ slug, page: pageParam }),
queryFn: ({ pageParam = 1 }) => getAnimeStaff({ slug, page: pageParam }),
initialPageParam: 1,
});

await queryClient.prefetchQuery({
queryKey: ['watch', slug, { secret }],
queryFn: () => getWatch({ slug: slug, secret: String(secret) }),
queryKey: ['watch', slug, { auth }],
queryFn: () => getWatch({ slug: slug, auth: String(auth) }),
});

await queryClient.prefetchQuery({
queryKey: ['favorite', slug, { secret, content_type: 'anime' }],
queryKey: ['favorite', slug, { auth, content_type: 'anime' }],
queryFn: () =>
getFavourite({ slug: String(slug), secret: String(secret), content_type: 'anime' }),
getFavourite({ slug: String(slug), auth: String(auth), content_type: 'anime' }),
});

const dehydratedState = dehydrate(queryClient);
Expand Down
7 changes: 1 addition & 6 deletions app/(pages)/characters/[slug]/_components/anime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

import clsx from 'clsx';



import { useParams } from 'next/navigation';



import EntryCard from '@/components/entry-card/entry-card';
import SubHeader from '@/components/sub-header';
import { Button } from '@/components/ui/button';
import useCharacterAnime from '@/services/hooks/characters/useCharacterAnime';
import { useSettingsContext } from '@/services/providers/settings-provider';


interface Props {
extended?: boolean;
}
Expand Down Expand Up @@ -47,13 +42,13 @@ const Component = ({ extended }: Props) => {
key={ch.anime.slug}
href={`/anime/${ch.anime.slug}`}
poster={ch.anime.poster}
withContextMenu
title={
ch.anime[titleLanguage!] ||
ch.anime.title_ua ||
ch.anime.title_ua ||
ch.anime.title_ja
}
posterClassName="!h-[calc(100%+2rem)] absolute -top-1 left-0"
/>
))}
</div>
Expand Down
Loading

0 comments on commit 1d98c84

Please sign in to comment.