quave:accounts-passwordless-react

v2.1.0Published 8 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, you should include safelist property in your tailwind.config.js

1  safelist: [
2    '-mx-1.5',
3    '-my-1.5',
4    'appearance-none',
5    'bg-green-100',
6    'bg-green-50',
7    'bg-indigo-600',
8    'bg-red-50',
9    'bg-white',
10    'block',
11    'border',
12    'border-gray-300',
13    'border-transparent',
14    'cursor-pointer',
15    'flex',
16    'focus:border-indigo-500',
17    'focus:outline-none',
18    'focus:ring-2',
19    'focus:ring-indigo-500',
20    'focus:ring-offset-2',
21    'focus:ring-offset-red-50',
22    'focus:ring-red-600',
23    'font-medium',
24    'hover:bg-indigo-700',
25    'hover:bg-red-100',
26    'hover:text-indigo-500',
27    'inline-flex',
28    'justify-center',
29    'justify-end',
30    'ml-auto',
31    'mt-0',
32    'mt-1',
33    'mt-8',
34    'p-1.5',
35    'p-4',
36    'pl-3',
37    'placeholder-gray-400',
38    'px-3',
39    'px-4',
40    'py-2',
41    'py-8',
42    'ring-green-600',
43    'ring-offset-green-50',
44    'rounded-md',
45    'shadow',
46    'shadow-sm',
47    'sm:max-w-md',
48    'sm:mx-auto',
49    'sm:px-10',
50    'sm:rounded-lg',
51    'sm:text-sm',
52    'sm:w-full',
53    'space-y-6',
54    'sr-only',
55    'text-gray-700',
56    'text-green-500',
57    'text-green-800',
58    'text-indigo-600',
59    'text-red-500',
60    'text-red-800',
61    'text-sm',
62    'text-white',
63    'w-full',
64  ],

or 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