next-auth
custom signin signout pages
pages/api/auth/[...nextauth].js
... pages: { signIn: '/auth/signin', signOut: '/auth/signout', error: '/auth/error', // Error code passed in query string as ?error= verifyRequest: '/auth/verify-request', // (used for check email message) newUser: '/auth/new-user' // New users will be directed here on first sign in (leave the property out if not of interest) }...
display signin errors
// pages/api/[...nextauth].js
export default NextAuth({
...
pages: {
...
error: '/signin', // Error code passed in query string as ?error=
}
});
// pages/signin.jsx
import { getProviders } from "next-auth/react"
import { useRouter } from "next/router";
const SignIn = ({ providers }) => {
const { error } = useRouter().query;
return (
<>
<h1>Login</h1>
{/* Error message */}
{error && <SignInError error={error} />}
{/* Login options */}
{Object.values(providers).map((provider) => (
<div key={provider.name}>...</div>
))}
</>
);
};
export async function getServerSideProps(context) {
return { props: { providers: await getProviders() } };
}
// pages/signin.jsx
const errors = {
Signin: "Try signing with a different account.",
OAuthSignin: "Try signing with a different account.",
OAuthCallback: "Try signing with a different account.",
OAuthCreateAccount: "Try signing with a different account.",
EmailCreateAccount: "Try signing with a different account.",
Callback: "Try signing with a different account.",
OAuthAccountNotLinked:
"To confirm your identity, sign in with the same account you used originally.",
EmailSignin: "Check your email address.",
CredentialsSignin:
"Sign in failed. Check the details you provided are correct.",
default: "Unable to sign in.",
};
const SignInError = ({ error }) => {
const errorMessage = error && (errors[error] ?? errors.default);
return <div>{errorMessage}</div>;
};
Authorization
Facebook App Auth
- link
- https://developers.facebook.com/apps
- create a new app
- Left menu
Facebook Login
>Settings
Valid OAuth Redirect URIs
- paste in
https://MYAPP.MYDOMAIN.com/api/auth/callback/facebook
- paste in
- profit?
Google Authorization
- link
- https://console.developers.google.com/apis/credentials
- select appropriate app
Authorized redirect URIs
- add
https://MYAPP.MYDOMAIN.com/api/auth/callback/google
- add
Error Message Handling
- [link](https://logfetch.com/next-auth-custom-error-page/#:~:text=Once the signin process is set up with,at %2Fpages%2Fsignin.jsx and show the error message there.)
Credits
- Pages | NextAuth.js (next-auth.js.org)
- [link](https://logfetch.com/next-auth-custom-error-page/#:~:text=Once the signin process is set up with,at %2Fpages%2Fsignin.jsx and show the error message there.)
- Nextauth Complete Tutorial #12 Add Facebook provider - YouTube
- NextAuth.js (next-auth.js.org)