Problem with using adonisjs middleware

How do i make user that are not logged in to go to log in page when trying to create a post?

What i did is created an Auth.js Middleware and i did this inside

'use strict'

const Response = require('@adonisjs/framework/src/Response')

/** @typedef {import('@adonisjs/framework/src/Request')} Request */
/** @typedef {import('@adonisjs/framework/src/Response')} Response */
/** @typedef {import('@adonisjs/framework/src/View')} View */

class Auth {
  /**
   * @param {object} ctx
   * @param {Request} ctx.request
   * @param {Function} next
   */
  async handle({ response, auth }, next) {
    // call next to advance the request
    try {
      await auth.check()
    } catch (error) {
      return response.route('login')
    }
    await next()
  }

  
}

module.exports = Auth

and i keep getting This page isn’t working127.0.0.1 redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

Route

Route.get('/posts/create','PostController.create').middleware('auth')

Kernel Js

const globalMiddleware = [
  'Adonis/Middleware/BodyParser',
  'Adonis/Middleware/Session',
  'Adonis/Middleware/Shield',
  'Adonis/Middleware/AuthInit',
  'Adonis/Middleware/Auth',
  'App/Middleware/ConvertEmptyStringsToNull',
]
const namedMiddleware = {
  auth: 'App/Middleware/Auth',
  guest: 'Adonis/Middleware/AllowGuestOnly'
}
1 Like

Hello @heolad

Does your login route have some middlewares?

Edit :
try remove auth middleware from GlobalMiddleware

1 Like

what you said at first to remove the Auth from GlobalMiddleware worked.

1 Like