Skip to content

Use generic language for Authenticatable instead of User #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 44 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Authenticate your react applications easily with react-query.

## Introduction

Thanks to react-query we have been able to reduce our codebases by a lot by caching server state with it. However, we still have to think about where to store the user data. The user data can be considered as a global application state because we need to access it from lots of places in the application. On the other hand, it is also a server state since all the user data is expected to arrive from a server. With this library, we can manage user authentication in an easy way. It is agnostic of the method you are using for authenticating your application, it can be adjusted according to the API it is being used against. It just needs the configuration to be provided and the rest will be set up automatically.
Thanks to react-query we have been able to reduce our codebases by a lot by caching server state with it. However, we still have to think about where to store the authenticatables (typically a "user") data. The authenticatables data can be considered as a global application state because we need to access it from lots of places in the application. On the other hand, it is also a server state since all the authenticatable data is expected to arrive from a server. With this library, we can manage authentication in an easy way. It is agnostic of the method you are using for authenticating your application, it can be adjusted according to the API it is being used against. It just needs the configuration to be provided and the rest will be set up automatically.

## Table of Contents

Expand Down Expand Up @@ -52,9 +52,9 @@ First of all, `AuthProvider` and `useAuth` must be initialized and exported.
// src/lib/auth.ts

import { initReactQueryAuth } from 'react-query-auth';
import { loginUser, loginFn, registerFn, logoutFn } from '...';
import { loginAuthenticatable, loginFn, registerFn, logoutFn } from '...';

interface User {
interface Authenticatable {
id: string;
name: string;
}
Expand All @@ -64,14 +64,14 @@ interface Error {
}

const authConfig = {
loadUser,
loadAuthenticatable,
loginFn,
registerFn,
logoutFn,
};

export const { AuthProvider, useAuth } = initReactQueryAuth<
User,
Authenticatable,
Error,
LoginCredentials,
RegisterCredentials
Expand Down Expand Up @@ -100,14 +100,15 @@ export const App = () => {
};
```

Then the user data is accessible from any component rendered inside the provider via the `useAuth` hook:
Then the authenticatables data is accessible from any component rendered inside the provider via the `useAuth` hook:

```ts
// src/components/UserInfo.tsx
import { useAuth } from 'src/lib/auth';

export const UserInfo = () => {
const { user } = useAuth();
// PROTIP: You can use destructuring to name your authenticatable to something that better serves your app
const { authenticatable: user } = useAuth();
return <div>My Name is {user.name}</div>;
};
```
Expand All @@ -120,9 +121,12 @@ Function that initializes and returns `AuthProvider`, `AuthConsumer` and `useAut

```ts
// src/lib/auth.ts
export const { AuthProvider, useAuth } = initReactQueryAuth<User, Error>({
export const { AuthProvider, useAuth } = initReactQueryAuth<
Authenticatable,
Error
>({
key,
loadUser,
loadAuthenticatable,
loginFn,
registerFn,
logoutFn,
Expand All @@ -137,36 +141,36 @@ export const { AuthProvider, useAuth } = initReactQueryAuth<User, Error>({
- `key: string`

- key that is being used by react-query.
- defaults to `'auth-user'`
- defaults to `'auth-authenticatable'`

- `loadUser: (data:any) => Promise<User>`
- `loadAuthenticatable: (data:any) => Promise<Authenticatable>`

- **Required**
- function that handles user profile fetching
- function that handles authenticatable profile fetching

- `loginFn: (data:any) => Promise<User>`
- `loginFn: (data:any) => Promise<Authenticatable>`

- **Required**
- function that handles user login
- function that handles authenticatable login

- `registerFn: (data:any) => Promise<User>`
- `registerFn: (data:any) => Promise<Authenticatable>`

- **Required**
- function that handles user registration
- function that handles authenticatable registration

- `logoutFn: (data:unknown) => Promise<any>`

- **Required**
- function that handles user logout
- function that handles authenticatable logout

- `logoutFn: () => Promise<any>`

- **Required**
- function that handles user logout
- function that handles authenticatable logout

- `waitInitial: boolean`

- Flag for checking if the provider should show `LoaderComponent` while fetching the user. If set to `false` it will fetch the user in the background.
- Flag for checking if the provider should show `LoaderComponent` while fetching the authenticatable. If set to `false` it will fetch the authenticatable in the background.
- defaults to `true`

- `LoaderComponent: () => React.ReactNode`
Expand Down Expand Up @@ -199,35 +203,42 @@ export const App = () => {

#### `useAuth`

The hook allows access of the user data across the app.
The hook allows access of the authenticatable data across the app.

```ts
import { useAuth } from 'src/lib/auth';

export const UserInfo = () => {
const { user, login, logout, register, error, refetch } = useAuth();
const {
authenticatable: user,
login,
logout,
register,
error,
refetch,
} = useAuth();
return <div>My Name is {user.name}</div>;
};
```

##### returns context value:

- `user: User | undefined`
- `authenticatable: Authenticatable | undefined`

- user data that was retrieved from server
- authenticatables data that was retrieved from server
- type can be provided by passing it to `initReactQueryAuth` generic

- `login: (variables: TVariables, { onSuccess, onSettled, onError }) => Promise<TData>`

- function to login the user
- function to login the authenticatable

- `logout: (variables: TVariables, { onSuccess, onSettled, onError }) => Promise<TData>`

- function to logout the user
- function to logout the authenticatable

- `register: (variables: TVariables, { onSuccess, onSettled, onError }) => Promise<TData>`

- function to register the user
- function to register the authenticatable

- `isLoggingIn: boolean`

Expand All @@ -241,9 +252,9 @@ export const UserInfo = () => {

- checks if is registering in is in progress

- `refetchUser: (options?: RefetchOptions | undefined) => Promise<QueryObserverResult<User, Error>>`
- `refetchAuthenticatable: (options?: RefetchOptions | undefined) => Promise<QueryObserverResult<Authenticatable, Error>>`

- function for refetching user data. this can also be done by invalidating its query by `key`
- function for refetching authenticatables data. this can also be done by invalidating its query by `key`

- `error: Error | null`
- error object
Expand All @@ -262,7 +273,11 @@ export const App = () => {
<ReactQueryProvider>
<AuthProvider>
<AuthConsumer>
{({ user }) => <div>{JSON.stringify(user) || 'No User Found'}</div>}
{({ authenticatable }) => (
<div>
{JSON.stringify(authenticatable) || 'No Authenticatable Found'}
</div>
)}
</AuthConsumer>
</AuthProvider>
</ReactQueryProvider>
Expand Down
6 changes: 3 additions & 3 deletions sample-app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import { Auth } from './components/Auth';
import { UserInfo } from './components/UserInfo';
import { AuthenticatableInfo } from './components/AuthenticatableInfo';
import { useAuth } from './lib/auth';

export function App() {
const { user } = useAuth();
return user ? <UserInfo /> : <Auth />;
const { authenticatable } = useAuth();
return authenticatable ? <AuthenticatableInfo /> : <Auth />;
}
6 changes: 3 additions & 3 deletions sample-app/api.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { storage } from './utils';

interface AuthResponse {
user: User;
authenticatable: Authenticatable;
jwt: string;
}

export interface User {
export interface Authenticatable {
id: string;
email: string;
name?: string;
Expand All @@ -21,7 +21,7 @@ export async function handleApiResponse(response) {
}
}

export async function getUserProfile() {
export async function getAuthenticatableProfile() {
return await fetch('/auth/me', {
headers: {
Authorization: storage.getToken(),
Expand Down
12 changes: 12 additions & 0 deletions sample-app/components/AuthenticatableInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { useAuth } from '../lib/auth';

export function AuthenticatableInfo() {
const { authenticatable, logout } = useAuth();
return (
<div>
Welcome {authenticatable?.name}
<button onClick={() => logout()}>Log Out</button>
</div>
);
}
2 changes: 1 addition & 1 deletion sample-app/components/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function Login() {
onSubmit={async e => {
e.preventDefault();
try {
await login(values);
await login(values as LoginCredentials);
} catch (err) {
setError(err);
}
Expand Down
2 changes: 1 addition & 1 deletion sample-app/components/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function Register() {
onSubmit={async e => {
e.preventDefault();
try {
await register(values);
await register(values as RegisterCredentials);
} catch (err) {
setError(err);
}
Expand Down
11 changes: 0 additions & 11 deletions sample-app/components/UserInfo.tsx

This file was deleted.

32 changes: 16 additions & 16 deletions sample-app/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { initReactQueryAuth } from '../../src';
import {
getUserProfile,
getAuthenticatableProfile,
registerWithEmailAndPassword,
loginWithEmailAndPassword,
User,
Authenticatable,
} from '../api';
import { storage } from '../utils';

Expand All @@ -18,47 +18,47 @@ export type RegisterCredentials = {
password: string;
};

async function handleUserResponse(data) {
const { jwt, user } = data;
async function handleAuthenticatableResponse(data) {
const { jwt, authenticatable } = data;
storage.setToken(jwt);
return user;
return authenticatable;
}

async function loadUser() {
let user = null;
async function loadAuthenticatable() {
let authenticatable = null;

if (storage.getToken()) {
const data = await getUserProfile();
user = data;
const data = await getAuthenticatableProfile();
authenticatable = data;
}
return user;
return authenticatable;
}

async function loginFn(data: LoginCredentials) {
const response = await loginWithEmailAndPassword(data);
const user = await handleUserResponse(response);
return user;
const authenticatable = await handleAuthenticatableResponse(response);
return authenticatable;
}

async function registerFn(data: RegisterCredentials) {
const response = await registerWithEmailAndPassword(data);
const user = await handleUserResponse(response);
return user;
const authenticatable = await handleAuthenticatableResponse(response);
return authenticatable;
}

async function logoutFn() {
await storage.clearToken();
}

const authConfig = {
loadUser,
loadAuthenticatable,
loginFn,
registerFn,
logoutFn,
};

const { AuthProvider, AuthConsumer, useAuth } = initReactQueryAuth<
User,
Authenticatable,
any,
LoginCredentials,
RegisterCredentials
Expand Down
19 changes: 11 additions & 8 deletions sample-app/mock/db.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { User } from '../api';
import { Authenticatable } from '../api';

const users: Record<string, User> = JSON.parse(
window.localStorage.getItem('db_users') || '{}'
const authenticatables: Record<string, Authenticatable> = JSON.parse(
window.localStorage.getItem('db_authenticatables') || '{}'
);

const blackListEmails = ['[email protected]', '[email protected]'];

export function setUser(data: User) {
export function setAuthenticatable(data: Authenticatable) {
if (data?.email && !blackListEmails.includes(data?.email)) {
users[data.email] = data;
window.localStorage.setItem('db_users', JSON.stringify(users));
authenticatables[data.email] = data;
window.localStorage.setItem(
'db_authenticatables',
JSON.stringify(authenticatables)
);
return data;
} else {
return null;
}
}

export function getUser(email: string) {
return users[email];
export function getAuthenticatable(email: string) {
return authenticatables[email];
}
Loading