### Development Environment Setup and Quality Checks Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/CONTRIBUTING.md This section outlines the essential commands for setting up the project's development environment, including dependency installation, running automated tests, and executing code quality checks using Bun. ```Shell bun install ``` ```Shell bun test ``` ```Shell bun run quality ``` -------------------------------- ### Install remix-auth-oauth2 Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/README.md This snippet shows the command to add the `remix-auth-oauth2` package to your project using npm. ```bash npm add remix-auth-oauth2 ``` -------------------------------- ### Start OAuth2 Authentication Flow in Remix Route Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/README.md This TypeScript snippet shows how to initiate the OAuth2 authentication flow by calling `authenticator.authenticate` in a Remix action or loader. This redirects the user to the identity provider's login page. ```ts export async function action({ request }: Route.ActionArgs) { await authenticator.authenticate("provider-name", request); } ``` -------------------------------- ### Store and Use Refresh Token in User Object Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/README.md This TypeScript snippet shows how to modify the `OAuth2Strategy`'s verify function to include the refresh token in the returned user object. It then demonstrates how to use this stored refresh token to get new tokens later. ```ts authenticator.use( new OAuth2Strategy( options, async ({ tokens, request }) => { let user = await getUser(tokens, request); return { ...user, accessToken: tokens.accessToken() refreshToken: tokens.hasRefreshToken() ? tokens.refreshToken() : null, } } ) ); // later in your code you can use it to get new tokens object let tokens = await strategy.refreshToken(user.refreshToken); ``` -------------------------------- ### Discover Provider Endpoints with OAuth2Strategy.discover in TypeScript Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/README.md Demonstrates how to use the `discover` static method of `OAuth2Strategy` to automatically fetch OAuth2 provider configuration endpoints (authorization, token, revocation) from the `/.well-known/openid-configuration` endpoint. This method performs a network request during strategy creation, potentially adding startup latency, and is recommended for one-time use. ```typescript export let authenticator = new Authenticator(); authenticator.use( await OAuth2Strategy.discover( "https://provider.com", { clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, redirectURI: "https://example.app/auth/callback", scopes: ["openid", "email", "profile"], // optional }, async ({ tokens, request }) => { // here you can use the params above to get the user and return it // what you do inside this and how you find the user is up to you return await getUser(tokens, request); } ) ); ``` -------------------------------- ### Configure OAuth2Strategy with Authenticator Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/README.md This TypeScript snippet demonstrates how to initialize and configure `OAuth2Strategy` with `remix-auth`'s `Authenticator`. It includes setting up client credentials, endpoints, scopes, and a verify callback function. ```ts import { OAuth2Strategy, CodeChallengeMethod } from "remix-auth-oauth2"; export const authenticator = new Authenticator(); authenticator.use( new OAuth2Strategy( { cookie: "oauth2", // Optional, can also be an object with more options clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, authorizationEndpoint: "https://provider.com/oauth2/authorize", tokenEndpoint: "https://provider.com/oauth2/token", redirectURI: "https://example.app/auth/callback", tokenRevocationEndpoint: "https://provider.com/oauth2/revoke", // optional scopes: ["openid", "email", "profile"], // optional codeChallengeMethod: CodeChallengeMethod.S256, // optional }, async ({ tokens, request }) => { // here you can use the params above to get the user and return it // what you do inside this and how you find the user is up to you return await getUser(tokens, request); } ), // this is optional, but if you setup more than one OAuth2 instance you will // need to set a custom name to each one "provider-name" ); ``` -------------------------------- ### Handle OAuth2 Callback in Remix Loader Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/README.md This TypeScript snippet demonstrates how to handle the OAuth2 callback from the identity provider in a Remix loader. It uses `authenticator.authenticate` to process the callback and retrieve the user object. ```ts export async function loader({ request }: Route.LoaderArgs) { let user = await authenticator.authenticate("provider-name", request); // now you have the user object with the data you returned in the verify function } ``` -------------------------------- ### Customize OAuth2 Strategy Cookie Options in TypeScript Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/README.md Illustrates how to configure custom cookie options for the OAuth2 strategy, including `name`, `maxAge`, `path`, `httpOnly`, `sameSite`, and `secure` flags. This allows fine-grained control over the session cookie's behavior and security settings, such as setting a 1-week max age and making it secure in production. ```typescript authenticator.use( new OAuth2Strategy( { cookie: { name: "oauth2", maxAge: 60 * 60 * 24 * 7, // 1 week path: "/auth", httpOnly: true, sameSite: "lax", secure: process.env.NODE_ENV === "production", }, clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, authorizationEndpoint: "https://provider.com/oauth2/authorize", tokenEndpoint: "https://provider.com/oauth2/token", redirectURI: "https://example.app/auth/callback", }, async ({ tokens, request }) => { return await getUser(tokens, request); } ) ); ``` -------------------------------- ### Refresh OAuth2 Access Token Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/README.md This TypeScript snippet illustrates how to use the `refreshToken` method of `OAuth2Strategy` to obtain new access tokens. It requires a previously stored refresh token. ```ts let strategy = new OAuth2Strategy(options, verify); let tokens = await strategy.refreshToken(refreshToken); ``` -------------------------------- ### Revoke OAuth2 Access Token Source: https://github.com/sergiodxa/remix-auth-oauth2/blob/main/README.md This TypeScript snippet demonstrates how to revoke an OAuth2 access token using the `revokeToken` method of the `OAuth2Strategy` instance. This invalidates the token with the identity provider. ```ts await strategy.revokeToken(user.accessToken); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.