Skip to content

Commit

Permalink
Merge pull request #37 from wKovacs64/remix-auth-4
Browse files Browse the repository at this point in the history
feat: target remix-auth v4 and remix-auth-oauth2 v3
  • Loading branch information
juhanakristian authored Jan 12, 2025
2 parents c3ecd49 + 334b40c commit 3130a6e
Show file tree
Hide file tree
Showing 14 changed files with 6,004 additions and 17,981 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint", "unicorn", "jest", "prettier"],
plugins: ["@typescript-eslint", "unicorn", "prettier"],
extends: [
"plugin:unicorn/recommended",
"plugin:@typescript-eslint/recommended",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/bump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '16'
node-version: '22'
cache: 'npm'
- name: Install dependencies
uses: bahmutov/npm-install@v1
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
- name: Checkout repo
uses: actions/checkout@v4

- name: Use Node 16
- name: Use Node 22
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 22

- name: Install dependencies
uses: bahmutov/npm-install@v1
Expand All @@ -28,10 +28,10 @@ jobs:
- name: Checkout repo
uses: actions/checkout@v4

- name: Use Node 16
- name: Use Node 22
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 22

- name: Install dependencies
uses: bahmutov/npm-install@v1
Expand All @@ -46,16 +46,16 @@ jobs:
- name: Checkout repo
uses: actions/checkout@v4

- name: Use Node 16
- name: Use Node 22
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 22

- name: Install dependencies
uses: bahmutov/npm-install@v1

- name: Test
run: npm run test -- --ci --coverage --maxWorkers=2
run: npm run coverage

lint:
name: Linter
Expand All @@ -64,10 +64,10 @@ jobs:
- name: Checkout repo
uses: actions/checkout@v4

- name: Use Node 16
- name: Use Node 22
uses: actions/setup-node@v4
with:
node-version: 16
node-version: 22

- name: Install dependencies
uses: bahmutov/npm-install@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 16
node-version: 22
registry-url: https://registry.npmjs.org/
- run: npm install
- run: npm run build
Expand Down
83 changes: 13 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# MicrosoftStrategy for [Remix](https://remix.run/) using [Remix-Auth](https://github.com/sergiodxa/remix-auth)

The Microsoft strategy is used to authenticate users against an account on [Microsoft Active Directory](https://docs.microsoft.com/en-us/azure/active-directory/develop/) using [Remix-Auth](https://github.com/sergiodxa/remix-auth).
This can be a work/school account or a personal Microsoft account, like Skype, Xbox and Outlook.com. It extends the OAuth2Strategy.
The Microsoft strategy is used to authenticate users against an account on [Microsoft Active Directory](https://docs.microsoft.com/en-us/azure/active-directory/develop/) using [Remix Auth](https://github.com/sergiodxa/remix-auth).
This can be a work/school account or a personal Microsoft account, like Skype, Xbox and Outlook.com. It extends the [remix-auth-oauth2](https://github.com/sergiodxa/remix-auth-oauth2) strategy.

## Supported runtimes

Expand All @@ -25,7 +25,7 @@ Be sure to copy the client secret, redirect URI, Tenant ID and the Application (
### Install dependencies

```bash
npm install remix-auth-microsoft remix-auth remix-auth-oauth2
npm install remix-auth-microsoft remix-auth
```

### Create the strategy instance
Expand All @@ -34,27 +34,27 @@ npm install remix-auth-microsoft remix-auth remix-auth-oauth2
// app/services/auth.server.ts
import { MicrosoftStrategy } from "remix-auth-microsoft";
import { Authenticator } from "remix-auth";
import { sessionStorage } from "~/services/session.server";

export let authenticator = new Authenticator<User>(sessionStorage); //User is a custom user types you can define as you want
export let authenticator = new Authenticator<User>(); //User is a custom user types you can define as you want

let microsoftStrategy = new MicrosoftStrategy(
{
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET",
redirectUri: "https://example.com/auth/microsoft/callback",
redirectURI: "https://example.com/auth/microsoft/callback",
tenantId: "YOUR_TENANT_ID", // optional - necessary for organization without multitenant (see below)
scope: "openid profile email", // optional
scopes: ["openid", "profile", "email"], // optional
prompt: "login", // optional
},
async ({ accessToken, extraParams, profile }) => {
async ({ request, tokens }) => {
// Here you can fetch the user from database or return a user object based on profile
// return {profile}
let accessToken = tokens.accessToken();
let idToken = tokens.idToken();
let profile = await MicrosoftStrategy.userProfile(accessToken);

// The returned object is stored in the session storage you are using by the authenticator

// If you're using cookieSessionStorage, be aware that cookies have a size limit of 4kb
// For example this won't work
// return {accessToken, extraParams, profile}

// Retrieve or create user using id received from userinfo endpoint
// https://graph.microsoft.com/oidc/userinfo
Expand All @@ -64,15 +64,13 @@ let microsoftStrategy = new MicrosoftStrategy(
// If you use the email address to identify users and allow signing in from any tenant (`tenantId` is not set)
// it opens up a possibility of spoofing users!


return User.findOrCreate({ id: profile.id });
}
);

authenticator.use(microsoftStrategy);
```


See [Microsoft docs](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow) for more information on `scope` and `prompt` parameters.

### Applications with single-tenant authentication (no multitenant allowed)
Expand All @@ -82,61 +80,6 @@ The value of `tenantId` should be the **Directory (tenant) ID** found under **Ov

You must also select **Accounts in this organizational directory** as Supported account types in your App Registration.

### Setup your routes

```tsx
// app/routes/login.tsx
export default function Login() {
return (
<form action="/auth/microsoft" method="post">
<button>Login with Microsoft</button>
</form>
);
}
```

```tsx
// app/routes/auth/microsoft.tsx
import type { ActionArgs } from "@remix-run/node";
import { authenticator } from "~/auth.server";
import { redirect } from "@remix-run/node";

export const loader = () => redirect("/login");

export const action = ({ request }: ActionArgs) => {
return authenticator.authenticate("microsoft", request);
};
```

```ts
// app/routes/auth/microsoft/callback.tsx
import type { LoaderArgs } from "@remix-run/node";
import { authenticator } from "~/auth.server";

export const loader = ({ request }: LoaderArgs) => {
return authenticator.authenticate("microsoft", request, {
successRedirect: "/dashboard",
failureRedirect: "/login",
});
};
```

### Add Session Storage
### Next steps

```ts
// app/services/session.server.ts
import { createCookieSessionStorage } from "@remix-run/node";

export let sessionStorage = createCookieSessionStorage({
cookie: {
name: "_session", // use any name you want here
sameSite: "lax", // this helps with CSRF
path: "/", // remember to add this so the cookie will work in all routes
httpOnly: true, // for security reasons, make this cookie http only
secrets: ["s3cr3t"], // replace this with an actual secret
secure: process.env.NODE_ENV === "production", // enable this in prod only
},
});

export let { getSession, commitSession, destroySession } = sessionStorage;
```
See the [Remix Auth documentation](https://sergiodxa.github.io/remix-auth/) on how to configure your routes, persist the session in a cookie, etc.
20 changes: 0 additions & 20 deletions config/jest.config.ts

This file was deleted.

24 changes: 0 additions & 24 deletions config/jest/babel.config.js

This file was deleted.

4 changes: 0 additions & 4 deletions config/jest/setup.ts

This file was deleted.

10 changes: 10 additions & 0 deletions config/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
coverage: {
include: ["src/**/*.ts"],
},
passWithNoTests: true,
},
});
Loading

0 comments on commit 3130a6e

Please sign in to comment.