GithubHelp home page GithubHelp logo

bug with using credentials about fastify-uws HOT 7 OPEN

geut avatar geut commented on June 3, 2024
bug with using credentials

from fastify-uws.

Comments (7)

brokolosov avatar brokolosov commented on June 3, 2024 1

yes, sure... I'm using typescript

// server.ts

import { fastifyUws, serverFactory } from '@geut/fastify-uws'
import fastify from 'fastify'
import dotenv from 'dotenv'
import app from './app'

dotenv.config()

const port = process.env.PORT as unknown as number
const host = process.env.HOST as unknown as string

const server = fastify({
  serverFactory,
})

server.register(fastifyUws)
server.register(app)

server.listen({ port, host }, () => {})

// app.ts

import cors from '@fastify/cors'
import type { FastifyPluginAsync } from 'fastify'

const app: FastifyPluginAsync = async (fastify, opts): Promise<void> => {
  void fastify.register(cors, {
    credentials: true,
    origin: ['http://127.0.0.1:3333'],
    methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'],
  })

  void fastify.register(AutoLoad, {
    dir: join(__dirname, 'plugins'),
    options: opts,
  })

  void fastify.register(AutoLoad, {
    dir: join(__dirname, 'routes'),
    options: Object.assign({ prefix: `/api/${process.env.API_VERSION}` }, opts),
  })
}

// router file

import type { FastifyPluginAsync } from 'fastify'
import { isLoggedIn } from '../../middleware/isLoggedIn'

const chart: FastifyPluginAsync = async (fastify): Promise<void> => {
  fastify.post('/create', {
    preHandler: isLoggedIn,
  },
  async (request, reply) => {
    try {
      const { ...someData } = await request.body
      const chart = await fastify.prisma.charts.create({
        data: { ... someData},
      })
      await reply.code(201).send(chart.id)
    }
    catch (e) {
      await reply.code(500).send('uups...')
    }
  },
  )

//client

import axios from 'axios'

const apiClient = axios.create({
  baseURL,
  withCredentials: true,
})

from fastify-uws.

tinchoz49 avatar tinchoz49 commented on June 3, 2024

can you show me a code example?

from fastify-uws.

brokolosov avatar brokolosov commented on June 3, 2024

This request ('/create') is not executed at all, and there are no errors either, it just frozen. But if remove prehandler (on server) and credentials (on client for this particular request), then works fine.

from fastify-uws.

brokolosov avatar brokolosov commented on June 3, 2024

P.S. One more clarification. The issue occurs only if the data is transferred via "body". If transferring through "query" then all works correctly too

from fastify-uws.

tinchoz49 avatar tinchoz49 commented on June 3, 2024

thank you @brokolosov for all the information, I will look into it

from fastify-uws.

tinchoz49 avatar tinchoz49 commented on June 3, 2024

i check this by doing a test and i cannot reproduce the problem:

import { test } from 'uvu'
import * as assert from 'uvu/assert'
import Fastify from 'fastify'
import cors from '@fastify/cors'
import { serverFactory, fastifyUws } from '../src/index.js'

test('cookies', async () => {
  const app = Fastify({
    serverFactory
  })

  app.register(fastifyUws)

  app.register(cors, {
    credentials: true,
    origin: ['http://127.0.0.1:3333'],
    methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']
  })

  app.post('/', {
    preHandler: async () => {
      console.log('all good')
    }
  }, (req, res) => {
    console.log(req.body)
    return { ok: true }
  })

  await app.listen({ port: 3333 })

  const res = await fetch(`http://localhost:${app.server.address().port}`, {
    method: 'POST',
    credentials: 'include',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ hi: true })
  }).then(res => res.json())

  console.log(res)
  
  await app.close()
})

test.run()

from fastify-uws.

brokolosov avatar brokolosov commented on June 3, 2024

Thank you for your time. It turned out to be much more complicated. The conflict occurs with the "session.store" module. But so far, I have not figured out what exactly the bug is.

from fastify-uws.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.