Skip to content
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

Add generic Userinfo to createFusionAuth for vue sdk #143

Merged
merged 6 commits into from
Jun 25, 2024
Merged
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
7 changes: 4 additions & 3 deletions packages/sdk-vue/src/FusionAuthVuePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { App } from 'vue';
import { App, InjectionKey } from 'vue';
import { FusionAuth, FusionAuthConfig } from './types.ts';
import * as components from './components/index.ts';
import { fusionAuthKey } from './injectionSymbols.ts';
import { createFusionAuth } from './createFusionAuth/index.ts';
import { UserInfo } from '@fusionauth-sdk/core';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be UserInfo from ./types.ts

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#147
Separate issue, though. Here's a ticket #147


type FusionAuthInstantiated = { instance: FusionAuth };
type FusionAuthInstantiated = { instance: FusionAuth<UserInfo | any> };

/**
* Installation method for the FusionAuthVuePlugin.
Expand All @@ -26,7 +27,7 @@ const FusionAuthVuePlugin = {
}

// Register the instance
app.provide(fusionAuthKey, fusionAuth);
app.provide(fusionAuthKey as InjectionKey<FusionAuth>, fusionAuth);

// Register the components
Object.entries(components).forEach(([key, component]) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk-vue/src/composables/useFusionAuth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { inject } from 'vue';
import { InjectionKey, inject } from 'vue';
import { fusionAuthKey } from '#/injectionSymbols';
import type { FusionAuth } from '#/types';
import type { FusionAuth, UserInfo } from '#/types';

export const useFusionAuth = (): FusionAuth => {
const fusionAuth = inject(fusionAuthKey);
export const useFusionAuth = <T = UserInfo>(): FusionAuth<T> => {
const fusionAuth = inject(fusionAuthKey as InjectionKey<FusionAuth<T>>);

if (!fusionAuth) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ describe('createFusionAuth', () => {
});

it('Fetches userInfo', async () => {
const user = { given_name: 'JSON', family_name: 'Bourne' };
const user = {
given_name: 'JSON',
family_name: 'Bourne',
customTrait: 'additional info',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this change? I believe we can use this test to cover what we're implementing here, but I'm not sure this is doing it. Here's an example

};
const mockUserInfoResponse = new Response(JSON.stringify(user), {
status: 200,
});
Expand All @@ -45,6 +49,7 @@ describe('createFusionAuth', () => {
await fusionAuth.getUserInfo();

expect(fusionAuth.userInfo.value).toEqual(user);
expect(fusionAuth.userInfo.value.customTrait).toEqual(user.customTrait);
});

it('Handles a failed user info request', async () => {
Expand Down
10 changes: 6 additions & 4 deletions packages/sdk-vue/src/createFusionAuth/createFusionAuth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ref } from 'vue';
import { Ref, ref } from 'vue';
import { SDKCore } from '@fusionauth-sdk/core';
import { FusionAuth, FusionAuthConfig, UserInfo } from '#/types';

import { NuxtUseCookieAdapter } from './NuxtUseCookieAdapter';

export const createFusionAuth = (config: FusionAuthConfig): FusionAuth => {
export const createFusionAuth = <T = UserInfo>(
config: FusionAuthConfig,
): FusionAuth<T> => {
let cookieAdapter;
if (config.nuxtUseCookie) {
cookieAdapter = new NuxtUseCookieAdapter(config.nuxtUseCookie);
Expand All @@ -19,7 +21,7 @@ export const createFusionAuth = (config: FusionAuthConfig): FusionAuth => {
});

const isLoggedIn = ref<boolean>(core.isLoggedIn);
const userInfo = ref<UserInfo | null>(null);
const userInfo: Ref<T | null> = ref(null);
const isGettingUserInfo = ref<boolean>(false);
const error = ref<Error | null>(null);

Expand All @@ -28,7 +30,7 @@ export const createFusionAuth = (config: FusionAuthConfig): FusionAuth => {
error.value = null;

try {
userInfo.value = await core.fetchUserInfo();
userInfo.value = await core.fetchUserInfo<T>();
return userInfo.value;
} catch (e) {
error.value = e as Error;
Expand Down
5 changes: 1 addition & 4 deletions packages/sdk-vue/src/injectionSymbols.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
import { InjectionKey } from 'vue';
import type { FusionAuth } from './types.ts';

export const fusionAuthKey = Symbol('fusionAuth') as InjectionKey<FusionAuth>;
export const fusionAuthKey = Symbol('fusionAuth');
6 changes: 3 additions & 3 deletions packages/sdk-vue/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export interface UserInfo {
/**
* FusionAuth object provided at app-level by FusionAuthVuePlugin
*/
export interface FusionAuth {
export interface FusionAuth<T = UserInfo> {
/**
* Whether the user is logged in.
*/
Expand All @@ -116,12 +116,12 @@ export interface FusionAuth {
* Internally updates `isFetchingUser` and `userInfo` refs, as well as `error` if the request fails.
* @returns {Promise<UserInfo>}
*/
getUserInfo: () => Promise<UserInfo | undefined>;
getUserInfo: () => Promise<T | undefined>;

/**
* Data fetched from the configured 'me' endpoint.
*/
userInfo: Ref<UserInfo | null>;
userInfo: Ref<T | null>;

/**
* Indicates that the getUserInfo call is unresolved.
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-vue/src/utilsForTests/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ref } from 'vue';
import { InjectionKey, ref } from 'vue';
import { vi } from 'vitest';

import type { FusionAuth } from '..';
Expand All @@ -13,7 +13,7 @@ export const getMockFusionAuth = ({
userInfo = ref(null),
}: Partial<FusionAuth>) => {
return {
key: fusionAuthKey as symbol,
key: fusionAuthKey as InjectionKey<FusionAuth>,
mockedValues: {
login,
logout,
Expand Down