thingco / shared-frontend-libs

0 stars 0 forks source link

Feat/authentication refactor #68

Closed DanCouper closed 3 years ago

DanCouper commented 3 years ago

https://user-images.githubusercontent.com/573694/131142246-aa3b87e7-e0c1-4f3a-846a-c7b94aa164e1.mp4

To view a test of this:

yarn
yarn workspace @thingco/logger build
yarn workspace @thingco/authentication watch

This will run a server listening on port 3000. It's set up to use OTP at the minute, I need to add username/password to the test app, but that also needs a way to switch configs so putting it off until I get the integration test code completed as I'm just reusing all the test app code for it.

resolves #56 resolves #50 resolves #14 resolves #57

So:

The library does not know anything about how the callbacks work, only what shape the functions are to be. So for example

import { Auth } from "@aws-amplify/auth";
import React, {useState} from "react";
import { useAwaitingOtpInput } from "@thingco/authentication";

const AwaitingOtpUsernameInput = () => {
  const { error, isActive, isLoading, validateUsername } = useAwaitingOtpUsernameInput(Auth.signIn);
  const [username, setUsername] = useState("")

  if (!isActive) return null;

  return (
    <form onSubmit={(e) => {
      e.preventDefault()
      validateUsername(username)
    }>
      <fieldset disabled={isLoading}>
        <input type="text" onChange={(e) => setUsername(e.target.value)} value={username} />
        { error && <p>Error: {error}</p>}
        <button type="submit">Submit username</button>
      </fieldset>
    </form>
  );
};