back4App Parse and NextJS 13 login with next auth

18 views Asked by At

I am working on a simple app using NextJS 13 next-auth and the new app route. I want to add a backed like Back4app and use it for sync the user from auth with the user from B4A. The problem is when I initialize the Parse server with @parse/react-ssr I do this in layout.tsx I tried this:

import { initializeParse } from '@parse/react-ssr'

initializeParse(
  'https://parseapi.back4app.com/',
  process.env.BACK4APP_APPLICATION_ID as string,
  process.env.BACK4APP_JAVASCRIPT_KEY as string
)

Then in the [...nextauth]/route.ts I get the session from the user with google login (is the only allowed login) and inside the signIn callback, I call linkWith('google') for linking the user.

 async signIn({ account, profile }) {
      try {
        const { providerAccountId, id_token } = account || {}
        const { email } = profile || {}
        const userToLogin = new Parse.User()
        userToLogin.set('username', email)
        userToLogin.set('email', email)
        try {
          await userToLogin.linkWith('google', {
            authData: {
              id: providerAccountId,
              id_token,
            },
          })
        } catch (e) {
          console.log(`Error: ${(e as Error).message}`)
        }
        return true
      } catch (error) {
        console.error('Error syncing with Back4App:', error)
        return false
      }
    },

It seems to be working fine, but the problem is that I cannot access the user with await Parse.User.current() or something similar, how do I get the logged in user?

0

There are 0 answers