Skip to content

Commit

Permalink
fix: authorization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mutoe committed Sep 12, 2023
1 parent ab11d70 commit 5ce52d1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
20 changes: 19 additions & 1 deletion cypress/e2e/auth.cy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { ROUTES } from './constant'

describe('Auth', () => {
beforeEach(() => {
cy.intercept('GET', /users/, { fixture: 'user.json' }).as('getUser')
cy.intercept('GET', /tags/, { fixture: 'tags.json' }).as('getTags')
cy.intercept('GET', /articles/, { fixture: 'articles.json' }).as('getArticles')
})

describe('Login and logout', () => {
it('should login success when submit a valid login form', () => {
cy.login()
Expand Down Expand Up @@ -51,10 +57,22 @@ describe('Auth', () => {
it('should not allow visiting login page when the user is logged in', () => {
cy.login()

cy.visit('/#/login')
cy.visit(ROUTES.LOGIN)

cy.url().should('match', /\/#\/$/)
})

it('should has credential header after login success', () => {
cy.login()

cy.visit(ROUTES.SETTINGS)
cy.intercept('PUT', /user/).as('updateSettingsRequest')

cy.findByRole('textbox', { name: 'Username' }).type('foo')
cy.findByRole('button', { name: 'Update Settings' }).click()

cy.wait('@updateSettingsRequest').its('request.headers').should('have.property', 'authorization')
})
})

describe('Register', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<fieldset class="form-group">
<input
v-model="form.password"
aria-label="Password"
aria-label="New password"
type="password"
class="form-control form-control-lg"
placeholder="New password"
Expand Down
2 changes: 1 addition & 1 deletion src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const limit = 10

export const api = new Api({
baseUrl: `${CONFIG.API_HOST}/api`,
securityWorker: token => token ? { headers: { authorization: `Bearer ${token}` } } : {},
securityWorker: token => token ? { headers: { Authorization: `Bearer ${token}` } } : {},
baseApiParams: {
headers: {
'content-type': ContentType.Json,
Expand Down
10 changes: 5 additions & 5 deletions src/store/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export const useUserStore = defineStore('user', () => {
const isAuthorized = computed(() => !!user.value)

function updateUser (userData?: User | null) {
if (userData === undefined || userData === null) {
userStorage.remove()
api.setSecurityData(null)
user.value = null
} else {
if (userData) {
userStorage.set(userData)
api.setSecurityData(userData.token)
user.value = userData
} else {
userStorage.remove()
api.setSecurityData(null)
user.value = null
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/utils/use-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Ref } from 'vue'
import { ref } from 'vue'
import { routerPush } from 'src/router'
import { isFetchError } from 'src/services'
import { userStorage } from 'src/store/user.ts'

interface UseAsync<T extends (...args: unknown[]) => unknown> {
active: Ref<boolean>
Expand All @@ -18,8 +19,9 @@ export default function useAsync<T extends (...args: unknown[]) => unknown> (fn:
return result as ReturnType<T>
} catch (error) {
if (isFetchError(error) && error.status === 401) {
userStorage.remove()
await routerPush('login')
throw new Error('Need to login first')
throw new Error('Unauthorized or token expired')
}
throw error
} finally {
Expand Down

0 comments on commit 5ce52d1

Please sign in to comment.