Open5

nextauthのsession検証

Takanori FukuyamaTakanori Fukuyama
    async signIn({ account, user }) {
      try {
        if (account.provider === "google") {
          const cred = GoogleAuthProvider.credential(account.id_token);
          const result = await signInWithCredential(auth, cred);
          const talent = await graphqlClient().findTalentOnAuth({
            provider: "google",
            providerAccountId: result.user.uid,
          });
          if (!talent?.talentByProvider) {
            // signUp
            const newTalent = await graphqlClient().signUpOnAuth({
              email: result.user.email,
              provider: "google",
              providerAccountId: result.user.uid,
            });
            user.id = newTalent.signUp;
            return true;
          }
          user.id = talent.talentByProvider.id;
          return true;
        }
        return false;
      } catch (e) {
        console.log("------------------------------");
        console.error(e);
        console.log("------------------------------");
        return false;
      }
    },
Takanori FukuyamaTakanori Fukuyama

成功したパターン
signIn → jwt → session
と渡すと実現できた

  callbacks: {
    async signIn({ account, user }) {
      try {
        if (account.provider === "google") {
          const cred = GoogleAuthProvider.credential(account.id_token);
          const result = await signInWithCredential(auth, cred);
          const talent = await graphqlClient().findTalentOnAuth({
            provider: "google",
            providerAccountId: result.user.uid,
          });
          if (!talent?.talentByProvider) {
            // signUp
            const newTalent = await graphqlClient().signUpOnAuth({
              email: result.user.email,
              provider: "google",
              providerAccountId: result.user.uid,
            });
            account.userId = newTalent.signUp;
            return true;
          }
          account.userId = talent.talentByProvider.id;
          return true;
        }
        return false;
      } catch (e) {
        console.error(e);
        return false;
      }
    },
    jwt: async ({ token, account }) => {
      if (account) {
        token["userId"] = account.userId;
      }
      return token;
    },
    session: async ({ session, user, token }) => {
      session["userId"] = token["userId"];
      return session;
    },
  },