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

@outcast.by/js-auth

v0.1.1

Published

## Content

Downloads

11

Readme

Lib for auth

Content

Action creators

Usage example:

// connect.js

import { connect } from 'react-redux'
import { signIn } from '@outcast.by/js-auth'

export default connect(() => ({}), { signIn })

signIn

Takes object as variables for the gql request:

signIn({ variables: { entity: { email: 'email', password: 'password' } } })

If you use Base Form pass it as onSubmit prop

<Form
  ...
  onSubmit={signIn}
  ...
/>

signUp

Takes object as variables for the gql request:

signUp({ variables: { entity: { email: 'email', password: 'password', passwordConfirmation: 'password' } } })

If you use Base Form pass it as onSubmit prop

If you need to pass extraParams, declare function amendVariables and pass it to Form

const amendVariables = (variables) => {
  return { ...variables, extraParams: LocalStorage.getIn('extraParams') } }
}

<Form
  ...
  onSubmit={signUp}
  amendVariables={amendVariables}
  ...
/>

forgotPassword

Takes object as variables for the gql request:

forgotPassword({ variables: { entity: { email: 'email', restoreUrl: `${window.location.origin}/auth/restore` } } })

If you use Base Form pass it as onSubmit prop and declare function amendVariables.

const amendVariables = (variables) => {
  return { ...variables, entity: { ...variables.entity, restoreUrl: `${window.location.origin}/auth/restore` } }
}

<Form
  ...
  onSubmit={forgotPassword}
  amendVariables={amendVariables}
  ...
/>

restorePassword

Takes object as variables for the gql request:

restorePassword({
  variables: { entity: { restoreHash: 'hash', password: 'password', passwordConfirmation: 'password' } },
})

hash locate in query string of url

If you use Base Form pass it as onSubmit prop and declare function amendVariables.

const { hash } = queryString.parse(window.location.search)

const amendVariables = (variables) => {
  return { ...variables, entity: { ...variables.entity, restoreHash: hash } }
}

<Form
  ...
  onSubmit={restorePassword}
  amendVariables={amendVariables}
  ...
/>

refreshToken

Takes object as variables for the gql request:

refreshToken({ variables: { entity: { refreshToken: `refreshToken` } } })

logout

Used without variables

logout()

complete

Takes object as variables for the gql request:

complete({
  variables: { entity: entityParams, oauthData: { uid: 'uid', provider: 'GOOGLE' } },
})

If you use Base Form pass it as onSubmit prop and declare function amendVariables.

const amendVariables = (variables) => {
  const { uid, provider } = oauthData
  return { ...variables, oauthData: { uid, provider: provider.toUpperCase() } }
}

<Form
  ...
  onSubmit={complete}
  amendVariables={amendVariables}
  ...
/>

getTwitterAuthUrl

Takes object as variables for the gql request:

getTwitterAuthUrl({ getTwitterAuthUrl: `${window.location.origin}${process.env.REACT_APP_TWITTER_CALLBACK_PATH}` })

Api Loaders

Usage example:

import { apiLoaders } from '@outcast.by/js-auth'

_.each(apiLoaders, (loader) => loader())

rootSignIn

Add to rootSignIn.js functions according what providers you need to use

import { facebookSignIn } from '@outcast.by/js-auth'
import { googleSignIn } from '@outcast.by/js-auth'

export default { google: googleSignIn, facebook: facebookSignIn }

These functions takes object with 3 params: useState setter, onSuccess function and history.push

const [completeAuth, setCompleteAuth] = useState({ missingFields: [], onSuccess: null, isShowCompleteAuth: false })
const onSuccess = () => history.push('/')

const onClickOauth = () => {
  facebookSignIn({ setCompleteAuth, onSuccess, pushToHistory: history.push })
}

Auth utils

  1. Auth.logout() - clear cookies keys for oauth and replace window location to /auth
  2. Auth.handleTokenError(action) - provide invalidTokenCallback for React module from @outcast.by/js-ext. When server response with 401 and error message is invalid_access_token this function requests new JWT data from refreshToken gql mutation. For prevent endless requests, we calculate count of refreshToken requests. If this count more than 5 performed logout()

Setup

  1. Add jsAuth config to your app config
// config/common/jsAuth.js

import store from 'store'
import { FIELDS } from 'gql/auth/fields'
import LocalStorage from 'utils/LocalStorage'

export default {
  accessTokenKey: 'accessToken',
  refreshTokenKey: 'refreshToken',
  deviceUuidKey: 'deviceUuid',
  dispatch: store.dispatch,
  fields: FIELDS,
  gqlVariables: { filter: 'String', limit: 'Int', order: 'String' },
  extraParams: () => ({ ownerUuid: LocalStorage.getIn('ownerUuid') }),
  facebookId: 'process.env.REACT_APP_FACEBOOK_ID',
  googleClientId: process.env.REACT_APP_GOOGLE_CLIENT_ID,
  twitterCallbackPath: '/auth/twitter/callback',
}

accessTokenKey, refreshTokenKey and deviceUuidKey - keys for storing and getting accessToken, refreshToken and deviceUuid from cookies dispatch - used for dispatch actions in lib fields - fields for gql requests gqlVariables - additional variables for gql requests extraParams - using for creating new user by oauth with additional params facebookId and googleClientId - data for providers oauth

  1. Add fields for gql example:

    // gql/auth/fields.js
    
    import gql from 'graphql-tag'
    import { USER_FIELDS } from '../users/userFields'
    
    export const FIELDS = gql`
      fragment AuthFields on Auth {
        accessToken
        refreshToken
        deviceUuid
        currentUser {
          ...UserFields
        }
      }
      ${USER_FIELDS}
    `
  2. Save accessToken, refreshToken and deviceUuid to cookies Example (by redux watcher):

export function* genStoreJWT ({ data: { accessToken, refreshToken, deviceUuid } }) {
  yield Cookies.set(Config.get(['jsAuth','accessTokenKey']), accessToken)
  yield Cookies.set(Config.get(['jsAuth','refreshTokenKey']), refreshToken)
  yield Cookies.set(Config.get(['jsAuth','deviceUuidKey']), deviceUuid)
  yield LocalStorage.setIn('ownerUuid', null)
}

export const watchers = [
  takeLatest([SIGN_IN, SIGN_UP, PROVIDER_LOGIN, RESTORE_PASSWORD, REFRESH_TOKEN, COMPLETE], genStoreJWT),
]