npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@animelist/auth-next

v0.1.1

Published

Integration with the @animelist/auth for NextJS

Downloads

51

Readme

@animelist/auth-next

Implementation of the @animelist/auth for NextJS.

You can checkout this Example.

Table of Contents

  1. Setup
  2. Get Current User
  3. Middleware
  4. Load user from server
  5. License

Setup

In your NextJS project install the packages:

npm

npm install @animelist/auth-next @animelist/client

yarn

yarn add @animelist/auth-next @animelist/client

pnpm

pnpm install @animelist/auth-next @animelist/client
  1. Create an .env.local with the environment variables:

    MAL_CLIENT_ID = <client_id>
    MAL_CLIENT_SECRET = <client_secret>
    MAL_REQUEST_DEBUG = true # optional

    To get the client id and client secret you need to log into your https://myanimelist.net/:

    • Go to Preferences > API and create a new client.
    • On the App Redirect URL use <url>/api/myanimelist/auth/callback.
      • For example http://localhost:3000/api/myanimelist/auth/callback if your app is running on localhost:3000.
  2. Create a component MyAnimeListAuthProvider to wrap the SessionProvider, this is required because the session provider cannot run as a server component.

"use client";

import { SessionProvider, type Session } from "@animelist/auth-next/client";

type MyAnimeListAuthProviderProps = {
  children: React.ReactNode;
  session?: Session | null;
};

// <SessionProvider> cannot be used as a server component, so we wrap it.
export function MyAnimeListAuthProvider({
  children,
  session,
}: MyAnimeListAuthProviderProps) {
  return <SessionProvider session={session}>{children}</SessionProvider>;
}
  1. Modify your root layout app/layout.tsx and add the provider
export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <MyAnimeListAuthProvider>
          {children}
        </MyAnimeListAuthProvider>
      </body>
    </html>
  );
}
  1. Add an app/api/[...myanimelist]/route.ts to handle all the MyAnimeList requests:
import { createMyAnimeListFetchHandler } from "@animelist/auth-next/server";

const handler = createMyAnimeListFetchHandler();

export { 
  handler as GET, 
  handler as POST, 
  handler as PATCH, 
  handler as DELETE 
};
  1. You are ready! In your app/page.tsx you can start using the api
"use client";
import { signIn, signOut, useSession } from "@animelist/auth-next/client";

export default function HomePage() {
  const { user, isLoading } = useSession();

  if (isLoading) {
    return <p>Loading...</p>;
  }

  return (
    <div>
      {user ? (
        <div>
          <p>{`Hello ${user.name}`}</p>
          <button onClick={signOut}>Sign Out</button>
        </div>
      ) : (
        <button onClick={signIn}>Sign In</button>
      )}
    </div>
  );
}

Finally, npm run dev make sure to run in the same port that your redirection url, otherwise you will receive an error when trying to login.

  1. The useSession also returns an accessToken that can use used with the client to make requests.
 // import { MALClient } from "@animelist/client";

 const { user, isLoading, accessToken } = useSession();

 useEffect(() => {
  if (!accessToken) {
    return;
  }

  // We need the 'proxyUrl' because we are running on the client
  const client = new MALClient({ 
    proxyUrl: "/api/myanimelist",
    accessToken, 
  });

  client.getSuggestedAnime()
    .then(console.log)
    .catch(console.error);
 }, [accessToken])

Get Current User

After the user is logged you can get the current user information using getServerSession.

Which returns null if the user is not logged or UserSession:

type UserSession = {
  userId: number;
  refreshToken: string;
  accessToken: string;
};
import { getServerSession } from "@animelist/auth-next/server";

const session = await getServerSession(cookies);

if (session) {
  console.log("User is logged in");
}

You can also use getRequiredServerSession(cookies) which throws an error if the user is not logged in.

If you want to get the user information you can use the getUser, keep in mind this fetches the user, instead of just retrieve the information from the cookie.

import { getUser } from "@animelist/auth-next/server";

const user = await getUser(cookies);

if (user) {
  console.log("User is logged in");
}

Middleware

Using the getServerSession you can define a middleware for redirect users.

import { getServerSession } from "@animelist/auth-next/server";
import { NextRequest, NextResponse } from "next/server";

export default function middleware(req: NextRequest) {
  const session = getServerSession(req.cookies);

  if (!session) {
    return NextResponse.redirect("/login");
  }

  return NextResponse.next();
}

Load user from server

Each time we load a page we will fetch the user from the client side, so you may need to show a spinner while the user is loading. To prevent this we can fetch the user from the server side.

Following our setup example we can do this:

import { cookies } from "next/headers";
import { getUser } from "@animelist/auth-next/server";

export default async function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {

  const session = await getUser(cookies());

  return (
    <html lang="en">
      <body>
        <MyAnimeListAuthProvider session={session}>
          {children}
        </MyAnimeListAuthProvider>
      </body>
    </html>
  );
}

Good to know

You may also notice you are receiving this warning:

⚠️ 'process.env.MAL_SECRET_KEY' was not set, using a default secret key

To fix that add other environment variable MAL_SECRET_KEY, to generate a secret key you can use:

openssl rand --base64 32

Or this beauty:

echo "console.log(require('crypto').randomBytes(32).toString('base64'))" | node

License

This project is licensed under the MIT License - see the LICENSE file for details.