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

401 is returned on the page that should return 404 #186

Closed
so1ve opened this issue Dec 5, 2021 · 0 comments
Closed

401 is returned on the page that should return 404 #186

so1ve opened this issue Dec 5, 2021 · 0 comments

Comments

@so1ve
Copy link

so1ve commented Dec 5, 2021

Hi!

I don't know if there's something wrong with my code When I visit localhost:4000/404 (a path that doesn't exist), koa-jwt returns 401 error.

If I use the passthrough: true option, I can't verify my identity.

index.ts

  const app = new Koa(config)
  app.use(errorHandler)
  app.use(helmet())
  app.use(cors())
  app.use(body({
    multipart: true
  }))
  app.use(unprotectedRouter.routes())
    .use(unprotectedRouter.allowedMethods({
      throw: true
    }))
  app.use(jwt({
    secret: JWT_SECRET
  }))
  app.use(protectedRouter.routes())
    .use(protectedRouter.allowedMethods({
      throw: true
    }))

protectedRouter.ts

import Router from '@koa/router'

import { API_VERSION } from './lib'
import { posts } from './controller'

const protectedRouter = new Router({
  prefix: `/${API_VERSION}`
})

// Create post route
protectedRouter.post('/posts', posts.createPost)

// Delete post route
protectedRouter.delete('/posts/:id', posts.deletePost)

export { protectedRouter }

unprotectedRouter.ts

import Router from '@koa/router'

import { API_VERSION } from './lib'
import {
  general,
  posts,
  categories,
  tags,
  profile
} from './controller'

const unprotectedRouter = new Router({
  prefix: `/${API_VERSION}`
})

// Post list route
unprotectedRouter.get('/posts', posts.getPosts)

// Post detail route
unprotectedRouter.get('/posts/:id', posts.getPost)

// Categories route
unprotectedRouter.get('/categories', categories.getCategories)

// Category detail route
unprotectedRouter.get('/categories/:slug', categories.getCategory)

// Tags route
unprotectedRouter.get('/tags', tags.getTags)

// Tag detail route
unprotectedRouter.get('/tags/:slug', tags.getTag)

export { unprotectedRouter }
@so1ve so1ve closed this as completed Jul 3, 2022
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

1 participant