Last active
February 24, 2024 13:41
-
-
Save yogain123/3fb9fc6c035f1f540e11c33d442c683a to your computer and use it in GitHub Desktop.
nextjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hola |
Setup
setup SSO for github
https://www.youtube.com/watch?v=Aiqzfmy9A_4&list=PLC3y8-rFHvwgC9mj0qv972IO5DmD-H0ZH&index=68
signin and signout from client
actually <Link href="" />
is not needed, you can use either Link or signIn function, or both
you can check cookie, you will see next auth session token
if you pass signIn("github") , github string inside then if you are already loggedin in github then it will auto login you on click of signin
Client side authentication
above code is deplricated, new one is
in header.jsx
import classes from "../../../styles/Header.module.css";
import { signIn, signOut, useSession } from "next-auth/react";
function Header() {
const sessionDetails = useSession();
const { data, status } = sessionDetails;
console.log({ data });
return (
<div className={classes.header}>
This is header
<hr />
{status === "authenticated" && status !== "loading" && (
<button
onClick={() => {
signIn("github");
}}
type="button"
>
SIGN OUT
</button>
)}
{status === "unauthenticated" && status !== "loading" && (
<button
onClick={() => {
signOut();
}}
type="button"
>
SIGN IN
</button>
)}
{status === "loading" && <div>Loading...</div>}
</div>
);
}
export default Header;
in _app.js
import { SessionProvider } from "next-auth/react";
import "../styles/globals.css";
function MyApp({ Component, pageProps }) {
return (
<SessionProvider>
<Component {...pageProps} />
</SessionProvider>
);
}
export default MyApp;
in [...nextauth].js
import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";
export default NextAuth({
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID,
clientSecret: process.env.GITHUB_SECRET,
}),
],
});
Connecting to DB
setup mongodb atlas
https://www.youtube.com/watch?v=H4ptrFimcSM&list=PLC3y8-rFHvwgC9mj0qv972IO5DmD-H0ZH&index=76
deploying nextjs app to versel
https://www.youtube.com/watch?v=KmxAH7ng8Qw&list=PLC3y8-rFHvwgC9mj0qv972IO5DmD-H0ZH&index=79
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fallback key
fallback "blocking" is same as fallback:false , only differenece is that instead of showing fallback page we will not see any new content on UI when page is generated on the server