feat(api): refactor API client usage across frontend components
- Replaced direct axios calls with a centralized apiClient in Register, Login, and Activate components for improved maintainability and consistency. - Updated backend base URL logic to support different environments, enhancing flexibility in API interactions. - Added console logging in the authController for better tracking of user registration flow.
This commit is contained in:
@@ -3,9 +3,12 @@ import { register, activateUser, login, logout } from '../services/authService.j
|
||||
const registerUser = async (req, res, next) => {
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
console.log('registerUser', email, password);
|
||||
await register(email, password);
|
||||
console.log('registerUser done');
|
||||
// Aus Sicherheitsgründen KEINE Userdaten (Passwort-Hash, Aktivierungscode, ...) zurückgeben
|
||||
res.status(201).json({ success: true });
|
||||
console.log('registerUser response sent');
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import axios from 'axios';
|
||||
import store from './store';
|
||||
|
||||
export const backendBaseUrl = import.meta.env.VITE_BACKEND || 'http://localhost:3005';
|
||||
export const backendBaseUrl = import.meta.env.VITE_BACKEND
|
||||
|| (import.meta.env.DEV ? 'http://localhost:3005' : window.location.origin);
|
||||
|
||||
const apiClient = axios.create({
|
||||
baseURL: `${backendBaseUrl}/api`,
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import axios from 'axios';
|
||||
import apiClient from '../apiClient.js';
|
||||
|
||||
import InfoDialog from '../components/InfoDialog.vue';
|
||||
import ConfirmDialog from '../components/ConfirmDialog.vue';
|
||||
@@ -63,7 +63,7 @@ export default {
|
||||
async activate() {
|
||||
try {
|
||||
const activationCode = this.$route.params.activationCode;
|
||||
await axios.get(`/api/auth/activate/${activationCode}`);
|
||||
await apiClient.get(`/auth/activate/${activationCode}`);
|
||||
await this.showInfo(this.$t('messages.success'), this.$t('auth.accountActivated'), '', 'success');
|
||||
this.$router.push('/login');
|
||||
} catch (error) {
|
||||
|
||||
@@ -34,8 +34,8 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import axios from 'axios';
|
||||
import { mapActions } from 'vuex';
|
||||
import apiClient from '../apiClient.js';
|
||||
import InfoDialog from '../components/InfoDialog.vue';
|
||||
import ConfirmDialog from '../components/ConfirmDialog.vue';
|
||||
import { buildInfoConfig, buildConfirmConfig, safeErrorMessage } from '../utils/dialogUtils.js';
|
||||
@@ -92,8 +92,8 @@ export default {
|
||||
...mapActions(['login']),
|
||||
async executeLogin() {
|
||||
try {
|
||||
const response = await axios.post(`${import.meta.env.VITE_BACKEND || 'http://localhost:3005'}/api/auth/login`, { email: this.email, password: this.password }, {
|
||||
timeout: 5000,
|
||||
const response = await apiClient.post('/auth/login', { email: this.email, password: this.password }, {
|
||||
timeout: 5000,
|
||||
});
|
||||
await this.login({ token: response.data.token, username: this.email });
|
||||
this.$router.push('/');
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import axios from 'axios';
|
||||
import apiClient from '../apiClient.js';
|
||||
|
||||
import InfoDialog from '../components/InfoDialog.vue';
|
||||
import ConfirmDialog from '../components/ConfirmDialog.vue';
|
||||
@@ -92,7 +92,7 @@ export default {
|
||||
|
||||
async register() {
|
||||
try {
|
||||
await axios.post(`${import.meta.env.VITE_BACKEND || 'http://localhost:3005'}/api/auth/register`, { email: this.email, password: this.password });
|
||||
await apiClient.post('/auth/register', { email: this.email, password: this.password });
|
||||
await this.showInfo(this.$t('messages.success'), this.$t('auth.registerSuccess'), '', 'success');
|
||||
} catch (error) {
|
||||
const message = safeErrorMessage(error, this.$t('auth.registerFailed'));
|
||||
|
||||
Reference in New Issue
Block a user