3.4.0 • Published 3 years ago

croods-auth v3.4.0

Weekly downloads
41
License
MIT
Repository
github
Last release
3 years ago

Maintainability Test Coverage npm bundle size

Croods-Auth

Croods-auth is another layer of abstraction on top of Croods, providing you with a ready-to-use solution for user authentication and permission control.

Install

yarn add croods-auth croods react react-dom lodash react-use-form-state

Usage

Bellow is a simple example of its usage with @reach/router:

import { CroodsProvider } from 'croods'
import { Auth, authHeaders, saveHeaders } from 'croods-auth'

export default props => (
  <CroodsProvider handleResponseHeaders={saveHeaders} headers={authHeaders} baseUrl="https://foo.bar">
    <Router>
      <Auth
        Component={SomeBlockedPage}
        path="/"
        unauthorized={() => redirect('/sign-in')}
      />
      <SignIn path="/sign-in" />
    </Router>
  </CroodsProvider>
)

Main concepts to notice here:

  • authHeaders: Provides Croods with headers from the storage for usage on Croods requests. Eg.: Auth-Token, Uid, Client, Token-Type and Expiry.
  • Auth: Used for checking permissions required for a component.

And then we implement our SignIn page:

import { useSignIn } from 'croods-auth'

const Input = ({ name, label = name, ...props }) => (
  <div className="form-group">
    <label htmlFor={name}>{label}</label>
    <input {...props} className="form-control" id={name} />
  </div>
)

export default props => {
  const [{ signingIn, error, ...config }] = useSignIn({
    afterSuccess: () => navigate(`/home`),
  })
  return (
    <form {...config.formProps}>
      <h2>Sign In</h2>
      <Input {...config.emailProps} label="Email address" />
      <Input {...config.passwordProps} />
      <button type="submit" className="btn btn-primary">
        {signingIn ? 'Signing in...' : 'Sign in'}
      </button>
    </form>
  )
}

There are hooks available for all usual authentication operations (sign up, edit profile, etc).

Read more about it on the Docs!

3.4.0

3 years ago

3.3.1-beta.2

4 years ago

3.3.1-beta.3

4 years ago

3.3.1-beta.4

4 years ago

3.3.1-beta.1

4 years ago

3.3.1-beta.0

4 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.8

4 years ago

3.1.7

4 years ago

3.1.6

4 years ago

3.1.5

4 years ago

3.1.4

4 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

2.0.0-rc.4

5 years ago

2.0.0-rc.3

5 years ago

2.0.0-rc.2

5 years ago

2.0.0-rc.1

5 years ago

2.0.0-rc.0

5 years ago

1.0.0-rc.0

5 years ago

1.0.2

5 years ago

1.0.0

5 years ago