chore: improving the withBaseURL workflow
This commit is contained in:
@ -697,7 +697,7 @@ export const readTokenFile = async () => {
|
|||||||
export const writeTokenFile = async (token: string) => {
|
export const writeTokenFile = async (token: string) => {
|
||||||
const tokenFilePath = await getTokenFilePath()
|
const tokenFilePath = await getTokenFilePath()
|
||||||
if (err(token)) return Promise.reject(token)
|
if (err(token)) return Promise.reject(token)
|
||||||
const result = window.electron.writeFile(tokenFilePath, token)
|
const result = window.electron.writeFile(tokenFilePath, token)
|
||||||
console.log('token written to disk')
|
console.log('token written to disk')
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
36
src/lib/withBaseURL.test.ts
Normal file
36
src/lib/withBaseURL.test.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import {
|
||||||
|
withAPIBaseURL
|
||||||
|
} from '@src/lib/withBaseURL'
|
||||||
|
|
||||||
|
describe('withBaseURL', () => {
|
||||||
|
/**
|
||||||
|
* running in the development environment
|
||||||
|
* the .env.development should load
|
||||||
|
*/
|
||||||
|
describe('withAPIBaseUrl', () => {
|
||||||
|
it('should return base url', () => {
|
||||||
|
const expected = 'https://api.dev.zoo.dev'
|
||||||
|
const actual = withAPIBaseURL('')
|
||||||
|
expect(actual).toBe(expected)
|
||||||
|
})
|
||||||
|
it('should return base url with /users', () => {
|
||||||
|
const expected = 'https://api.dev.zoo.dev/users'
|
||||||
|
const actual = withAPIBaseURL('/users')
|
||||||
|
expect(actual).toBe(expected)
|
||||||
|
})
|
||||||
|
it ('should return a longer base url with /oauth2/token/revoke', () => {
|
||||||
|
const expected = 'https://api.dev.zoo.dev/oauth2/token/revoke'
|
||||||
|
const actual = withAPIBaseURL('/oauth2/token/revoke')
|
||||||
|
expect(actual).toBe(expected)
|
||||||
|
})
|
||||||
|
it('should ensure base url does not have ending slash', () => {
|
||||||
|
const expected = 'https://api.dev.zoo.dev'
|
||||||
|
const actual = withAPIBaseURL('')
|
||||||
|
expect(actual).toBe(expected)
|
||||||
|
const expectedEndsWith = expected[expected.length-1]
|
||||||
|
const actualEndsWith = actual[actual.length-1]
|
||||||
|
expect(actual).toBe(expected)
|
||||||
|
expect(actualEndsWith).toBe(expectedEndsWith)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@ -1,5 +1,5 @@
|
|||||||
import { VITE_KC_API_BASE_URL } from '@src/env'
|
import { VITE_KC_API_BASE_URL } from '@src/env'
|
||||||
|
|
||||||
export default function withBaseUrl(path: string): string {
|
export function withAPIBaseURL (path: string) : string {
|
||||||
return VITE_KC_API_BASE_URL + path
|
return VITE_KC_API_BASE_URL + path
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,7 @@ import {
|
|||||||
import { isDesktop } from '@src/lib/isDesktop'
|
import { isDesktop } from '@src/lib/isDesktop'
|
||||||
import { markOnce } from '@src/lib/performance'
|
import { markOnce } from '@src/lib/performance'
|
||||||
import {
|
import {
|
||||||
default as withBaseURL,
|
withAPIBaseURL
|
||||||
default as withBaseUrl,
|
|
||||||
} from '@src/lib/withBaseURL'
|
} from '@src/lib/withBaseURL'
|
||||||
import { ACTOR_IDS } from '@src/machines/machineConstants'
|
import { ACTOR_IDS } from '@src/machines/machineConstants'
|
||||||
|
|
||||||
@ -38,12 +37,13 @@ export const TOKEN_PERSIST_KEY = 'TOKEN_PERSIST_KEY'
|
|||||||
const persistedCookie = getCookie(COOKIE_NAME)
|
const persistedCookie = getCookie(COOKIE_NAME)
|
||||||
const persistedLocalStorage = localStorage?.getItem(TOKEN_PERSIST_KEY) || ''
|
const persistedLocalStorage = localStorage?.getItem(TOKEN_PERSIST_KEY) || ''
|
||||||
const persistedDevToken = VITE_KC_DEV_TOKEN
|
const persistedDevToken = VITE_KC_DEV_TOKEN
|
||||||
export const persistedToken = persistedDevToken || persistedCookie || persistedLocalStorage
|
export const persistedToken =
|
||||||
|
persistedDevToken || persistedCookie || persistedLocalStorage
|
||||||
console.log('Initial persisted token')
|
console.log('Initial persisted token')
|
||||||
console.table([
|
console.table([
|
||||||
['cookie', !!persistedCookie],
|
['cookie', !!persistedCookie],
|
||||||
['local storage', !!persistedLocalStorage],
|
['local storage', !!persistedLocalStorage],
|
||||||
['dev token', !!persistedDevToken]
|
['dev token', !!persistedDevToken],
|
||||||
])
|
])
|
||||||
|
|
||||||
export const authMachine = setup({
|
export const authMachine = setup({
|
||||||
@ -141,7 +141,7 @@ export const authMachine = setup({
|
|||||||
|
|
||||||
async function getUser(input: { token?: string }) {
|
async function getUser(input: { token?: string }) {
|
||||||
const token = await getAndSyncStoredToken(input)
|
const token = await getAndSyncStoredToken(input)
|
||||||
const url = withBaseURL('/user')
|
const url = withAPIBaseURL('/user')
|
||||||
const headers: { [key: string]: string } = {
|
const headers: { [key: string]: string } = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
}
|
}
|
||||||
@ -201,13 +201,11 @@ async function getAndSyncStoredToken(input: {
|
|||||||
// dev mode
|
// dev mode
|
||||||
if (VITE_KC_DEV_TOKEN) {
|
if (VITE_KC_DEV_TOKEN) {
|
||||||
console.log('Token used for authentication')
|
console.log('Token used for authentication')
|
||||||
console.table([
|
console.table([['dev token', !!VITE_KC_DEV_TOKEN]])
|
||||||
['dev token', !!VITE_KC_DEV_TOKEN],
|
|
||||||
])
|
|
||||||
return VITE_KC_DEV_TOKEN
|
return VITE_KC_DEV_TOKEN
|
||||||
}
|
}
|
||||||
|
|
||||||
const inputToken = input.token && input.token !== '' ? input.token : ''
|
const inputToken = input.token && input.token !== '' ? input.token : ''
|
||||||
const cookieToken = getCookie(COOKIE_NAME)
|
const cookieToken = getCookie(COOKIE_NAME)
|
||||||
const localStorageToken = localStorage?.getItem(TOKEN_PERSIST_KEY) || ''
|
const localStorageToken = localStorage?.getItem(TOKEN_PERSIST_KEY) || ''
|
||||||
const token = inputToken || cookieToken || localStorageToken
|
const token = inputToken || cookieToken || localStorageToken
|
||||||
@ -217,7 +215,7 @@ async function getAndSyncStoredToken(input: {
|
|||||||
['persisted token', !!inputToken],
|
['persisted token', !!inputToken],
|
||||||
['cookie', !!cookieToken],
|
['cookie', !!cookieToken],
|
||||||
['local storage', !!localStorageToken],
|
['local storage', !!localStorageToken],
|
||||||
['dev token', !!VITE_KC_DEV_TOKEN]
|
['dev token', !!VITE_KC_DEV_TOKEN],
|
||||||
])
|
])
|
||||||
if (token) {
|
if (token) {
|
||||||
// has just logged in, update storage
|
// has just logged in, update storage
|
||||||
@ -244,7 +242,7 @@ async function logout() {
|
|||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
try {
|
try {
|
||||||
await fetch(withBaseUrl('/oauth2/token/revoke'), {
|
await fetch(withAPIBaseURL('/oauth2/token/revoke'), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
headers: {
|
headers: {
|
||||||
@ -267,7 +265,7 @@ async function logout() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fetch(withBaseUrl('/logout'), {
|
return fetch(withAPIBaseURL('/logout'), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user