Skip to content

Commit

Permalink
Added basic login with firebase without forwarding to list after login
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-dabrowski committed Dec 6, 2023
1 parent fece96d commit b684667
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
16 changes: 10 additions & 6 deletions editor_frontend/src/components/TheLogin.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<template>
<div>
<form @submit.prevent="submitForm(username, password)">
<form @submit.prevent="submitForm(email, password)">
<div>
<label for="username">Username:</label>
<input type="text" id="username" v-model="username" required />
<input v-model="email" type="email" placeholder="Email" />
</div>
<div>
<label for="password">Password:</label>
Expand All @@ -15,17 +15,21 @@
</template>

<script lang="ts">
import firebaseApp from "@/initfirestore";
import { getAuth, signInWithEmailAndPassword } from "firebase/auth";
export default {
data() {
return {
username: "",
email: "",
password: "",
};
},
methods: {
submitForm(username: string, password: string) {
// Handle form submission here
console.log(`Username: ${username}, Password: ${password}`);
submitForm(email: string, password: string) {
const auth = getAuth(firebaseApp);
signInWithEmailAndPassword(auth, email, password).catch((error) => {
console.error(error);
});
},
},
};
Expand Down
11 changes: 10 additions & 1 deletion editor_frontend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@ import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import "./styles/main.scss";
import firebaseApp from "@/initfirestore";
import { getAuth } from "firebase/auth";

createApp(App).use(router).mount("#app");
let app = false;
const auth = getAuth(firebaseApp);
auth.onAuthStateChanged((_) => {
if (!app) {
app = true;
createApp(App).use(router).mount("#app");
}
});
23 changes: 22 additions & 1 deletion editor_frontend/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@ import { createRouter, createWebHashHistory } from "vue-router";
import TheList from "@/components/TheList.vue";
import ExhibitEditor from "@/components/ExhibitEditor.vue";
import TheLogin from "@/components/TheLogin.vue";
import firebaseApp from "@/initfirestore";
import { getAuth } from "firebase/auth";

export default createRouter({
const router = createRouter({
history: createWebHashHistory(),
routes: [
{
path: "/",
component: TheList,
meta: {
requiresAuth: true,
},
},
{
path: "/exhibit/:id",
component: ExhibitEditor,
meta: {
requiresAuth: true,
},
},
{
path: "/login",
component: TheLogin,
},
],
});

const auth = getAuth(firebaseApp);
router.beforeEach((to, from, next) => {
const requiresAuth = to.matched.some((record) => record.meta.requiresAuth);
const isAuthenticated = auth.currentUser;
if (requiresAuth && !isAuthenticated) {
next("/login");
} else {
next();
}
});

export default router;

0 comments on commit b684667

Please sign in to comment.