GithubHelp home page GithubHelp logo

giovan / nextjs-starter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hugotox/nextjs-cookie-demo

1.0 1.0 0.0 1.5 MB

Next.js starter project using redux and cookie based authentication

JavaScript 100.00%

nextjs-starter's Introduction

Next.js starter

Starter project using Next.js with redux, cookie based authentication, server side rendering and tests set up.

By default, authentication is not enforced for any page. To make pages available to authenticated users only, use the withLoginRequired higher order component.

The withLoginRequired HOC will make an API call server side the first time the page is accessed. It will use the session cookie from the req object provided by Express to call /api/whoami endpoint. This endpoint returns a user object if authenticated, and saves it to redux store.

withLoginRequired example usage:

import withLoginRequired from '../lib/auth/with-login-required';
import Home from '../components/index/Home';

export default withLoginRequired(Home);

If no session cookie is found, it will redirect to /login, otherwise it will inject the user object as a prop to the wrapped component.

withLoginRequired and connect example

If you need to make your page component a redux container, just connect it to redux using the compose function:

import { connect } from 'react-redux';
import { compose } from 'redux';
import Home from '../components/index/Home';
import withLoginRequired from '../lib/auth/with-login-required';

const mapStateToProps = state => {
  return {
    // return desired props from redux
  };
};

const mapDispatchToProps = dispatch => {
  return {
    // create your action dispatchers here
  };
};

export default compose(
  withLoginRequired,
  connect(
    mapStateToProps,
    mapDispatchToProps
  )
)(Home);

Run in dev mode

$ npm run dev

Build & run production

$ npm run build
$ npm run start

Run unit tests

$ npm run test

With coverage report

$ npm run test:coverage

Coverage report is exported to coverage/lcov-report/index.html

nextjs-starter's People

Contributors

dependabot[bot] avatar hugotox avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.