SAML connections
Before you start
You must configure your application instance through the Clerk Dashboard for the SAML connection(s) that you want to use. Visit the appropriate SAML guide for your platform to learn how to configure your instance.
Create the sign-up and sign-in flow
When using SAML, the sign-in and sign-up flows are equivalent. A successful SAML flow consists of the following steps:
- Start the SAML flow by calling
SignIn.authenticateWithRedirect(params)
orSignUp.authenticateWithRedirect(params)
. Note that both of these methods require aredirectUrl
param, which is the URL that the browser will be redirected to once the user authenticates with the OAuth provider. - Create a route at the URL that the
redirectUrl
param points to. The following example names this route/sso-callback
. This route should either call theClerk.handleRedirectCallback()
method or render the prebuilt<AuthenticateWithRedirectCallback/>
component.
The following example shows two files:
- The sign-in page where the user can start the SAML flow.
- The SSO callback page where the SAML flow is completed.
'use client';
import * as React from 'react';
import { useSignIn, useSignUp } from '@clerk/nextjs';
export default function OauthSignIn() {
const { signIn } = useSignIn();
const { signUp } = useSignUp();
if (!signIn || !signUp) return null;
const signInWith = (e: React.FormEvent) => {
e.preventDefault();
const email = (e.target as HTMLFormElement).email.value;
signIn
.authenticateWithRedirect({
identifier: email,
strategy: 'saml',
redirectUrl: '/sso-callback',
redirectUrlComplete: '/',
})
.then((res) => {
console.log(res);
})
.catch((err: any) => {
console.log(err.errors);
console.error(err, null, 2);
});
};
// Render a button for each supported SAML provider
// you want to add to your app
return (
<form onSubmit={(e) => signInWith(e)}>
<input type="email" name="email" placeholder="Enter email address" />
<button>Sign in with SAML</button>
</form>
);
}
import { AuthenticateWithRedirectCallback } from '@clerk/nextjs';
export default function SSOCallback() {
console.log('testing');
// Handle the redirect flow by rendering the
// prebuilt AuthenticateWithRedirectCallback component.
// This is the final step in the custom SAML flow.
return <AuthenticateWithRedirectCallback />;
}
SAML account transfer flows
It is common for users who are authenticating with OAuth to use a sign-in button when they mean to sign-up, and vice versa. For those cases, the SignIn
and SignUp
objects have a transferable
status that indicates whether the user can be transferred to the other flow.
If you would like to keep your sign-in and sign-up flows completely separate, then you can skip this section.
The following example shows how to handle these cases in your sign-in flow. The same logic can be applied to the sign-up flow; change the signIn.AuthenticateWithRedirect()
to signUp.authenticateWithRedirect()
.
'use client';
import * as React from 'react';
import { useSignIn, useSignUp } from '@clerk/nextjs';
export default function OauthSignIn() {
const { signIn } = useSignIn();
const { signUp, setActive } = useSignUp();
if (!signIn || !signUp) return null;
const signInWith = (e: React.FormEvent) => {
e.preventDefault();
const email = (e.target as HTMLFormElement).email.value;
signIn
.authenticateWithRedirect({
identifier: email,
strategy: 'saml',
redirectUrl: '/sso-callback',
redirectUrlComplete: '/',
})
.then((res) => {
console.log(res);
})
.catch((err: any) => {
console.log(err.errors);
console.error(err, null, 2);
});
};
async function handleSignIn(e: React.FormEvent) {
if (!signIn || !signUp) return null;
// If the user has an account in your application, but does not yet
// have a SAML account connected to it, you can transfer the SAML
// account to the existing user account.
const userExistsButNeedsToSignIn =
signUp.verifications.externalAccount.status === 'transferable' &&
signUp.verifications.externalAccount.error?.code ===
'external_account_exists';
if (userExistsButNeedsToSignIn) {
const res = await signIn.create({ transfer: true });
if (res.status === 'complete') {
setActive({
session: res.createdSessionId,
});
}
}
// If the user has a SAML account but does not yet
// have an account in your app, you can create an account
// for them using the SAML information.
const userNeedsToBeCreated =
signIn.firstFactorVerification.status === 'transferable';
if (userNeedsToBeCreated) {
const res = await signUp.create({
transfer: true,
});
if (res.status === 'complete') {
setActive({
session: res.createdSessionId,
});
}
} else {
// If the user has an account in your application
// and has an SAML account connected to it, you can sign them in.
signInWith(e);
}
}
// Render a button for each supported OAuth provider
// you want to add to your app
return (
<form onSubmit={(e) => handleSignIn(e)}>
<input type="email" name="email" placeholder="Enter email address" />
<button>Sign in with SAML</button>
</form>
);
}