How do I redirect to another URL when using claimCheck?

I have the following code…

app.get(
    "/secure",
    oidc.claimCheck((req, claims) => {
        console.log(`The roles are ${JSON.stringify(claims)}`);
        return claims["https://secondave.net/roles"].includes("editor");
    }),
    (req, res)=>{
        // TODO: Add a new post
        res.send("Claim worked");
    }
)

But in this case I would like to redirect to a specific url (in this case root /) when the user is unauthorized. This way it just redirects back to Auth0 site

Hey @jackiegleason, claimCheck does not have access to the response middleware, so you cannot redirect from within that. As a workaround, you can manually check the claims and redirect similar to this:

app.get(
    "/secure",
    requiresAuth(),
    (req, res)=>{
        if (req.oidc.user["https://secondave.net/roles"].includes("editor")) {
            return res.redirect("/");
        }
        // TODO: Add a new post
        res.send("Claim worked");
    }
)
1 Like

Thanks for sharing that @thameera with the rest of community!