Skip to content

Commit 7b164df

Browse files
chore: update readme
1 parent 8494193 commit 7b164df

File tree

2 files changed

+81
-37
lines changed

2 files changed

+81
-37
lines changed

README.md

Lines changed: 80 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,97 @@
1-
# Remix Auth - Strategy Template
1+
# MicrosoftStrategy
22

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.
44

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
66

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 ||
811

9-
## How to use it
12+
## How to use
1013

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
1915

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.
2117

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.
2619

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.
2821

29-
To facilitae creating a documentation for your strategy, you can use the following Markdown
22+
### Create the strategy instance
3023

31-
```markdown
32-
# Strategy Name
24+
```ts
25+
import { MicrosoftStrategy } from "remix-auth";
3326

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+
);
3537

36-
## Supported runtimes
38+
authenticator.use(microsoftStrategy);
39+
```
3740

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+
```
4253

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";
4458

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.
4684

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+
);
4897
```

src/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import { SessionStorage } from "@remix-run/server-runtime";
2-
import {
3-
AuthenticateOptions,
4-
Strategy,
5-
StrategyVerifyCallback,
6-
} from "remix-auth";
1+
import { StrategyVerifyCallback } from "remix-auth";
72

83
import {
94
OAuth2Profile,

0 commit comments

Comments
 (0)