Skip to content

Commit

Permalink
Merge pull request #47 from heikkkk/fix/sucessfull-login-after-logout
Browse files Browse the repository at this point in the history
Fixed bug not letting users log in after logging out
  • Loading branch information
heikkkk authored Apr 6, 2024
2 parents 8e3afe5 + a13a9a2 commit 8a813b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
13 changes: 6 additions & 7 deletions src/components/Login/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
import '../../assets/css/Login/login.css'
import LogoTitle from '@/components/LogoTitle.vue'
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/UserStore.js'
import { login, username, password } from '../../services/LoginService.js'
import axios from 'axios'
import { ref, computed } from 'vue'
import { ref } from 'vue'
const authenticationError = ref(false)
Expand All @@ -15,7 +13,8 @@ const handleSubmit = async () => {
try {
const response = await login()
if (response && response.status === 200) {
router.push('/discover')
await router.push('/discover')
} else {
authenticationError.value = true
}
Expand All @@ -30,16 +29,16 @@ const handleSubmit = async () => {
<div class="login-wrapper">
<div class="login-container">
<form class="login-form" @submit.prevent="handleSubmit">
<LogoTitle color="#ffffff"></LogoTitle>
<LogoTitle data-cy="logo-title-login" color="#ffffff"></LogoTitle>
<p v-if="authenticationError" class="error_msg">Incorrect username or password</p>
<input v-model="username" :class="authenticationError === false ? 'username-input' : 'error'" type="text" placeholder="Username">
<input v-model="password" :class="authenticationError === false ? 'password-input' : 'error'" type="password" placeholder="Password">
<div class="login-button-container">

<button class="login-button" type="submit">Login</button>
<button data-cy="login-button" class="login-button" type="submit">Login</button>
<div class="signup-paragraph-container">
<p >Don't have a user?
<RouterLink to="/sign-up" style="color: white">Sign up</RouterLink>
<RouterLink data-cy="sign-up-button" to="/sign-up" style="color: white">Sign up</RouterLink>
</p>
</div>
</div>
Expand Down
35 changes: 20 additions & 15 deletions src/services/LoginService.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import axios from 'axios'
import { ref, computed } from 'vue'
import { useUserStore } from '@/stores/UserStore.js'
import { getIdByUsername } from '@/services/QuizInfoService.js'
import piniaPluginPersistedState from "pinia-plugin-persistedstate"
import { createPinia } from 'pinia'
import { createApp } from 'vue'
import App from '@/App.vue'

const pinia = createPinia()
pinia.use(piniaPluginPersistedState);
const app = createApp(App)
app.use(pinia)

const username = ref('')
const password = ref('')
const authenticationError = ref(false)
const userStore = useUserStore()

const authConfig = computed(() => ({
auth: {
Expand All @@ -16,24 +22,23 @@ const authConfig = computed(() => ({
}))

function parseResponse(response) {
const userStore = useUserStore()
userStore.setUsername(response.data.user_name)
userStore.setToken(response.data.access_token)
return response; // Return the response
}

export function postLoginCredentials() {
return axios.post('http://localhost:8080/sign-in', {}, authConfig.value)
.then(response => {
return parseResponse(response, userStore)
})
.catch(error => {
authenticationError.value = true
throw error; // Throw error to propagate it to the caller
})
export async function postLoginCredentials() {
try {
const response = await axios.post('http://localhost:8080/sign-in', {}, authConfig.value);
return parseResponse(response);
} catch (error) {
throw error; // Throw error to propagate it to the caller
}
}

export function login() {
return postLoginCredentials()
export async function login() {
return await postLoginCredentials();
}

export { username, password, authenticationError }
export { username, password }

0 comments on commit 8a813b3

Please sign in to comment.