Skip to content

Commit

Permalink
Merl 1698 include the viewer in use auth hook (#1744)
Browse files Browse the repository at this point in the history
* Added viewer to useAuth with commonly used variables
  • Loading branch information
TeresaGobble authored Jan 24, 2024
1 parent 664e32a commit e7745b8
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-readers-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@faustwp/core": patch
---

Exposes viewer data for use during the useAuth hook call.
68 changes: 68 additions & 0 deletions packages/faustwp-core/src/hooks/useAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import trim from 'lodash/trim.js';
import defaults from 'lodash/defaults.js';
import { useEffect, useState } from 'react';
import { gql } from '@apollo/client';
import {
ensureAuthorization,
EnsureAuthorizationOptions,
} from '../auth/index.js';
import { getApolloAuthClient } from '../client.js';

type RedirectStrategyConfig = {
strategy: 'redirect';
Expand All @@ -19,6 +21,26 @@ type LocalStrategyConfig = {
skip?: boolean;
};

type ViewerType = {
name?: string;
username?: string;
capabilities?: string[];
databaseId?: number;
description?: string;
email?: string;
firstName?: string;
id?: number;
lastName?: string;
nickname?: string;
locale?: string;
registeredDate?: string;
slug?: string;
templates?: string[];
uri?: string;
url?: string;
userId?: number;
};

export type UseAuthConfig = RedirectStrategyConfig | LocalStrategyConfig;

export function useAuth(_config?: UseAuthConfig) {
Expand All @@ -38,6 +60,7 @@ export function useAuth(_config?: UseAuthConfig) {
const [isReady, setIsReady] = useState<boolean>(false);
const [loginUrl, setLoginUrl] = useState<string | null>(null);
const [called, setCalled] = useState<boolean>(false);
const [viewer, setViewer] = useState<ViewerType | null>(null);

useEffect(() => {
if (config.skip === true) {
Expand Down Expand Up @@ -114,9 +137,54 @@ export function useAuth(_config?: UseAuthConfig) {
}, 200);
}, [isReady, isAuthenticated, loginUrl, config]);

/**
* Expose viewer options to the toolbar if the user is authenticated.
*/
useEffect(() => {
if (config.skip === true) {
return;
}

if (isAuthenticated !== true) {
return;
}

(async () => {
const client = getApolloAuthClient();

const { data } = await client.query({
query: gql`
query GetFaustViewer {
viewer {
name
username
capabilities
databaseId
description
email
firstName
id
lastName
nickname
locale
registeredDate
slug
templates
uri
url
userId
}
}
`,
});
setViewer(data.viewer as ViewerType | null);
})();
}, [isAuthenticated, config.skip]);

return {
isAuthenticated,
isReady,
loginUrl,
viewer,
};
}
36 changes: 36 additions & 0 deletions packages/faustwp-core/tests/hooks/useAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ describe('useAuth hook', () => {
status: 200,
body: JSON.stringify({ accessToken: 'at', refreshToken: 'rt' }),
});
fetchMock.get(`http://headless.local/index.php?graphql&query=query%20GetFaustViewer%20%7B%0A%20%20viewer%20%7B%0A%20%20%20%20name%0A%20%20%20%20username%0A%20%20%20%20capabilities%0A%20%20%20%20databaseId%0A%20%20%20%20description%0A%20%20%20%20email%0A%20%20%20%20firstName%0A%20%20%20%20id%0A%20%20%20%20lastName%0A%20%20%20%20nickname%0A%20%20%20%20locale%0A%20%20%20%20registeredDate%0A%20%20%20%20slug%0A%20%20%20%20templates%0A%20%20%20%20uri%0A%20%20%20%20url%0A%20%20%20%20userId%0A%20%20%20%20__typename%0A%20%20%7D%0A%7D&operationName=GetFaustViewer&variables=%7B%7D`, {
status: 200,
body: JSON.stringify({
data: {
viewer: {
name: 'admin',
username: 'admin',
capabilities: ['administrator'],
databaseId: 1,
description: '',
email: '',
},
},
}),
});

const { result, waitForNextUpdate } = renderHook(() => useAuth());

Expand Down Expand Up @@ -111,6 +126,21 @@ describe('useAuth hook', () => {
refreshToken: 'rt',
}),
});
fetchMock.get(`http://headless.local/index.php?graphql&query=query%20GetFaustViewer%20%7B%0A%20%20viewer%20%7B%0A%20%20%20%20name%0A%20%20%20%20username%0A%20%20%20%20capabilities%0A%20%20%20%20databaseId%0A%20%20%20%20description%0A%20%20%20%20email%0A%20%20%20%20firstName%0A%20%20%20%20id%0A%20%20%20%20lastName%0A%20%20%20%20nickname%0A%20%20%20%20locale%0A%20%20%20%20registeredDate%0A%20%20%20%20slug%0A%20%20%20%20templates%0A%20%20%20%20uri%0A%20%20%20%20url%0A%20%20%20%20userId%0A%20%20%20%20__typename%0A%20%20%7D%0A%7D&operationName=GetFaustViewer&variables=%7B%7D`, {
status: 200,
body: JSON.stringify({
data: {
viewer: {
name: 'admin',
username: 'admin',
capabilities: ['administrator'],
databaseId: 1,
description: '',
email: '',
},
},
}),
});

const { result, waitForNextUpdate } = renderHook(() => useAuth());

Expand Down Expand Up @@ -140,4 +170,10 @@ describe('useAuth hook', () => {

fetchMock.restore();
});

it('returns the viewer object', () => {
const { result } = renderHook(() => useAuth());
expect(result.current.viewer).toBeDefined();
});

});

0 comments on commit e7745b8

Please sign in to comment.