Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Jun 17, 2024
1 parent 19fc865 commit 840d413
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/Frontend/src/components/AuthenticatedAccount.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { tokenHelper } from '../helpers/tokenHelper'
import { AuthenticationHelper } from '../helpers/authenticationHelper'
const authenticationHelper = new AuthenticationHelper()
const Router = useRouter()
const tokenInfo = computed(() => {
Expand All @@ -17,7 +19,7 @@ const tokenInfo = computed(() => {
})
async function logout () {
tokenHelper.removeToken()
authenticationHelper.logout()
await Router.push('/login')
}
Expand Down
18 changes: 10 additions & 8 deletions src/Frontend/src/components/MfaConfigurationBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@
import { ref, onMounted } from 'vue'
import { useQuasar, QStepper } from 'quasar'
import { apiHelper } from '../helpers/apiHelper'
import { MfaHelper } from '../helpers/mfaHelper'
import { instanceOfMfaError } from 'src/models/MfaResponse'
import { MfaError } from 'src/models/MfaError'
import { MfaInformation } from 'src/models/MfaInformation'
const $q = useQuasar()
const mfaHelper = new MfaHelper()
const step = ref(1)
const stepper = ref<QStepper>()
const mfa = ref<MfaInformation>()
const token = ref('')
const processing = ref(false)
onMounted(async () => {
await getMfaStatus()
await getStatus()
})
async function getMfaStatus () {
mfa.value = await apiHelper.mfaInfo()
async function getStatus () {
mfa.value = await mfaHelper.getStatus()
}
async function activate () {
try {
processing.value = true
const mfaResponse = await apiHelper.mfaActivate(token.value)
const mfaResponse = await mfaHelper.activate(token.value)
if (instanceOfMfaError(mfaResponse)) {
$q.notify({
Expand All @@ -40,7 +42,7 @@ async function activate () {
} finally {
processing.value = false
await getMfaStatus()
await getStatus()
token.value = ''
step.value = 1
Expand All @@ -50,7 +52,7 @@ async function activate () {
async function deactivate () {
try {
processing.value = true
const mfaResponse = await apiHelper.mfaDeactivate(token.value)
const mfaResponse = await mfaHelper.deactivate(token.value)
if (instanceOfMfaError(mfaResponse)) {
$q.notify({
Expand All @@ -65,7 +67,7 @@ async function deactivate () {
} finally {
processing.value = false
}
await getMfaStatus()
await getStatus()
}
async function stepperNext () {
Expand Down
8 changes: 6 additions & 2 deletions src/Frontend/src/helpers/authenticationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class AuthenticationHelper {

if (response.status === 200) {
const authenticationResponse = await response.json() as AuthenticationResponse
tokenHelper.setToken(authenticationResponse)
tokenHelper.setToken(authenticationResponse.token)

this.mfaIdentifier = undefined

Expand All @@ -47,7 +47,7 @@ export class AuthenticationHelper {

if (response.status === 200) {
const authenticationResponse = await response.json() as AuthenticationResponse
tokenHelper.setToken(authenticationResponse)
tokenHelper.setToken(authenticationResponse.token)

return LoginAction.Forward
}
Expand All @@ -69,4 +69,8 @@ export class AuthenticationHelper {

return LoginAction.ClearPassword
}

logout () {
tokenHelper.removeToken()
}
}
65 changes: 65 additions & 0 deletions src/Frontend/src/helpers/mfaHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const apiBaseUrl = process.env.NODE_ENV === 'development' ? '/api/v1/' : '/auth/api/v1/'

import { MfaError } from 'src/models/MfaError'
import { MfaSuccess } from 'src/models/MfaSuccess'
import { MfaResponse } from 'src/models/MfaResponse'
import { MfaInformation } from 'src/models/MfaInformation'

import { tokenHelper } from './tokenHelper'

export class MfaHelper {
async getStatus () : Promise<MfaInformation> {
const token = tokenHelper.getToken()

const response = await fetch(`${apiBaseUrl}UserAccount/Mfa`, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
})

return await response.json() as MfaInformation
}

async activate (mfaToken: string) : Promise<MfaResponse> {
const token = tokenHelper.getToken()

const response = await fetch(`${apiBaseUrl}UserAccount/Mfa/Activate`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: mfaToken
})
})

if (response.status === 204) {
return { success: true } as MfaSuccess
}

return await response.json() as MfaError
}

async deactivate (mfaToken: string) : Promise<MfaResponse> {
const token = tokenHelper.getToken()

const response = await fetch(`${apiBaseUrl}UserAccount/Mfa/Deactivate`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: mfaToken
})
})

if (response.status === 204) {
return { success: true } as MfaSuccess
}

return await response.json() as MfaError
}
}
5 changes: 2 additions & 3 deletions src/Frontend/src/helpers/tokenHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { date, LocalStorage } from 'quasar'

import { AuthenticationResponse } from 'src/models/AuthenticationResponse'
import { TokenInfo } from 'src/models/TokenInfo'

const authenticationTokenKey = 'authenticationToken'
Expand All @@ -9,8 +8,8 @@ function getToken () : string | null {
return LocalStorage.getItem<string>(authenticationTokenKey)
}

function setToken (authenticationResponse : AuthenticationResponse) {
LocalStorage.set(authenticationTokenKey, authenticationResponse.token)
function setToken (token : string) {
LocalStorage.set(authenticationTokenKey, token)
}

function removeToken () {
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/src/pages/AccountPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MfaConfigurationBox from '../components/MfaConfigurationBox.vue'

<template>
<q-page padding>
<h1>Account</h1>
<h1>My Account</h1>

<div class="row q-col-gutter-xl">
<div class="col-12 col-md-5 col-lg-4">
Expand Down
8 changes: 4 additions & 4 deletions src/Frontend/src/pages/LoginPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const $q = useQuasar()
const Router = useRouter()
const loading = ref(false)
const oneTimePasswordRequired = ref(false)
const totpToken = ref<undefined | string>(undefined)
const emailAddress = ref('')
const password = ref('')
const oneTimePasswordRequired = ref(false)
const totpToken = ref<undefined | string>(undefined)
async function login () {
loading.value = true
Expand Down Expand Up @@ -96,7 +96,7 @@ async function tokenLogin () {
class="q-pa-lg shadow-1"
>
<template v-if="oneTimePasswordRequired">
<q-form @submit.prevent="tokenLogin">
<q-form @submit.prevent="tokenLogin()">
<q-card-section class="q-gutter-md">
<q-input
v-model="totpToken"
Expand All @@ -120,7 +120,7 @@ async function tokenLogin () {
</q-form>
</template>
<template v-else>
<q-form @submit.prevent="login">
<q-form @submit.prevent="login()">
<q-card-section class="q-gutter-md">
<q-input
v-model="emailAddress"
Expand Down

0 comments on commit 840d413

Please sign in to comment.