Skip to content

Commit

Permalink
✨ Added login
Browse files Browse the repository at this point in the history
  • Loading branch information
wictorwilen committed Feb 10, 2022
1 parent 28dcaa0 commit 10609d2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,19 @@ The `TeamsSsoProvider` requires the following configuration properties:
| `scopes` | *Optional* The Scopes to use. Default to `[""]` |
| `redirectUri` | *Optional* The Redirect URI to be used for the AAD App |
| `useMgt` | *Optional* Boolean value indicating if the Microsoft Graph Toolkit auth provider should be initialized |
| `autoLogin` | *Optional* Boolean value indicating if the provider should automatically log in the user. Defaults to true. If set to false, then `login` of the `TeamsSsoProvider` context object has to be called. |

### State
### Context

The `TeamsSsoProvider` contains the following state variables
The `TeamsSsoProvider` contains the following context variables and methods

| Variable | Description |
| Variable/method | Description |
|-|-|
| `token` | The SSO/access token. `undefined` if not set |
| `name` | The user name. Defaults to `undefined` |
| `error` | Any error message. `undefined` if no errors |
| `logout()` | Signs the user out of the application |
| `login()` | Signs the user in to the application |

### Usage with the `TeamsSsoContext.Consumer`

Expand Down
8 changes: 8 additions & 0 deletions src/TeamsSsoProvider/TeamsSsoContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ export interface TeamsSsoContextProps {
*/
error?: string;

/**
* Logout method
*/
logout: () => void;

/**
* Login method
*/
login: () => void;
};

export const TeamsSsoContext = React.createContext<Partial<TeamsSsoContextProps>>({
Expand Down
31 changes: 26 additions & 5 deletions src/TeamsSsoProvider/TeamsSsoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const TeamsSsoProvider = (props: React.PropsWithChildren<TeamsSsoProvider

const [{ inTeams }] = useTeams();

useEffect(() => {
const login = React.useCallback((): void => {
if (inTeams === true) {

microsoftTeams.authentication.getAuthToken({
Expand All @@ -53,6 +53,7 @@ export const TeamsSsoProvider = (props: React.PropsWithChildren<TeamsSsoProvider
setName(account[0].name);
}
setError(undefined);
microsoftTeams.appInitialization.notifySuccess();
}).catch(err => {
if (err instanceof msal.InteractionRequiredAuthError) {
if (props.scopes && props.redirectUri && props.appId) {
Expand All @@ -66,6 +67,7 @@ export const TeamsSsoProvider = (props: React.PropsWithChildren<TeamsSsoProvider
setName(account[0].name);
}
setError(undefined);
microsoftTeams.appInitialization.notifySuccess();
}).catch(err => {
setError(err);
});
Expand All @@ -75,9 +77,17 @@ export const TeamsSsoProvider = (props: React.PropsWithChildren<TeamsSsoProvider
}
});
} else {
if (props.scopes && !props.appId) {
const message = "When scopes is specified an appId is also required";
setError(message);
microsoftTeams.appInitialization.notifyFailure({
reason: microsoftTeams.appInitialization.FailedReason.AuthFailed,
message
});
}
setToken(token);
microsoftTeams.appInitialization.notifySuccess();
}
microsoftTeams.appInitialization.notifySuccess();
},
failureCallback: (message: string) => {
setError(message);
Expand Down Expand Up @@ -121,16 +131,25 @@ export const TeamsSsoProvider = (props: React.PropsWithChildren<TeamsSsoProvider
}).catch(err => {
setError(err);
});
} else {
throw new Error("Missing redirectUri");
}
} else {
setError(err);
}
});

} else {
throw new Error("Missing scopes and/or appId");
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [inTeams]);
}, [inTeams, props.appId, props.appIdUri, props.scopes, props.redirectUri]);

useEffect(() => {
if (!props.autoLogin === false) {
login();
}
}, [login, props.autoLogin]);

useEffect(() => {
if (props.useMgt) {
Expand Down Expand Up @@ -163,10 +182,12 @@ export const TeamsSsoProvider = (props: React.PropsWithChildren<TeamsSsoProvider
setError(undefined);
setName(undefined);
msalInstance.logoutRedirect();
} else {
console.warn("Nothing to logout from, missing appId");
}
}, [props.appId]);

const memoedToken = React.useMemo(() => ({ token, name, error, logout }), [token, name, error, logout]);
const memoedToken = React.useMemo(() => ({ token, name, error, logout, login }), [token, name, error, logout, login]);

return (<TeamsSsoContext.Provider value={memoedToken} >
{props.children}
Expand Down
5 changes: 5 additions & 0 deletions src/TeamsSsoProvider/TeamsSsoProviderProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ export interface TeamsSsoProviderProps {
* Set to true to initialize Microsoft Graph Toolkit authorization provider
*/
useMgt?: boolean;

/**
* Automatically log in user, when outside of Teams. Defaults to true. If set to false the login method of TeamsSsoContext must be called manually
*/
autoLogin?: boolean;
}

0 comments on commit 10609d2

Please sign in to comment.