Configure and use OAuth2Strategy directly
mainTo use the strategy, add it to your Authenticator instance. You must provide the client credentials and the provider's OAuth2 endpoints. The second argument to the OAuth2Strategy constructor is a verify function that receives tokens and request, where you should implement your user lookup logic.
import { OAuth2Strategy, CodeChallengeMethod } from "remix-auth-oauth2";
export const authenticator = new Authenticator<User>();
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
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"
);