Next.js Discord

Discord Forum

auth middleware?

Unanswered
Swedish Lapphund posted this in #help-forum
Open in Discord
Swedish LapphundOP
export function middleware(request) {
  // Call our authentication function to check the request
  if (!isAuthenticated(request)) {
    // Respond with JSON indicating an error message
    return Response.json(
      { success: false, message: 'authentication failed' },
      { status: 401 }
    )
  }
}


This has me confused. I tried to use middleware to protect my routes like /profile. But this ^ example from documentation sends the request to their auth checker. How are you supposed to check the auth on just a request? I feel so lost.

What I tried to do in my auth is this:
import ky from 'ky';

export default async function isAuthenticated(request) {
    try {
        const response = await ky.post('http://serverurl:3002/auth/checkauth', {
          credentials: 'include'
        });
      const json = await response.json();
      if(json.status === "success") {
          return true
      } else {
        return false
      }
      } catch (error) {
        console.log(error.message)
        return false
      }
}

But this throws an 404 error

0 Replies