|
1 |
| -# Remix Auth - Strategy Template |
| 1 | +# MicrosoftStrategy |
2 | 2 |
|
3 |
| -> A template for creating a new Remix Auth strategy. |
| 3 | +The Microsoft strategy is used to authenticate users against a [Microsoft Identity](https://docs.microsoft.com/en-us/azure/active-directory/develop/) account. This can be a work/school account or a personal Microsoft account, like Skype, Xbox and Outlook.com. It extends the OAuth2Strategy. |
4 | 4 |
|
5 |
| -If you want to create a new strategy for Remix Auth, you could use this as a template for your repository. |
| 5 | +## Supported runtimes |
6 | 6 |
|
7 |
| -The repo installs the latest version of Remix Auth and do the setup for you to have tests, linting and typechecking. |
| 7 | +| Runtime | Has Support | |
| 8 | +| ---------- | ----------- | |
| 9 | +| Node.js | ✅ | |
| 10 | +| Cloudflare | ✅ | |
8 | 11 |
|
9 |
| -## How to use it |
| 12 | +## How to use |
10 | 13 |
|
11 |
| -1. In the `package.json` change `name` to your strategy name, also add a description and ideally an author, repository and homepage keys. |
12 |
| -2. In `src/index.ts` change the `MyStrategy` for the strategy name you want to use. |
13 |
| -3. Implement the strategy flow inside the `authenticate` method. Use `this.success` and `this.failure` to correctly send finish the flow. |
14 |
| -4. In `tests/index.test.ts` change the tests to use your strategy and test it. Inside the tests you have access to `jest-fetch-mock` to mock any fetch you may need to do. |
15 |
| -5. Once you are ready, set the secrets on Github |
16 |
| - - `NPM_TOKEN`: The token for the npm registry |
17 |
| - - `GIT_USER_NAME`: The you want the bump workflow to use in the commit. |
18 |
| - - `GIT_USER_EMAIL`: The email you want the bump workflow to use in the commit. |
| 14 | +### Create an OAuth application |
19 | 15 |
|
20 |
| -## Scripts |
| 16 | +Follow the steps on [the Microsoft documentation](https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app) to create a new App Registration. You should select **Web** as the platform, configure a **Redirect URI** and add a client secret. |
21 | 17 |
|
22 |
| -- `build`: Build the project for production using the TypeScript compiler (strips the types). |
23 |
| -- `typecheck`: Check the project for type errors, this also happens in build but it's useful to do in development. |
24 |
| -- `lint`: Runs ESLint againt the source codebase to ensure it pass the linting rules. |
25 |
| -- `test`: Runs all the test using Jest. |
| 18 | + If you want to support login with both personal Microsoft accounts and school/work accounts, you might need to configure the supported account types by editing the manifest file. Set `signInAudience` value to `MicrosoftADandPersonalMicrosoftAccount` to allow login also with personal accounts. |
26 | 19 |
|
27 |
| -## Documentations |
| 20 | +Be sure to copy the client secret, Redirect URI and the Application (client) ID (under Overview) because you will need them later. |
28 | 21 |
|
29 |
| -To facilitae creating a documentation for your strategy, you can use the following Markdown |
| 22 | +### Create the strategy instance |
30 | 23 |
|
31 |
| -```markdown |
32 |
| -# Strategy Name |
| 24 | +```ts |
| 25 | +import { MicrosoftStrategy } from "remix-auth"; |
33 | 26 |
|
34 |
| -<!-- Description --> |
| 27 | +let microsoftStrategy = new MicrosoftStrategy( |
| 28 | + { |
| 29 | + clientID: "YOUR_CLIENT_ID", |
| 30 | + clientSecret: "YOUR_CLIENT_SECRET", |
| 31 | + callbackURL: "https://example.com/auth/microsoft/callback", |
| 32 | + }, |
| 33 | + async (accessToken, _, extraParams, profile) => { |
| 34 | + return User.findOrCreate({ email: profile.emails[0].value }); |
| 35 | + } |
| 36 | +); |
35 | 37 |
|
36 |
| -## Supported runtimes |
| 38 | +authenticator.use(microsoftStrategy); |
| 39 | +``` |
37 | 40 |
|
38 |
| -| Runtime | Has Support | |
39 |
| -| ---------- | ----------- | |
40 |
| -| Node.js | ✅ | |
41 |
| -| Cloudflare | ✅ | |
| 41 | +### Setup your routes |
| 42 | + |
| 43 | +```tsx |
| 44 | +// app/routes/login.tsx |
| 45 | +export default function Login() { |
| 46 | + return ( |
| 47 | + <Form action="/auth/microsoft" method="post"> |
| 48 | + <button>Login with Microsoft</button> |
| 49 | + </Form> |
| 50 | + ); |
| 51 | +} |
| 52 | +``` |
42 | 53 |
|
43 |
| -<!-- If it doesn't support one runtime, explain here why --> |
| 54 | +```tsx |
| 55 | +// app/routes/auth/microsoft.tsx |
| 56 | +import { ActionFunction, LoaderFunction } from "remix"; |
| 57 | +import { authenticator } from "~/auth.server"; |
44 | 58 |
|
45 |
| -## How to use |
| 59 | +export let loader: LoaderFunction = () => redirect("/login"); |
| 60 | + |
| 61 | +export let action: ActionFunction = ({ request }) => { |
| 62 | + return authenticator.authenticate("microsoft", request); |
| 63 | +}; |
| 64 | +``` |
| 65 | + |
| 66 | +```tsx |
| 67 | +// app/routes/auth/microsoft/callback.tsx |
| 68 | +import { ActionFunction, LoaderFunction } from "remix"; |
| 69 | +import { authenticator } from "~/auth.server"; |
| 70 | + |
| 71 | +export let loader: LoaderFunction = ({ request }) => { |
| 72 | + return authenticator.authenticate("microsoft", request, { |
| 73 | + successRedirect: "/dashboard", |
| 74 | + failureRedirect: "/login", |
| 75 | + }); |
| 76 | +}; |
| 77 | +``` |
| 78 | + |
| 79 | +### Allow login only with accounts from a single organization (tenant) |
| 80 | + |
| 81 | +If you want to allow login only for users from a single organization, you should add the `tenant` attribute to the configuration passed to `MicrosoftStrategy`. The value of `tenant` should be the **Directory (tenant) ID** found under **Overview** in your App Registration page. |
| 82 | + |
| 83 | +You must also select **Accounts in this organizational directory** as Supported account types in your App Registration. |
46 | 84 |
|
47 |
| -<!-- Explain how to use the strategy, here you should tell what options it expects from the developer when instantiating the strategy --> |
| 85 | +```ts |
| 86 | +let microsoftStrategy = new MicrosoftStrategy( |
| 87 | + { |
| 88 | + clientID: "YOUR_CLIENT_ID", |
| 89 | + clientSecret: "YOUR_CLIENT_SECRET", |
| 90 | + callbackURL: "https://example.com/auth/microsoft/callback", |
| 91 | + tenant: "YOUR_TENANT_ID", |
| 92 | + }, |
| 93 | + async (accessToken, _, extraParams, profile) => { |
| 94 | + return User.findOrCreate({ email: profile.emails[0].value }); |
| 95 | + } |
| 96 | +); |
48 | 97 | ```
|
0 commit comments