Skip to content

Commit c2f1cc3

Browse files
Merge pull request #21 from HE-Arc/6-login-registration-logic-users-rights
6 login registration logic users rights
2 parents f23cc42 + 04ffb11 commit c2f1cc3

File tree

8 files changed

+74
-20
lines changed

8 files changed

+74
-20
lines changed

api/Pipfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ djangorestframework = "*"
99
django-cors-headers = "*"
1010
psycopg2-binary = "*"
1111
python-dotenv = "*"
12-
rest-framework-simplejwt = "*"
12+
djangorestframework-simplejwt = "*"
1313

1414
[dev-packages]

api/Pipfile.lock

+18-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/src/api.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios';
2+
axios.defaults.baseURL = import.meta.env.VITE_API_URL
23

34
// Create API instance
45
const api = axios.create({

frontend/src/auth.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ export async function login(username, password) {
4040
}
4141

4242
// Logout function
43-
export function logout() {
43+
export async function logout() {
44+
try {
45+
const response = await api.post("/auth/logout/", {});
46+
4447
localStorage.removeItem("access");
4548
localStorage.removeItem("refresh");
46-
setAuthToken(null); // Remove token from API
49+
setAuthToken(null);
50+
return response.data;
51+
} catch (error) {
52+
console.error("Logout failed:", error.response.data);
53+
throw error.response.data;
54+
}
4755
}

frontend/src/components/NavBar.vue

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script setup>
2+
import LoginLogout from '@/components/functional/LoginLogout.vue'
23
import { ref } from 'vue'
34
</script>
45

@@ -23,10 +24,7 @@ import { ref } from 'vue'
2324
</router-link>
2425
</li>
2526
<li>
26-
<!-- TODO if logged in - log out -->
27-
<router-link :to="{ name: 'login' }" class="text-gray-400 dark:text-white-400 inline-block py-2 px-4 text-sm font-medium">
28-
Login
29-
</router-link>
27+
<LoginLogout></LoginLogout>
3028
</li>
3129
</ul>
3230
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<script setup>
2+
import { ref, watchEffect } from "vue";
3+
import { useRouter } from "vue-router";
4+
import { logout } from "../../auth.js";
5+
6+
const router = useRouter();
7+
const errorMessage = ref("");
8+
const isAuthenticated = ref(!!localStorage.getItem("access"));
9+
10+
// Watch for changes to localStorage and update isAuthenticated
11+
watchEffect(() => {
12+
isAuthenticated.value = !!localStorage.getItem("access");
13+
});
14+
15+
async function handleLogout() {
16+
try {
17+
await logout();
18+
localStorage.removeItem("access");
19+
isAuthenticated.value = false;
20+
router.push("/login");
21+
} catch (error) {
22+
errorMessage.value = "Error logging out.";
23+
}
24+
}
25+
</script>
26+
27+
<template>
28+
<div v-if="isAuthenticated">
29+
<router-link to="#" @click.prevent="handleLogout" class="text-gray-400 dark:text-white-400 inline-block py-2 px-4 text-sm font-medium">
30+
Logout
31+
</router-link>
32+
</div>
33+
<div v-else>
34+
<router-link :to="{ name: 'login' }" class="text-gray-400 dark:text-white-400 inline-block py-2 px-4 text-sm font-medium">
35+
Login
36+
</router-link>
37+
</div>
38+
</template>

frontend/src/router/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const router = createRouter({
3939
{ path: "/login",
4040
name: 'login',
4141
component: () => import('../views/LoginView.vue' ) },
42+
4243
{ path: "/register",
4344
name: 'register',
4445
component: () => import('../views/RegisterView.vue' ) },
@@ -49,7 +50,7 @@ const router = createRouter({
4950

5051
router.beforeEach((to, from, next) => {
5152
const isAuthenticated = !!localStorage.getItem("access");
52-
if ( ( to.path != "/register" && to.path != "/login" ) && !isAuthenticated) {
53+
if ( ( to.path != "/register" && to.path != "/login" && to.path != "/about" ) && !isAuthenticated) {
5354
next("/login");
5455
} else {
5556
next();

frontend/src/views/LoginView.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const errorMessage = ref("");
1212
async function handleLogin() {
1313
try {
1414
await login(username.value, password.value);
15-
router.push("/characters"); // Redirect after login
15+
router.push("/characters").then(() => { window.location.reload(); });
1616
} catch (error) {
1717
errorMessage.value = "Invalid username or password.";
1818
}
@@ -35,7 +35,7 @@ async function handleLogin() {
3535
<input class="ff-input" v-model="password" type="password" placeholder="Password" required />
3636
</div>
3737
<button type="submit" class="ff-button">Login</button>
38-
<a class="text-red px-4 py-2 rounded" href="register">No account? Register</a>
38+
<a class="px-4 py-2.5 rounded dark:text-gray-900" href="register">No account? Register</a>
3939
</form>
4040

4141
</div>

0 commit comments

Comments
 (0)