Skip to content

Instantly share code, notes, and snippets.

@yogain123
Last active February 24, 2024 13:41
Show Gist options
  • Save yogain123/3fb9fc6c035f1f540e11c33d442c683a to your computer and use it in GitHub Desktop.
Save yogain123/3fb9fc6c035f1f540e11c33d442c683a to your computer and use it in GitHub Desktop.
nextjs
@yogain123
Copy link
Author

redirect

maybe that page is under maintainenece or maybe it is permanent shifted to other page (but user might have bookmarked old page)

Screen Shot 2022-10-23 at 6 50 37 PM

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

AUTHENTICATION IN NEXTJS

Screen Shot 2022-10-23 at 7 47 17 PM

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

@yogain123
Copy link
Author

yogain123 commented Oct 23, 2022

signin and signout from client

Screen Shot 2022-10-23 at 8 27 04 PM


Screen Shot 2022-10-23 at 8 27 13 PM


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

@yogain123
Copy link
Author

yogain123 commented Oct 25, 2022

Client side authentication

Screen Shot 2022-10-25 at 3 08 58 PM


Screen Shot 2022-10-25 at 3 09 09 PM


Screen Shot 2022-10-25 at 3 09 16 PM



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,
    }),
  ],
});



Screen Shot 2022-10-25 at 3 40 21 PM

@yogain123
Copy link
Author

Securing pages client side

Screen Shot 2022-10-25 at 3 39 09 PM

@yogain123
Copy link
Author

yogain123 commented Oct 25, 2022

Server side authentication

authentication inside getServerSideProps function

Screen Shot 2022-10-25 at 3 44 27 PM


Screen Shot 2022-10-25 at 3 44 42 PM

@yogain123
Copy link
Author

Securing pages server side

Screen Shot 2022-10-25 at 3 46 09 PM

@yogain123
Copy link
Author

securing api routes

Screen Shot 2022-10-25 at 3 47 02 PM

@yogain123
Copy link
Author

yogain123 commented Oct 25, 2022

@yogain123
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment