1.2.15 • Published 4 years ago

@config.one/auth v1.2.15

Weekly downloads
2
License
MIT
Repository
-
Last release
4 years ago

Foo

@config.one/auth

NPM JavaScript Style Guide

Contents

Install

npm i @config.one/auth

Get started

import React, { Component } from 'react';
import { AuthProvider, Registration } from '@config.one/auth';

const config = {
  paths: {
    base:     'http://127.0.0.1:8000',
    register: '/api/public/v1/user/register',
  },
  dev: true,
};

class Example extends Component {
  onSuccess = (values, response) => {
    console.log('Registration successful', values);
  };
  
  onFailure = response => {
    console.log('Registration failed!', response);
  };
  
  onLoginClick = () => {
    console.log('Login button clicked!');  
  };

  render () {
    return (
      <AuthProvider config={config}>
        <Registration 
          onSuccess={this.onSuccess}
          onFailure={this.onFailure}
          onLoginClick={this.onLoginClick}
        />
      </AuthProvider>
    )
  }
}

Configuration

./config.js
export default {
    paths: {
        // Base path will be prepended to each path.
        // If not specified, the current location will be used.
        base:               'http://127.0.0.1:8000/',
        
        // Used by the Registration component.
        domainList:         '/api/public/v1/domain/list',
        register:           '/api/public/v1/user/register',
        
        // Used by the Login component.
        token:              '/api/public/token',
        userProfile:        '/api/secure/v1/user/profile',
        
        // Used by the PasswordReset component.
        emailRequestReset:  '/api/public/v1/user/reset_password/email/start',
        
        // You can specify an absolute path. It will ignore the base path.
        // Used by the PhoneActivation component.
        phoneActivate:      'http://config.one/api/secure/v1/user/phone/activate',
    },
    // If true, some usefull verbose information will be shown in the browser console.
    dev: true,
}

None of the configuration entries are required. The default configuration is:

const config = {
  paths: {
      domainList:         '/api/public/v1/domain/list',
      register:           '/api/public/v1/user/register',
      token:              '/api/public/token',
      emailRequestReset:  '/api/public/v1/user/reset_password/email/start',
      phoneActivate:      '/api/secure/v1/user/phone/activate',
      userProfile:        '/api/secure/v1/user/profile'
  },
  dev: false
}

Helpers

createAxios

@config.one/auth uses axios under the hood to make remote requests. The createAxios helper takes a single argument, which is a user config and returns an instance of axios. It intercepts all requests with the status code 401 (Unauthorized) and tries to refresh the access_token before any .then() and catch(). If there were other parallel requests, they are stalled into a queue until the new access_token is retrieved. You should also pass with instance to the AuthProvider component, so that both your project components and the plugin components could use the same instance of axios.

import { AuthProvider, createAxios } from '@config.one/auth';
import config from './config';

// Create an auth aware axios instance
const axios = createAxios(config);

const App = () => (
    <AuthProvider config={config} tokenAwareAxios={axios}>
        ...
    </AuthProvider>
)

Components

AuthProvider

All components should be wrapped into AuthProvider component to have access to users configuration and to axios instance.

import { AuthProvider, Registration, Login, PasswordReset } from '@config.one/auth';
import config from './config';

// Create an auth aware axios instance
const axios = createAxios(config);

const App = () => (
  <AuthProvider config={config} tokenAwareAxios={axios}>
    <Registration/>
    <Login/>
    <PasswordReset/>      
  </AuthProvider>
);

It's not necessary for the components to be direct children of the AuthProvider. They can be nested at any depth:

import { AuthProvider, Registration, Login, PasswordReset } from '@config.one/auth';
import config from './config';

const App = () => (
  <AuthProvider config={config}>
    <SomeOtherComponent>
      <AnotherOne>
        <Registration/>
        <Login/>
        <PasswordReset/>  
      </AnotherOne>
    </SomeOtherComponent>  
  </AuthProvider>
);

Registration

import { Registration } from '@config.one/auth';

<Registration
  onSuccess={this.onRegistrationSuccess}
  onFailure={this.onRegistrationFailure}
  onLoginClick={this.onLoginClick}
  showLoginButton={true}
  autoLogin={true}
  onLoginSuccess={this.onLoginSuccess}
  onLoginFailure={this.onLoginFailure}
/>
API
NameTypeDefaultDescription
onSuccessfunctionnoneWill be called if the response from the server is successfull (code 2xx). The callback gets 2 arguments: values and response. values contains all the data entered into the form, response is a standard Response object.
onFailurefunctionnoneWill be called if the response from the server is unsuccessfull (codes 4xx and 5xx). The callback gets 1 argument: values which contains all the data entered into the form.
onLoginClickfunctionnoneWill be fired by click on the "Login" button on the registration form.
showLoginButtonbooleantrueShow/Hide the "Login" button on the registration form.
autoLoginbooleantrueDefines whether to automatically sign in after a successful user registration.
onLoginSuccessfunctionnoneSame as onSuccess, but fires after a successful automatic signing.
onLoginFailurefunctionnoneSame as onFailure, but fires if automatical signing fails.

Login

import { Login } from '@config.one/auth'

<Login
    onSuccess={this.onLoginSuccess}
    onFailure={this.onLoginFailure}
    onRegistrationClick={this.onRegistrationClick}
    showRegistrationButton={true}
    showForgotPassword={true}
/>
API
NameTypeDefaultDescription
onSuccessfunctionnoneWill be called if the response from the server is successfull (code 2xx). The callback gets 2 arguments: values and response. values contains all the data entered into the form, response is a standard Response object.
onFailurefunctionnoneWill be called if the response from the server is unsuccessfull (codes 4xx and 5xx). The callback gets 1 argument: values which contains all the data entered into the form.
onRegistrationClickfunctionnoneWill be fired by clickin on the "Registration" button on the registration form.
showRegistrationButtonbooleantrueShow/Hide the "Registration" button on the login form.
showForgotPasswordbooleantrueShow/Hide the "Forgot password" link on the login form.

PhoneVerification

<PhoneVerification
    onSuccess={this.onPhoneVerificationSuccess}
    onFailure={this.onPhoneVerificationFailure}
    onResendSuccess={this.onResendSuccess}
    onResendFailure={this.onResendFailure}
    cooldown={0.5}
    digits={4}
/>
API
NameTypeDefaultDescription
onVerifySuccessfunctionnoneFired on pressing the "Verify" button and if the response from the server is successfull (code 2xx). The callback gets 2 arguments: code and response. code is the OTP entered to the form, response is a standard Response object.
onVerifyFailurefunctionnoneFired if the response from the server is unsuccessfull (codes 4xx and 5xx). The callback gets 1 argument, which is the OTP entered to the form.
onResendSuccessfunctionnoneFired if the response from the server is successfull (code 2xx). The callback gets 1 argument - a standard Response object.
onResendFailurebooleantrueFired if the response from the server is unsuccessfull (codes 4xx and 5xx). The callback gets 1 argument - a standard Response object.
cooldownint1Timeout in minutes to be able to resend the SMS. Could be less than 1 (e.g. 0.5)
digitsint4Number of allowed digits in the code field

ResetPassword & ResetConfirmation

These components should be used in couple. ResetPassword is the form to enter the email to send the resetting link for first time. ResetConfirmation is a confirmation message form with a button, to resend the link.

<ResetPassword
    onSuccess={this.onPasswordResetSuccess}
    onFailure={this.onPasswordResetFailure}
    onLoginClick={this.onLoginClick}
/>

<ResetConfirmation
    onLoginClick={this.onLoginClick}
    onLinkResendSuccess={this.onLinkResendSuccess}
    onLinkResendFailure={this.onLinkResendFailure}
    cooldown={1}
/>
API
NameTypeDefaultDescription
onSuccessfunctionnoneFired on pressing the "Verify" button and if the response from the server is successfull (code 2xx). The callback gets 2 arguments: code and response. code is the OTP entered to the form, response is a standard Response object.
onFailurefunctionnoneFired if the response from the server is unsuccessfull (codes 4xx and 5xx). The callback gets 1 argument, which is the OTP entered to the form.
onLoginClickfunctionnoneWill be fired by clickin on the "Login" button on the registration form.
onLinkResendSuccessfunctionnoneFired if the response from the server is successfull (code 2xx). The callback gets 1 argument - a standard Response object.
onLinkResendFailurebooleantrueFired if the response from the server is unsuccessfull (codes 4xx and 5xx). The callback gets 1 argument - a standard Response object.
cooldownint5Timeout in minutes to be able to resend the link. Could be less than 1 (e.g. 0.5)

AuthChecker

This helper component can be used to decide what to render, depending on the authentication data.

import { Login, AuthChecker } from '@config.one/auth';
import Profile from './Profile';
import UserActivation from './UserActivation';

<AuthChecker>
    {(isLoggedIn, user) => {
        if (isLoggedIn) {
            return user.confirmed ? <Profile user={user}/> : <UserActivation/>;
        } else {
            return <Login />
        }
    }}
</AuthChecker>

License

MIT © Config.one GmbH

1.2.15

4 years ago

1.2.14

4 years ago

1.2.13

4 years ago

1.2.12

4 years ago

1.2.11

4 years ago

1.2.10

4 years ago

1.2.8

4 years ago

1.2.9

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago