quave:accounts-passwordless-react

v2.0.0Published 11 months ago

quave:accounts-passwordless-react

quave:accounts-passwordless-react is a Meteor package that provides a plug-and-play Passwordless authentication system.

Why

It is designed to simplify the process of adding a password less authentication to Meteor apps.

We are using the accounts-passwordless package from Meteor.

We believe we are not reinventing the wheel in this package but what we are doing is like putting together the wheels in the vehicle :).

Installation

meteor add quave:accounts-passwordless-react

Usage

You need to add the Passwordless component to the route where you want to expose the form to login with the token.

The only required property is onEnterToken as you need to send the user to a different location or update the UI after the authentication is done.

1import React from 'react';
2import { useHistory } from 'react-router-dom';
3import { Passwordless } from 'meteor/quave:accounts-passwordless-react';
4
5export const Access = () => {
6  const history = useHistory();
7
8  const onEnterToken = () => {
9    history.push('/');
10    openAlert('Welcome!');
11  };
12  
13  return (
14    <Passwordless
15      onEnterToken={onEnterToken}
16    />
17  );
18};

Tailwind

By default we provide tailwindcss classes, if you want to use them you need to include this comment somewhere in your code to avoid purging these classes:

1// classnames tailwind passwordless:
2// appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm text-sm font-medium text-gray-700 mt-0 flex justify-end text-indigo-600 hover:text-indigo-500 cursor-pointer justify-center px-4 border-transparent text-white bg-indigo-600 hover:bg-indigo-700 focus:ring-2 focus:ring-offset-2 bg-red-50 bg-green-50 p-4 text-red-800 text-green-800 ml-auto pl-3 -mx-1.5 -my-1.5 inline-flex p-1.5 text-red-500 text-green-500 hover:bg-red-100 bg-green-100 focus:ring-offset-red-50 ring-offset-green-50 focus:ring-red-600 ring-green-600 sr-only mt-8 sm:mx-auto sm:w-full sm:max-w-md bg-white py-8 shadow sm:rounded-lg sm:px-10 space-y-6 mt-1

Additional options

You can customize all the texts, for example, if you have a system with multiple languages you could have a getText function as a helper to get the proper text based on the language.

1import React from 'react';
2import { useHistory } from 'react-router-dom';
3import { Passwordless } from 'meteor/quave:accounts-passwordless-react';
4import { getText } from '../../infra/texts';
5
6export const Access = () => {
7  const history = useHistory();
8
9  const onEnterToken = () => {
10    history.push(RoutePaths.HOME);
11    openAlert('Welcome!');
12  };
13  
14  return (
15    <Passwordless
16      onEnterToken={onEnterToken}
17      extra={{ language }}
18      emailLabel={getText('emailLabel')}
19      tokenLabel={getText('tokenLabel')}
20      emailLinkLabel={getText('emailLinkLabel')}
21      tokenLinkLabel={getText('tokenLinkLabel')}
22      requestButtonLabel={getText('requestButtonLabel')}
23      enterButtonLabel={getText('enterButtonLabel')}
24      emailValidationErrorMessage={getText('emailValidationErrorMessage')}
25      tokenValidationErrorMessage={getText('tokenValidationErrorMessage')}
26      getSuccessRequestTokenMessage={({ isNewUser }) =>
27        getText('successRequestTokenMessage', { isNewUser })
28      }
29    />
30  );
31};

We do support even more options, you can see all of them in the code.

By default TailwindCSS classes are added for you but you can override them. You can set up TailwindCSS as in this example.

UI props

1  inputProps = {},
2  emailInputProps = {},
3  tokenInputProps = {},
4  labelProps = {},
5  linkProps = {},
6  buttonProps = {},
7  containerClassName = 'mt-8 sm:mx-auto sm:w-full sm:max-w-md',
8  wrapperClassName = 'bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10',
9  formClassName = 'space-y-6',

Event Handlers props

1  onRequestToken = noop,
2  onRequestError = defaultErrorHandler,
3  onInvalidEmail = noop,
4  onEnterToken = noop,
5  onEnterError = defaultErrorHandler,
6  onInvalidToken = noop,

Accounts data props

1  userData = {}, // will be merged with the user data when a new user is created
2  extra, // will be send in the Accounts.requestLoginTokenForUser call as option

Components props

1  ErrorComponent = Error,
2  SuccessComponent = Success,

If you don't want to use this UI to display messages you can provide null for both.

License

MIT