-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement feature to verify role while log in
- Loading branch information
1 parent
09ba86b
commit f48294a
Showing
5 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Session from "supertokens-web-js/recipe/session"; | ||
import { UserRoleClaim } from "supertokens-web-js/recipe/userroles"; | ||
|
||
import logout from "./logout"; | ||
import useUserStore from "../store"; | ||
|
||
export async function verifySessionRoles(claims: string[]): Promise<boolean> { | ||
if (await Session.doesSessionExist()) { | ||
let errorCount = 0; | ||
|
||
const validationErrors = await Session.validateClaims({ | ||
overrideGlobalClaimValidators: (globalValidators) => { | ||
const validators = claims.map((claim) => | ||
UserRoleClaim.validators.includes(claim), | ||
); | ||
|
||
return [...globalValidators, ...validators]; | ||
}, | ||
}); | ||
|
||
if (validationErrors.length === 0) { | ||
return true; | ||
} | ||
|
||
for (const err of validationErrors) { | ||
if (err.validatorId === UserRoleClaim.id) { | ||
// user roles claim check failed | ||
errorCount += 1; | ||
} else { | ||
// some other claim check failed (from the global validators list) | ||
} | ||
} | ||
|
||
if (errorCount < claims.length) { | ||
// some user roles claim check passed | ||
return true; | ||
} else { | ||
const userStore = useUserStore(); | ||
|
||
const { removeUser } = userStore; | ||
// all user roles claim check failed | ||
removeUser(); | ||
await logout(); | ||
} | ||
} | ||
// either a session does not exist, or one of the validators failed. | ||
// so we do not allow access to this page. | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters