Remix Auth Microsoft Strategy

3.0.1 · active · verified Wed Apr 22

remix-auth-microsoft is a dedicated strategy for remix-auth that enables authentication against Microsoft Active Directory (work/school accounts) and personal Microsoft accounts (Skype, Xbox, Outlook.com). It extends remix-auth-oauth2 and simplifies the integration of Microsoft's OAuth 2.0 flow into Remix applications. The current stable version is 3.0.1, with major version releases often correlating with updates to its underlying remix-auth and remix-auth-oauth2 dependencies, alongside patch releases for bug fixes. Key differentiators include its tight integration with the Remix ecosystem via remix-auth, support for both Node.js and Cloudflare runtimes, and explicit guidance for multi-tenant and single-tenant configurations. It handles the intricacies of Microsoft's authentication endpoints, abstracting much of the OAuth 2.0 implementation details for developers, making it easier to implement secure Microsoft authentication flows in Remix applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up the MicrosoftStrategy with Remix Auth, including environment variable usage for credentials, recommended scopes, and a basic user resolution logic. It also includes an example of an authenticator instance and how to use it with the strategy.

// app/services/auth.server.ts
import { MicrosoftStrategy } from "remix-auth-microsoft";
import { Authenticator } from "remix-auth";

interface User {
  id: string;
  // ... other user properties
}

export let authenticator = new Authenticator<User>(/* SessionStorage */);

let microsoftStrategy = new MicrosoftStrategy(
  {
    clientId: process.env.MICROSOFT_CLIENT_ID ?? '',
    clientSecret: process.env.MICROSOFT_CLIENT_SECRET ?? '',
    redirectURI: process.env.MICROSOFT_REDIRECT_URI ?? 'http://localhost:3000/auth/microsoft/callback',
    tenantId: process.env.MICROSOFT_TENANT_ID, // optional for multi-tenant apps
    scopes: ['openid', 'profile', 'email', 'offline_access'], // recommended scopes
    prompt: 'select_account' // 'login', 'consent', 'none', 'select_account'
  },
  async ({ request, tokens, profile }) => {
    // Here you can fetch the user from your database or create a new user
    // based on the profile data from Microsoft.
    // `tokens` contains accessToken, refreshToken, idToken, etc.
    // `profile` contains basic user info parsed from the ID token or user info endpoint.

    // Example: Using a placeholder User.findOrCreate
    // It's crucial to use a reliable identifier like 'sub' from the ID token
    // or 'id' from the user profile, not email, to prevent spoofing.

    const userProfileId = profile.id; // or profile.sub from the ID token
    if (!userProfileId) {
      throw new Error("Could not get user ID from Microsoft profile.");
    }

    // Replace with your actual user management logic
    const user = { id: userProfileId, email: profile.emails?.[0]?.value || 'unknown@example.com' };
    console.log(`Authenticated user: ${user.id} (${user.email})`);

    // The returned object is stored in the session by the authenticator
    return user;
  }
);

authenticator.use(microsoftStrategy, "microsoft");

// Example of a route to initiate authentication
// app/routes/auth.microsoft.tsx
// import type { ActionFunctionArgs } from "@remix-run/node";
// import { redirect } from "@remix-run/node";
// import { authenticator } from "~/services/auth.server";

// export async function action({ request }: ActionFunctionArgs) {
//   return authenticator.authenticate("microsoft", request, {
//     successRedirect: "/dashboard",
//     failureRedirect: "/login",
//   });
// }

view raw JSON →