Skip to content

Commit

Permalink
add auth guard, hide register button after login
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbednarz committed May 14, 2022
1 parent b74d8f5 commit a067f82
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public FileRepository(MainDatabaseContext context) : base(context)

public async Task<List<FileDataDTO>> GetUserFiles(int userId)
{
return await _context.Files.Where(x => x.UserId == userId).Select(x => new FileDataDTO
return await _context.Files.Where(x => x.UserId == userId && x.IsDeleted == false).Select(x => new FileDataDTO
{
Id = x.Id,
Name = x.Name,
Expand Down
24 changes: 23 additions & 1 deletion CloudDrive.Vue/src/components/FilesGrid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
<template v-slot:item="props">
<div class="q-pa-xs col-2">
<q-card class="file-card bg-secondary text-black">
<q-card-actions class="delete-button" text-align="right">
<q-btn
flat
round
icon="fa-solid fa-trash"
@click="tryDelete(props.row.relativePath)"
/>
</q-card-actions>
<q-card-section class="text-center">
<i class="fa-solid fa-file text-h5"></i>
<div class="text-body2">{{ props.row.name }}</div>
Expand Down Expand Up @@ -76,7 +84,17 @@ export default {
computed: {},
methods: {
...mapActions(useDirectoryStore, ["getDirectoriesToSelectList"]),
...mapActions(useFileStore, ["getUserFiles"]),
...mapActions(useFileStore, ["getUserFiles", "deleteFile"]),
async tryDelete(relativePath) {
await this.deleteFile(relativePath);
this.getUserFiles().then((response) => {
this.files = response;
this.$q.notify({
type: "positive",
message: `Plik usunięty pomyślnie!`,
});
});
},
},
mounted() {
this.getUserFiles().then((response) => {
Expand All @@ -97,4 +115,8 @@ export default {
opacity: 1;
cursor: pointer;
}
.delete-button {
padding: 0;
margin-left: auto;
}
</style>
4 changes: 4 additions & 0 deletions CloudDrive.Vue/src/components/LoginForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export default {
connectToHub(this.currentUser.username, this.$q);
this.closePopover();
this.$q.notify({
type: "info",
message: `Zalogowano pomyślnie`,
});
},
},
mounted() {
Expand Down
5 changes: 3 additions & 2 deletions CloudDrive.Vue/src/layouts/MainLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ export default {
tryLogout() {
this.logout();
this.$q.notify({
type: "positive",
message: `Wylogowano pomyślnie!`,
type: "info",
message: `Wylogowano pomyślnie`,
});
this.$router.push({ name: "home" });
},
},
mounted() {
Expand Down
11 changes: 7 additions & 4 deletions CloudDrive.Vue/src/pages/IndexPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
class="q-mr-md"
/>
</div> -->
<div>
<div v-if="loggedInUser.token"></div>
<div v-else>
<q-btn
color="secondary"
text-color="black"
Expand All @@ -45,7 +46,7 @@
<script>
import MainContainer from "../components/MainContainer";
import RegisterForm from "../components/RegisterForm";
import { mapState } from "pinia";
import { mapState, mapWritableState } from "pinia";
import { useAuthenticationStore } from "../stores/authentication.js";
export default {
Expand All @@ -55,12 +56,14 @@ export default {
RegisterForm,
},
computed: {
...mapState(useAuthenticationStore, ["user"]),
...mapState(useAuthenticationStore, ["currentUser"]),
loggedInUser() {
return this.currentUser;
},
},
data() {
return {
isRegisterPopoverVisible: false,
currentUser: JSON.parse(localStorage.getItem("user")),
};
},
methods: {
Expand Down
5 changes: 0 additions & 5 deletions CloudDrive.Vue/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,5 @@ export default route(function (/* { store, ssrContext } */) {
),
});

// Router.beforeEach((to, from, next) => {
// if (to.name == "upload") next();
// else next({ name: "upload" });
// });

return Router;
});
22 changes: 22 additions & 0 deletions CloudDrive.Vue/src/router/routes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Notify } from "quasar";

const routes = [
{
path: "/",
Expand All @@ -11,11 +13,13 @@ const routes = [
{
path: "drive",
name: "drive",
beforeEnter: (to, from, next) => authGuard(to, from, next),
component: () => import("pages/DrivePage.vue"),
},
{
path: "upload",
name: "upload",
beforeEnter: (to, from, next) => authGuard(to, from, next),
component: () => import("pages/UploadPage.vue"),
},
],
Expand All @@ -27,4 +31,22 @@ const routes = [
},
];

function authGuard(to, from, next) {
let isAuthenticated = false;
if (localStorage.getItem("user")) {
isAuthenticated = true;
} else {
isAuthenticated = false;
}
if (isAuthenticated) {
next();
} else {
Notify.create({
message: "Dostęp wzbroniony dla niezalogowanego użytkownika!",
color: "negative",
});
next({ name: "home" });
}
}

export default routes;
16 changes: 16 additions & 0 deletions CloudDrive.Vue/src/stores/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,21 @@ export const useFileStore = defineStore({
});
});
},

async deleteFile(relativePath) {
if (
authenticationStore.currentUser &&
authenticationStore.currentUser.token
) {
await api.delete("/File/deleteFile", {
headers: {
Authorization: `Bearer ${authenticationStore.currentUser.token}`,
},
params: {
relativePath: relativePath,
},
});
}
},
},
});

0 comments on commit a067f82

Please sign in to comment.