Skip to content

Commit

Permalink
breaking(): keycloak is now a reactive reference to the keycloak-js a…
Browse files Browse the repository at this point in the history
…dapter instance
  • Loading branch information
JoseGoncalves committed Jun 17, 2024
1 parent 9e4b6ba commit a199c5d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ import { useKeycloak } from '@josempgon/vue-keycloak'

const {
// Reactive State
keycloak,
isAuthenticated,
isPending,
hasFailed,
Expand All @@ -204,9 +205,6 @@ const {
roles,
resourceRoles,

// Object Instances
keycloak,

// Functions
hasRoles,
hasResourceRoles,
Expand All @@ -216,6 +214,7 @@ const {

| State | Type | Description |
| --------------- | ------------------------------------------------------ | ------------------------------------------------------------------- |
| keycloak | `Ref<`[`Keycloak`][Instance]`>` | Instance of the keycloak-js adapter. |
| isAuthenticated | `Ref<boolean>` | If `true` the user is authenticated. |
| isPending | `Ref<boolean>` | If `true` the authentication request is still pending. |
| hasFailed | `Ref<boolean>` | If `true` authentication request has failed. |
Expand All @@ -226,12 +225,6 @@ const {
| roles | `Ref<string[]>` | List of the user's roles. |
| resourceRoles | `Ref<Record<string, string[]>` | List of the user's roles in specific resources. |

#### Object Instances

| Instance | Type | Description |
| --------------- | --------------------------------------- | --------------------------------------------------------------- |
| keycloak | [`Keycloak`][Instance] | Instance of the keycloak-js adapter. |

#### Functions

| Function | Type | Description |
Expand Down
6 changes: 2 additions & 4 deletions src/composable.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type Keycloak from 'keycloak-js'
import type { KeycloakTokenParsed } from 'keycloak-js'
import { toRefs, Ref } from 'vue'
import { getKeycloak } from './keycloak'
import { KeycloakInstance } from './keycloak'
import { KeycloakState, state } from './state'
import { isNil } from './utils'

export interface KeycloakComposable {
keycloak: Ref<KeycloakInstance>
isAuthenticated: Ref<boolean>
hasFailed: Ref<boolean>
isPending: Ref<boolean>
Expand All @@ -15,15 +15,13 @@ export interface KeycloakComposable {
userId: Ref<string>
roles: Ref<string[]>
resourceRoles: Ref<Record<string, string[]>>
keycloak: Keycloak
hasRoles: (roles: string[]) => boolean
hasResourceRoles: (roles: string[], resource: string) => boolean
}

export const useKeycloak = (): KeycloakComposable => {
return {
...toRefs<KeycloakState>(state),
keycloak: getKeycloak(),
hasRoles: (roles: string[]) =>
!isNil(roles) && state.isAuthenticated && roles.every(role => state.roles.includes(role)),
hasResourceRoles: (roles: string[], resource: string) =>
Expand Down
3 changes: 2 additions & 1 deletion src/keycloak.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { defaultInitConfig } from './const'
jest.mock('keycloak-js', () => jest.fn())
jest.mock('./state', () => {
return {
setKeycloak: jest.fn(),
setToken: jest.fn(),
hasFailed: jest.fn(),
isPending: jest.fn(),
Expand Down Expand Up @@ -64,7 +65,7 @@ describe('keyckoak', () => {
})
})

describe('createKeycloak & getKeycloak', () => {
describe('createKeycloak', () => {
test('should define a new keycloak instance and return it', () => {
const result = createKeycloak(keycloakConfig)

Expand Down
9 changes: 3 additions & 6 deletions src/keycloak.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import Keycloak from 'keycloak-js'
import type { KeycloakConfig, KeycloakInitOptions } from 'keycloak-js'
import { hasFailed, isAuthenticated, isPending, setToken } from './state'
import { hasFailed, isAuthenticated, isPending, setKeycloak, setToken } from './state'
import { isNil } from './utils'

type KeycloakInstance = Keycloak | undefined
export type KeycloakInstance = Keycloak | undefined

let $keycloak: KeycloakInstance = undefined

export function getKeycloak(): Keycloak {
return $keycloak
}

export async function getToken(minValidity: number = 10): Promise<string> {
return updateToken(minValidity)
}
Expand All @@ -32,6 +28,7 @@ export async function updateToken(minValidity: number): Promise<string> {

export function createKeycloak(config: KeycloakConfig | string): Keycloak {
$keycloak = new Keycloak(config)
setKeycloak($keycloak)
return $keycloak
}

Expand Down
9 changes: 8 additions & 1 deletion src/state.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { reactive } from 'vue'
import type { KeycloakTokenParsed } from 'keycloak-js'
import { KeycloakInstance } from './keycloak'

export interface KeycloakState {
keycloak: KeycloakInstance
isAuthenticated: boolean
hasFailed: boolean
isPending: boolean
Expand All @@ -14,17 +16,22 @@ export interface KeycloakState {
}

export const state = reactive<KeycloakState>({
keycloak: undefined as KeycloakInstance,
isAuthenticated: false,
hasFailed: false,
isPending: false,
token: '',
decodedToken: {},
decodedToken: {} as KeycloakTokenParsed,
username: '',
userId: '',
roles: [] as string[],
resourceRoles: {},
})

export const setKeycloak = (value: KeycloakInstance): void => {
state.keycloak = value
}

export const setToken = (token: string, tokenParsed: KeycloakTokenParsed): void => {
state.token = token
const content = tokenParsed
Expand Down

0 comments on commit a199c5d

Please sign in to comment.