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

@codens/codens-auth

v0.1.9

Published

---

Downloads

1

Readme

codens-auth


Installation

npm install --save codens-auth

Documentation

Getting Started

1. Create configuration file

Create a configuration object to be able to do API calls and redirect to the correct auth endpoints.

export const authConfig = {
  host: {
    protocol: 'http',
    name: 'localhost:5000',
  },
  endpoints: {
    loggedIn: '/', // route when logged in
    login: '/login', // login route
    signup: '/signup', // signup route
    recover: '/recover', // recover route
    api: {
      login: '/auth', // api login route
      register: '/register', // api register route
      recover: '/recover', // api recover route
    },
  },
};

2. Import and combine reducer

const { auth, form } = codensAuthReducer;
export default combineReducers({
  auth,
  form,
});

form is the redux-form reducer. Don't implement if you already use redux-form.

3. Modify routes

Add requireAuth to protect the route. noAuth makes route inaccessible when user is logged in.

<Route>
  <Route path="/protected" component={Protected} requireAuth />
  <Route path="/open" component={Open} noAuth />
</Route>

createRoutes adds onEnter depending on the routes. store and config are required.

import { createRoutes } from 'codens-auth';
const routes = createRoutes(routeElements, store, config);

When using SSR, modify this in the render function as well.
Note: createRoutes has to be called before the Provider component.

4. Add token to initialstate

When SSR is enabled, you have to add the token to the initialstate to redirect if necessary.
Note: Add cookieParser to read cookies.

const token = (req.cookies && req.cookies.token) || null;
const store = configureStore(memoryHistory, { auth: { token, isFetching: false } });

5. Extract language messages

Add pattern node_modules/codens-auth/src/**/*.js to extractMessages.js.


Components

1. Login - Signup

import {
  Login,
  Signup,
} from 'codens-auth';

<Route path="/login" component={Login} noAuth />
<Route path="/signup" component={Signup} noAuth />

Login: Login form component.
Signup: Signup form component.

2. SignoutButton

import { SignoutButton } from 'codens-auth';

<SignoutButton />

API Server

1. Create configuration file

const authConfig = {
  token: {
    expires: 10080,
    secret: 'eyJ0aXRsZSI6ImZ1Y2sgeW91IHBheSBtZSIsImxlYWQiOiJ0',
  },
};

2. createSecureAPI

import { createSecureAPI } from 'codens-auth/lib/server';

const secureAPI = createSecureAPI(authConfig, mongo);

Note: mongo is an instance of mongoDB.

3. Routes

Make sure that the auth routes are accessible.

router.use(secureAPI.routes.unless([
  '/auth',
  '/register',
]));

Auth routes:

router.post('/auth', secureAPI.auth);
router.post('/register', secureAPI.register);