Skip to content

Commit

Permalink
MVP-119: Refactor CombinedSSOUser type (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyMitch authored Mar 18, 2024
2 parents 57420b4 + b9ac502 commit 792b5b5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const callTest = async () => {
return await response.json();
};
```
User state can be accessed through `user` from `useSSO()` hook, or `state?.userInfo` from `state` of `useSSO()` hook. It is preferred that you use `user` state as it is a normalized object that combines properties of users from different identity providers into a single user object.
User state can be accessed through `user` from the `useSSO()` hook. The `user` object is a normalized object that combines properties of users from different identity providers into a single user object.
Example `user` object from `useSSO()` hook (Typescript Type is `SSOUser`):
Expand All @@ -193,14 +193,15 @@ Example `user` object from `useSSO()` hook (Typescript Type is `SSOUser`):
"last_name": "Doe",
"client_roles": ["Admin"],
"scope": "openid idir email profile azureidir",
"identity_provider": "idir"
"identity_provider": "idir",
"originalData": { /* ... (original user data from sso) */ }
}
```
For all user properties of `state?.userInfo` which is of type `CombinedSSOUser`, reference [SSO Keycloak Wiki - Identity Provider Attribute Mapping].
For all user properties of `user.originalData` which is of type `OriginalSSOUser`, reference [SSO Keycloak Wiki - Identity Provider Attribute Mapping].
> [!Note*]
> _'client_roles' when used in `state?.userInfo` can be `undefined`. When checking if a user has a role, it is advised to use the `hasRole()` function from `useSSO()` or using the `user` object._
> _When checking if a user has a role, it is advised to use the `hasRole()` function from `useSSO()`._
<br />
Expand Down Expand Up @@ -492,7 +493,7 @@ import {
AuthService, // Type for useSSO().
AuthState, // Type for state of useSSO().
SSOUser, // Normalized user info for all identity providers.
CombinedSSOUser, // All user info from SSO.
OriginalSSOUser, // All user info from SSO.
SSOIdirUser, // User types specific to Idir users.
SSOBCeIDUser, // User types specific to BCeID users.
SSOGithubUser, // User types specific to Github users.
Expand Down Expand Up @@ -583,7 +584,7 @@ export type AuthAction = {
payload?: {
accessToken?: string;
idToken?: string;
userInfo?: CombinedSSOUser;
userInfo?: OriginalSSOUser;
};
};
Expand All @@ -592,7 +593,7 @@ export type AuthState = {
isAuthenticated: boolean;
accessToken?: string;
idToken?: string;
userInfo?: CombinedSSOUser;
userInfo?: OriginalSSOUser;
};
export type AuthStateWithDispatch = {
Expand Down Expand Up @@ -636,7 +637,7 @@ export type SSOGithubUser = {
last_name?: string;
};
export type CombinedSSOUser = BaseSSOUser &
export type OriginalSSOUser = BaseSSOUser &
SSOIdirUser &
SSOBCeIDUser &
SSOGithubUser;
Expand All @@ -646,6 +647,7 @@ export type SSOUser = BaseSSOUser & {
username: string;
first_name: string;
last_name: string;
originalData: OriginalSSOUser;
};
```

Expand Down
7 changes: 4 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type AuthAction = {
payload?: {
accessToken?: string;
idToken?: string;
userInfo?: CombinedSSOUser;
userInfo?: OriginalSSOUser;
};
};

Expand All @@ -62,7 +62,7 @@ export type AuthState = {
isAuthenticated: boolean;
accessToken?: string;
idToken?: string;
userInfo?: CombinedSSOUser;
userInfo?: OriginalSSOUser;
};

export type AuthStateWithDispatch = {
Expand Down Expand Up @@ -103,11 +103,12 @@ export type SSOGithubUser = {
last_name?: string;
};

export type CombinedSSOUser = BaseSSOUser & SSOIdirUser & SSOBCeIDUser & SSOGithubUser;
export type OriginalSSOUser = BaseSSOUser & SSOIdirUser & SSOBCeIDUser & SSOGithubUser;

export type SSOUser = BaseSSOUser & {
guid: string;
username: string;
first_name: string;
last_name: string;
originalData: OriginalSSOUser;
};
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CombinedSSOUser, SSOUser } from './types';
import { OriginalSSOUser, SSOUser } from './types';

/**
* Decodes a JSON Web Token (JWT) and returns the payload object.
Expand Down Expand Up @@ -27,7 +27,7 @@ export const hasAtLeastOneRole = (userRoles: string[], requiredRoles: string[])
requiredRoles.some((role) => userRoles.includes(role));

// Combine properties of each user type into a single object
export const normalizeUser = (userInfo: CombinedSSOUser): SSOUser => {
export const normalizeUser = (userInfo: OriginalSSOUser): SSOUser => {
const {
name = '',
preferred_username,
Expand Down Expand Up @@ -75,6 +75,7 @@ export const normalizeUser = (userInfo: CombinedSSOUser): SSOUser => {
client_roles,
scope,
identity_provider,
originalData: userInfo,
};

return user;
Expand Down

0 comments on commit 792b5b5

Please sign in to comment.