-
Search
+
+
+ Search
+
diff --git a/pages/index.vue b/pages/index.vue
index 4a461ccc..457da75d 100644
--- a/pages/index.vue
+++ b/pages/index.vue
@@ -1,5 +1,8 @@
-
+
Welcome to Name X!
diff --git a/plugins/firebase.client.ts b/plugins/firebase.client.ts
index 1a110d43..828387c3 100644
--- a/plugins/firebase.client.ts
+++ b/plugins/firebase.client.ts
@@ -1,6 +1,7 @@
import { initializeApp } from 'firebase/app'
import { getRemoteConfig, fetchAndActivate } from 'firebase/remote-config'
import { getAnalytics } from 'firebase/analytics'
+import { getAuth, type Auth } from 'firebase/auth'
import { FeatureFlags } from '@/util/constants'
export default defineNuxtPlugin(async (nuxtApp) => {
@@ -17,6 +18,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
const analytics = getAnalytics(app)
const remoteConfig = getRemoteConfig(app)
+ const auth: Auth = getAuth(app)
remoteConfig.settings.fetchTimeoutMillis = 60000
remoteConfig.settings.minimumFetchIntervalMillis = 60000
@@ -34,4 +36,10 @@ export default defineNuxtPlugin(async (nuxtApp) => {
nuxtApp.provide('remoteConfig', remoteConfig)
console.info(remoteConfig)
+
+ return {
+ provide: {
+ auth: auth
+ }
+ }
})
diff --git a/plugins/keycloak.client.ts b/plugins/keycloak.client.ts
deleted file mode 100644
index cd271bc8..00000000
--- a/plugins/keycloak.client.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import Keycloak from 'keycloak-js'
-
-export default defineNuxtPlugin(async (_nuxtApp) => {
- const config = useRuntimeConfig()
- const keycloak = new Keycloak({
- url: config.public.keycloakAuthUrl,
- realm: config.public.keycloakRealm,
- clientId: config.public.keycloakClientId,
- })
-
- try {
- const authenticated = await keycloak.init({
- onLoad: 'login-required',
- responseMode: 'query',
- pkceMethod: 'S256',
- })
- console.log(`[Keycloak] User authenticated?: ${authenticated}`)
- } catch (error) {
- console.error('Failed to initialize Keycloak adapter: ', error)
- }
-
- return {
- provide: {
- auth: keycloak,
- userProfile: await keycloak.loadUserProfile(),
- },
- }
-})
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 60bf4e7f..57e2a97f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -38,9 +38,6 @@ dependencies:
flush-promises:
specifier: ^1.0.2
version: 1.0.2
- keycloak-js:
- specifier: ^21.1.1
- version: 21.1.2
launchdarkly-js-client-sdk:
specifier: ^3.1.3
version: 3.1.3
@@ -6756,13 +6753,6 @@ packages:
verror: 1.10.0
dev: true
- /keycloak-js@21.1.2:
- resolution: {integrity: sha512-+6r1BvmutWGJBtibo7bcFbHWIlA7XoXRCwcA4vopeJh59Nv2Js0ju2u+t8AYth+C6Cg7/BNfO3eCTbsl/dTBHw==}
- dependencies:
- base64-js: 1.5.1
- js-sha256: 0.9.0
- dev: false
-
/keygrip@1.1.0:
resolution: {integrity: sha512-iYSchDJ+liQ8iwbSI2QqsQOvqv58eJCEanyJPJi+Khyu8smkcKSFUCbPwzFcL7YVtZ6eONjqRX/38caJ7QjRAQ==}
engines: {node: '>= 0.6'}
diff --git a/store/user-cache.ts b/store/user-cache.ts
new file mode 100644
index 00000000..cee571bc
--- /dev/null
+++ b/store/user-cache.ts
@@ -0,0 +1,31 @@
+import { defineStore } from 'pinia'
+import { useNuxtApp } from '#app'
+import { OAuthProvider, getRedirectResult, onAuthStateChanged } from 'firebase/auth'
+
+export const useUserStore = defineStore('user-cache', () => {
+ const authenticated = ref(false)
+ const userName = ref('')
+ const token = ref('')
+ // eslint-disable-next-line require-jsdoc
+ async function setUser () {
+ const { $auth } = useNuxtApp()
+ $auth.onAuthStateChanged(async function (user) {
+ if (user) {
+ userName.value = user.displayName ? user.displayName : ''
+ authenticated.value = true
+ const { $auth } = useNuxtApp()
+ const userCredential = await getRedirectResult($auth)
+ if (userCredential) {
+ const credentials = OAuthProvider.credentialFromResult(userCredential)
+ token.value = credentials?.accessToken ? credentials.accessToken : ''
+ }
+ } else {
+ userName.value = ''
+ authenticated.value = false
+ token.value = ''
+ }
+ })
+ }
+ return { authenticated, userName, token, setUser }
+}
+)
diff --git a/util/firebase-auth.ts b/util/firebase-auth.ts
new file mode 100644
index 00000000..15b2474b
--- /dev/null
+++ b/util/firebase-auth.ts
@@ -0,0 +1,20 @@
+import { signOut, OAuthProvider, signInWithRedirect } from 'firebase/auth'
+
+/**
+ * perform user log out
+ */
+export async function logout () {
+ const { $auth } = useNuxtApp()
+ signOut($auth)
+ window.location.assign('/')
+}
+
+/**
+ * perform user log in
+ */
+export async function login () {
+ const config = useRuntimeConfig().public
+ const provider = new OAuthProvider(config.gcpIDP)
+ const { $auth } = useNuxtApp()
+ signInWithRedirect($auth, provider)
+}
diff --git a/util/namex-api.ts b/util/namex-api.ts
index 86d15ed7..bacbe250 100644
--- a/util/namex-api.ts
+++ b/util/namex-api.ts
@@ -1,40 +1,35 @@
+import { useUserStore } from '~/store/user-cache'
import type { Transaction } from '~/types'
/**
- * Retrieve the Keycloak session token, refreshing to make it valid if necessary.
- * @returns the session token
+ * Make a call to the NameX API and return the response json
+ * @param url The url to fetch
+ * @param options call options
+ * @return The json response object
*/
-async function getToken(): Promise {
- const { $auth } = useNuxtApp()
- return await $auth
- .updateToken(30)
- .then((_refreshed) => {
- return $auth.token
- })
- .catch(async (error) => {
- console.error(`Failed to get session token: ${error}`)
- return undefined
- })
-}
+export async function callNamexApi (url: URL, options?: object): Promise {
+ const userStore = useUserStore()
+ let response: Response = new Response()
+ const token = userStore.token
-/**
- * Make an HTTP request to a given Namex API URL.
- */
-async function callNamexApi(url: URL, options?: object) {
- const token = await getToken()
- return fetch(url, {
- headers: {
- Authorization: `Bearer ${token}`,
- },
- ...(options ? options : {}),
- })
+ if (token.length > 0) {
+ const token = userStore.token
+
+ response = await fetch(url, {
+ headers: {
+ Authorization: `Bearer ${token}`
+ },
+ ...(options || {})
+ })
+ }
+ return response
}
/**
* Returns the full NameX API url given an endpoint
* @param endpoint The API endpoint (including the starting slash '/')
*/
-export function getNamexApiUrl(endpoint: string): URL {
+export function getNamexApiUrl (endpoint: string): URL {
const config = useRuntimeConfig().public
return new URL(
config.namexAPIVersion + endpoint,
@@ -45,40 +40,40 @@ export function getNamexApiUrl(endpoint: string): URL {
/**
* Make a GET request to the given NameX API URL and return the response json.
* @param url the url to fetch
- * @returns the json response object
+ * @return the json response object
*/
-export async function getNamexObject(url: URL): Promise {
+export async function getNamexObject (url: URL): Promise {
return (await callNamexApi(url)).json()
}
/**
* Get a name request object given its NR number.
*/
-export async function getNameRequest(nrNumber: string) {
+export async function getNameRequest (nrNumber: string) {
return callNamexApi(getNamexApiUrl(`/requests/${nrNumber}`))
}
/**
* Patch the name request with the given NR number with a patch object.
*/
-export async function patchNameRequest(nrNumber: string, patch: object) {
+export async function patchNameRequest (nrNumber: string, patch: object) {
const url = getNamexApiUrl(`/requests/${nrNumber}`)
return await callNamexApi(url, {
method: 'PATCH',
- body: JSON.stringify(patch),
+ body: JSON.stringify(patch)
})
}
-export async function getTransactions(
+export async function getTransactions (
nrNumber: string
): Promise> {
return getNamexObject(getNamexApiUrl(`/events/${nrNumber}`))
}
-export async function getBusiness(corpNum: string) {
+export async function getBusiness (corpNum: string) {
return callNamexApi(getNamexApiUrl(`/businesses/${corpNum}`))
}
-export async function getCorporation(corpNum: string) {
+export async function getCorporation (corpNum: string) {
return callNamexApi(getNamexApiUrl(`/corporations/${corpNum}`))
}