Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erro ao instanciar o useAeria em algum arquivo server side ! #34

Open
MarcoTDeveloper opened this issue Apr 18, 2024 · 0 comments
Open

Comments

@MarcoTDeveloper
Copy link

Criando uma simples função de validação utilizando os métodos do useAeria e instanciando ela dentro de algum arquivo server-side, o Next irá retornar o seguinte error:

Uma imagem do error:

image

Código do error:

./node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>

Código da função que causa o error:

"use server"

import { useAeria as Aeria, isLeft } from 'aeria';

export const verifyAeriaToken = async (token: string) => {
  if (!token) {
    return false
  }
  
  const aeria = await Aeria();
    
  const isValidated = await aeria.collections.user.functions.authenticate({ revalidate: true, token: { type: 'bearer', content: token } })

  if(isLeft(isValidated)) {
    return false
  }

  return true
}

Código do arquivo server-side que ela esta sendo instanciada:

src/middleware.ts

import { NextResponse, NextRequest } from 'next/server'
import { verifyAeriaToken } from './actions/verify-aeria-token'
export async function middleware(request: NextRequest) {
  const path = request.nextUrl.pathname
  const token = request.cookies.get('@aeria-auth')?.value || ''

  const authenticated = token ? await verifyAeriaToken(token) : false

  const isPublicPath =
    path === '/applicant' ||
    path === '/applicant/sign-in' ||
    path === '/applicant/sign-up'

  const isPrivatePath =
    path === "/applicant/dashboard" ||
    path === "/applicant/profile" ||
    path === "/applicant/modules" ||
    path === "/applicant/shop" ||
    path === "/applicant/exams" ||
    path === "/applicant/create-exam" ||
    path === "/applicant/finances"

  if (!authenticated && isPrivatePath) {
    return NextResponse.redirect(new URL('/applicant/sign-in', request.nextUrl))
  }

  if (isPublicPath && authenticated) {
    return NextResponse.redirect(new URL('/applicant/dashboard', request.nextUrl))
  }

  if (!isPublicPath && !authenticated) {
    return NextResponse.redirect(new URL('/applicant/sign-in', request.nextUrl))
  }

}

export const config = {
  matcher: [
    '/applicant/:path*'
  ],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants