0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

T3Stackのゲスト認証(簡易的)

Last updated at Posted at 2024-09-22

What

T3stackで簡易的にguest認証を実装しましたので、方法だけご説明します。

Code

npm create t3-app@latest

NextAuthを使うか聞かれるのでyesです。

.env
# supabaseで取得したリンクです。mode:sessionで取得してください。transactionは使えないです。
DATABASE_URL="supabaseのURL"

# Next Auth
# You can generate a new secret on the command line with:
# openssl rand -base64 32 ←これをコマンドに入力してランダムな文字を取得します。
# https://next-auth.js.org/configuration/options#secret

# 取得したランダムな文字をSECRETに使います。
NEXTAUTH_SECRET="openssl rand -base64 32で取得"
NEXTAUTH_JWT_SECRET="openssl rand -base64 32で取得"

NEXTAUTH_URL="http://localhost:3000"

# discordから取得します
DISCORD_CLIENT_ID="discordから"
DISCORD_CLIENT_SECRET="discordから"

参考にしました。

schema.prismaは初期状態です。

npx prisma generate
npx prisma migrate dev  
server/auth.ts
import { PrismaAdapter } from "@auth/prisma-adapter";
import {
  getServerSession,
  type DefaultSession,
  type NextAuthOptions,
} from "next-auth";
import { type Adapter } from "next-auth/adapters";
import CredentialsProvider from "next-auth/providers/credentials";

import { db } from "~/server/db";

declare module "next-auth" {
  interface Session extends DefaultSession {
    user: {
      id: string;
      name: string;
      email: string;
    } & DefaultSession["user"];
  }
}

export const authOptions: NextAuthOptions = {
  callbacks: {
    async jwt({ token, user }) {
      console.log("jwt callback", { token, user });

      //pass in user id to token
      if (user) {
        return {
          ...token,
          id: user.id,
        };
      }
      return token;
    },
    async session({ session, token, user }) {
      console.log("session callback", { session, token, user });

      //pass in user id to session
      return {
        ...session,
        user: {
          ...session.user,
          id: token.id,
        },
      };
    },
  },
  session: {
    strategy: "jwt",
  },
  jwt: {
    secret: process.env.NEXTAUTH_JWT_SECRET,
  },
  adapter: PrismaAdapter(db) as Adapter,
  providers: [
    CredentialsProvider({
      name: "Guest Account",
      credentials: {
        // username: {
        //   label: "Username",
        //   type: "text",
        //   placeholder: "空欄で構いません",
        // },
        // password: {
        //   label: "Password",
        //   type: "password",
        //   placeholder: "空欄で構いません",
        // },
      },
      async authorize() {
        const user = await db.user.findUnique({
          where: {
            email: "guest@example.com",
          },
        });
        if (user) {
          return user;
        }
        return null;
      },
    }),
  ],
  secret: process.env.NEXTAUTH_SECRET,
};

export const getServerAuthSession = () => getServerSession(authOptions);

あとは、supabase上で"guest@example.com"をレコードに追加しておけばゲストログイン可能です。

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?