-3

I am trying to check if a particular string is not equal to example strings like this

const role = 'operations'
const perms = 'super-admin' || 'admin' || 'staff' || 'operations'
    if (role !== perms) {
        throw new UnauthorizedROLE();
    }

This how ever always throws the error but if I just do

if (role !== operations) {
        throw new UnauthorizedROLE();
    }

this one succeeds. How can I check with multiple like that and succeed

King
  • 1,885
  • 3
  • 27
  • 84

1 Answers1

0

If you check the type of perms, you'll see that it's just 'super-admin'. You can't create a unary string type like that in JavaScript. Because JavaScript evaluates right-to-left, you'll end up assigning the leftmost non-falsy value.

> const perms = 'super-admin' || 'admin' || 'staff' || 'operations'
'super-admin'

Try the following instead:

const role = 'operations'
const perms = ['super-admin', 'admin', 'staff', 'operations']
    if (!perms.includes(role)) {
        throw new UnauthorizedROLE();
    }
old greg
  • 799
  • 8
  • 19