Giter Club home page Giter Club logo

Comments (4)

harrysolovay avatar harrysolovay commented on July 28, 2024

Thank you for the feature request / prospect of a PR! For clarity, can you please provide a snippet of example usage?

from amplify-ui.

alissaVrk avatar alissaVrk commented on July 28, 2024

the usage should be something like:

withAuthenticator(() => <Comp />, {
//maybe the predicate should be inside a config object, 
  to allow for easy addition of properties in the future 

authorizationPredicate: user => 
    user.signInUserSession.accessToken.payload["cognito:groups"].indexOf('myGroup') >= 0,

children: [
    <Unauthorized slot="unauthorized" message="you should upgrade to see this"/> //optional, will get the user in props
  ]
})

of course you can use this with the Authenticator component directly as well

from amplify-ui.

harrysolovay avatar harrysolovay commented on July 28, 2024

This kind of protection likely should not happen on the front end, unless you were to include a build step which serializes the predicate and sends it to a secure back-end resource for later use in safeguarding group data. This would be a cool experience, but definitely falls outside of Amplify's scope.

While there is not a front-end component visibility "rule" system surrounding withAuthentication, there are ways to achieve what you're describing. An isAuthenticatedUserMemberOfXGroup hook, for example:

function isAuthenticatedUserMemberOfGroup(groupName: string) {
  const [is, setIs] = useState<boolean | undefined>();
  // get `user`
  // see if the predicate rings true
  setIs(user.signInUserSession.accessToken.payload["cognito:groups"].indexOf(groupName) >= 0);
  return is;
}

function MyComp() {
  const isMemberOfG = isAuthenticatedUserMemberOfGroup("MyGroup");
  return isMemberOfG && (
    <div>Only members of "MyGroup" can see this</div>
  )
}

Bringing these checks into UI component props would likely result in a somewhat rigidly-structured API. It's my sense that this functionality would be best represented in case-by-case specificity.

@sammartinez @wlee221 –– what are your thoughts?

from amplify-ui.

alissaVrk avatar alissaVrk commented on July 28, 2024

sorry for disappearing.. and thanks for the detailed answer

I don't think there is a security issue here, since the withAuthentication only can block the view of a component. the actual content should be protected on the server side either way.

the code in the Authenticator takes the state it gets from server and according to it decides what to display, I'm just letting me/the user add some logic to that

all that I'm trying to solve here is the client code, which really doesn't care why you can't see something, it only wants to know

  1. should it display the content
  2. what should it display instead

with current implementation I have nested ifs each of them dealing with a different reason (authentication, authorization, anything else.. ), I thought that doing it all in one place could be nice.

I'm really not attached to the API I suggested, will be glad to hear other ideas :)

from amplify-ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.