Skip to content

bug: Protected pages don't work on pages #458

@redimongo

Description

@redimongo

Describe the bug

When I use the following code in an API route it works and shows the user is Unauthorized prints our that it's isAuthorised:false

// pages/api/protected-resource.ts
import { logtoClient } from '../../../lib/logto';

export default logtoClient.withLogtoApiRoute(
  (request, response) => {
    console.log(request.user);
    if (!request.user.isAuthenticated) {
      response.status(401).json({ message: 'Unauthorized' });

      return;
    }

    // Access token can be obtained from request.user.accessToken

    response.json({
      data: 'this_is_protected_resource',
    });
  },
  { getAccessToken: true, resource: 'https://drn1.com.au' }
);

But when I do it in pages/staff for a client end view it prints our that it's isAuthorised:true

/pages/staff/protected.ts

import { LogtoContext } from '@logto/next';
import { logtoClient } from '../../lib/logto';

type Props = {
  user: LogtoContext;
};

const Profile = ({ user }: Props) => {
    return <div>{user?.claims?.sub}</div>;
};

export default Profile;

export const getServerSideProps = logtoClient.withLogtoSsr(
  async function ({ req }) {
    const { user } = req;

    // Access token can be obtained from user.accessToken
    console.log(JSON.stringify(user));
    return {
      props: { user },
    };
  },
  { getAccessToken: true, resource: 'https://drn1.com' }
);

Context

OS: Mac OS

Environment: Localhost (nextJS)

Logto version: 1.0.0

Node version: v16.15.1
npm version: v8.11.0

Screenshot 2023-03-28 at 2 17 13 pm

the user has no roles
Screenshot 2023-03-28 at 2 18 10 pm

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions