Skip to content

Commit

Permalink
fix!: hasRole returns ComputedRef<boolean> instead of boolean (#80
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nogic1008 authored Mar 7, 2024
1 parent d3e43ab commit 83a4dd3
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 15 deletions.
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Changelog

## v0.5.0

[compare changes](https://github.com/ddradar/nuxt-swa/compare/v0.4.0...v0.5.0)

### ⚠️ Breaking Changes

- `hasRole` returns `ComputedRef<boolean>` instead of `boolean` ([#80](https://github.com/ddradar/nuxt-swa/pull/80))

### 🩹 Fixes

- ⚠️ `hasRole` returns `ComputedRef<boolean>` instead of `boolean` ([#80](https://github.com/ddradar/nuxt-swa/pull/80))
- Read cookie if auth header is undefined ([#79](https://github.com/ddradar/nuxt-swa/pull/79))

### 🤖 CI

#### Dependencies Update

|kind|package|old|new|PRs|
|----|-------|--:|--:|---|
|NPM(dev)|@types/node|18.19.18|18.19.22|[#81](https://github.com/ddradar/nuxt-swa/pull/81)|
|GitHub Actions|actions/download-artifact|4.1.2|4.1.3|[#77](https://github.com/ddradar/nuxt-swa/pull/77)|
|GitHub Actions|codecov/codecov-action|4.0.2|4.1.0|[#78](https://github.com/ddradar/nuxt-swa/pull/78)|

### ❤️ Contributors

- Nogic ([@nogic1008](http://github.com/nogic1008))

## v0.4.0

Highlights:
Expand Down
4 changes: 2 additions & 2 deletions docs/content/2.api-docs/1.composables/1.use-easy-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ Refresh `clientPrincipal` data.

Returns user has specified role.

- Type: `(role: string) => boolean`
- Type: `(role: string) => ComputedRef<boolean>`
- Params:
- `role`: `string` Role name
- Return: `boolean`
- Return: `ComputedRef<boolean>`

::callout{icon="i-heroicons-light-bulb"}
If "anonymous" is passed as an argument, this method **always** returns `true` whether user is logged in or not.
Expand Down
4 changes: 2 additions & 2 deletions docs/content/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ hero:
description: Provides Azure Static Web Apps features to your Nuxt apps.
orientation: horizontal
headline:
label: Released v0.4.0
to: https://github.com/ddradar/nuxt-swa/releases/tag/v0.4.0
label: Released v0.5.0
to: https://github.com/ddradar/nuxt-swa/releases/tag/v0.5.0
icon: i-heroicons-rocket-launch-20-solid
links:
- label: Get started
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nuxt-swa-docs",
"private": true,
"version": "0.4.0",
"version": "0.5.0",
"description": "Nuxt SWA module documentation and playground",
"type": "module",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nuxt-swa-monorepo",
"private": true,
"version": "0.4.0",
"version": "0.5.0",
"description": "Nuxt 3 module for Azure Static Web Apps",
"repository": "ddradar/nuxt-swa",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt-swa/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuxt-swa",
"version": "0.4.0",
"version": "0.5.0",
"description": "Nuxt module for Azure Static Web Apps",
"repository": {
"type": "git",
Expand Down
8 changes: 5 additions & 3 deletions packages/nuxt-swa/src/runtime/composables/useEasyAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useEasyAuth = async (): Promise<{
* @param role Role name
* @returns true when `role` is "anonymous" or {@link ClientPrincipal.userRoles clientPrincipal.userRoles} includes `role`.
*/
hasRole: (role: UserRole) => boolean
hasRole: (role: UserRole) => ComputedRef<boolean>
/**
* Sign in to your app.
* @param provider Authorization provider used for login
Expand Down Expand Up @@ -72,8 +72,10 @@ export const useEasyAuth = async (): Promise<{
const isLoggedIn = computed(() => !!_auth.value)

// Method
function hasRole(role: UserRole): boolean {
return role === 'anonymous' || !!_auth.value?.userRoles.includes(role)
function hasRole(role: UserRole) {
return computed(
() => role === 'anonymous' || !!_auth.value?.userRoles.includes(role)
)
}
async function login(provider: IdentityProvider): Promise<void> {
await navigateTo(`${_authBasePath}/login/${provider}`, {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt-swa/src/runtime/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default defineNuxtRouteMiddleware(async to => {

const roles = [to.meta.allowedRoles].flat()
const { isLoggedIn, hasRole } = await useEasyAuth()
if (roles.some(hasRole)) return
if (roles.some(r => hasRole(r).value)) return

return abortNavigation({ statusCode: isLoggedIn.value ? 403 : 401 })
})
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('runtime/composables/useEasyAuth', () => {

// Act - Assert
const { hasRole } = await useEasyAuth()
expect(hasRole(role)).toBe(expected)
expect(hasRole(role).value).toBe(expected)
}
)
})
Expand Down
8 changes: 5 additions & 3 deletions packages/nuxt-swa/test/unit/runtime/middleware/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ describe('runtime/middleware/auth', () => {
'{ allowedRoles: %o } returns 200',
async allowedRoles => {
// Arrange
useEasyAuthMock.mockResolvedValue({ hasRole: (_role: string) => true })
useEasyAuthMock.mockResolvedValue({
hasRole: (_role: string) => ref(true),
})

// Act
await authRouteMiddleware({ meta: { allowedRoles } } as any, {} as any)
Expand All @@ -46,7 +48,7 @@ describe('runtime/middleware/auth', () => {
async allowedRoles => {
// Arrange
useEasyAuthMock.mockResolvedValue({
hasRole: (_role: string) => false,
hasRole: (_role: string) => ref(false),
isLoggedIn: ref(false),
})

Expand All @@ -62,7 +64,7 @@ describe('runtime/middleware/auth', () => {
async allowedRoles => {
// Arrange
useEasyAuthMock.mockResolvedValue({
hasRole: (_role: string) => false,
hasRole: (_role: string) => ref(false),
isLoggedIn: ref(true),
})

Expand Down

0 comments on commit 83a4dd3

Please sign in to comment.