npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-session-manager-provider

v1.0.5

Published

Session Provider for Next.js

Downloads

1

Readme

Overview

Next Session Provider is a open source session provider solution for Next.js applications.

It is designed based on NextAuth package that provides authentication solution. This package is targeted to project that want to have their own authentication or they accually have one.

For now there is a Client Side session provider that store and manage session.

Getting Started

npm install next-session-manager-provider

How to use

  1. Configure Shared session state - to be able to use useSession first you'll need to expose the session context , at the top level of your application same as in the next-auth package. There is a 1 difference that you have to pass a function named "sessionGetter" that is a API call for your backend server that gets current user based on your authentication.
function sessionGetter() {
    fetch("/api/getUser").then((user: User) => {
        // Get user and return it.
        return user;
    })
}

const App = ({ Component, pageProps: { session, ...pageProps } }: AppProps) => {
    return (
        <SessionProvider session={session} sessionGetter={sessionGetter}>
            <Childrens>
        </SessionProvider>
    )
}
  1. Frotend - React Hooks - The useSession React Hook in the next-session-manager-provider client is the way to get session and status of session
import { useSession } from "next-session-manager-provider/react";

const Menu = () => {
    const { data: session, update, status } = useSession();

    // session variable has all information about saved session. example:
    console.log(session.firstName);

    // update is a function that call provider to update state.
    update();

    // status is a type that store session status. There is a 3 session states: "authenticated" | "unauthenticated" | "loading"
    console.log(status); // Its conna output current session status.
}
  1. Authorization - After authenticate you can trigger function that make session refresh. Example:
import { onSignIn, onSingOut } from "next-session-manager-provider/react";

const LoginView = () => {
    const authenticationHandler = () => {
        // Your Authenticate Logic

        // It should refresh your session to the newest using your function that you pass in provider.
        onSignIn();
    }
}

const LogoutView = () => {
    const logoutHandler = () => {
        // Your Logout Logic

        // It should refresh session as function above.
        onSingOut();
    }
}

Typescript support

By default, Typescript will merge new interface properties and overwrite exisiting one. In this case, the default session user properties will be overwitten, with the one one define above.

Example that overwrite whole interface.

import NextSessionManagerProvider from "next-session-manager-provider";

export interface UserSession {
    firstName: string;
    lastName: string;
    dateOfBirth: string;
}

declare module "next-session-manager-provider" {
    interface Session {
        user: UserSession;
    }
}

Feature plans

Add getting session on the server side. Right now if you're using cookies as a jwt token storage you just have to get it from request for example with

export const axiosInstanceSSR = (cookies: string) =>
    axios.create({
        baseURL: "http://localhost:8080",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
            Cookie: cookies,
        },
        withCredentials: true,
    });


export const getServerSideProps = async ({ req }: any) => {
    // Send request with axios
    const products = await axiosInstanceSSR(req.headers.cookie).get(`/api/products`);

    return {
        props: {
            data: {
                products,
            }
        },
    }
}

License

ISC