CoolFace
Apppublic

enzostvs/deepsite

sourceHugging Facemitupdated 8mo agoView on Hugging Face
17klikes
auth.ts71 linesDownload Raw Back to lib
1import type { NextAuthOptions } from "next-auth";2import { getServerSession } from "next-auth/next";3 4export const authOptions: NextAuthOptions = {5  providers: [6    {7      id: "huggingface",8      name: "Hugging Face",9      type: "oauth",10      clientId: process.env.AUTH_HUGGINGFACE_ID,11      clientSecret: process.env.AUTH_HUGGINGFACE_SECRET,12      wellKnown: "https://huggingface.co/.well-known/openid-configuration",13      authorization: {14        url: "https://huggingface.co/oauth/authorize",15        params: {16          scope: "openid profile read-repos manage-repos inference-api",17        },18      },19      userinfo: {20        url: "https://huggingface.co/oauth/userinfo",21        async request(context) {22          const { tokens } = context;23          const response = await fetch("https://huggingface.co/oauth/userinfo", {24            headers: {25              Authorization: `Bearer ${tokens.access_token}`,26            },27          });28          const data = await response.json();29          return data;30        }31      },32      checks: ["state"],33      async profile(profile) {34        return {35          id: profile.sub,36          name: profile.name || profile.preferred_username,37          username: profile.preferred_username,38          image: profile.picture,39          isPro: profile.isPro40        };41      },42    },43  ],44  callbacks: {45    async jwt({ token, account, user }) {46      if (account) {47        token.accessToken = account.access_token;48      }49      if (user) {50        token.username = user.username;51        token.isPro = user.isPro;52      }53      return token;54    },55    async session({ session, token }) {56      session.accessToken = token.accessToken;57      if (session.user) {58        session.user.username = token.username;59        session.user.isPro = token.isPro;60      }61      return session;62    },63  },64  pages: {65    signIn: "/signin",66  },67  secret: process.env.NEXTAUTH_SECRET,68};69 70export const auth = () => getServerSession(authOptions);71