Skip to content

Commit

Permalink
Changed token to uid and added register path.
Browse files Browse the repository at this point in the history
  • Loading branch information
That-Thing committed Oct 16, 2024
1 parent fc153d1 commit 66c2ad6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/components/navComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import 'vue-toast-notification/dist/theme-sugar.css';
const username = ref('');
const password = ref('');
const token = ref(null); // To track the token
const uid = ref(null); // To track the uid
/**
* Check for token in localStorage when the component is mounted
* Check for uid in localStorage when the component is mounted
*/
onMounted(() => {
token.value = localStorage.getItem('token');
uid.value = localStorage.getItem('uid');
});
/**
Expand All @@ -31,10 +31,10 @@ const signIn = async () => {
username: username.value,
password: password.value
});
if (response.data.token) { // If valid response, store user data and token, then redirect to home
localStorage.setItem('token', response.data.token);
if (response.data.uid) { // If valid response, store user data and uid, then redirect to home
localStorage.setItem('uid', response.data.uid);
localStorage.setItem('user', JSON.stringify(response.data.user));
token.value = response.data.token; // Update token
uid.value = response.data.uid; // Update uid
router.push('/');
}
} catch (error) {
Expand Down Expand Up @@ -73,7 +73,7 @@ const signIn = async () => {
<FontAwesomeIcon :icon="faQuestion" />
</router-link>
</li>
<li v-if="!token" class="nav-item ms-auto d-none d-lg-block me-3 dropdown">
<li v-if="!uid" class="nav-item ms-auto d-none d-lg-block me-3 dropdown">
<router-link to="/" class="btn btn-primary rounded-circle hover-enlarge" id="sign-in-button" data-bs-toggle="dropdown" aria-expanded="false">
<FontAwesomeIcon :icon="faRightToBracket" />
</router-link>
Expand All @@ -91,7 +91,7 @@ const signIn = async () => {
</form>
</ul>
</li>
<li class="nav-item d-none d-lg-block" :class="{ 'ms-auto': token }">
<li class="nav-item d-none d-lg-block" :class="{ 'ms-auto': uid }">
<router-link to="/" class="btn btn-primary rounded-circle hover-enlarge" id="extra-options-button" data-bs-toggle="dropdown" aria-expanded="false">
<FontAwesomeIcon :icon="faBars" />
</router-link>
Expand Down Expand Up @@ -121,7 +121,7 @@ const signIn = async () => {
About
</router-link>
</li>
<li v-if="!token" class="nav-item">
<li v-if="!uid" class="nav-item">
<router-link to="/login" class="nav-link">
<FontAwesomeIcon :icon="faRightToBracket" />
Login
Expand Down
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import HomeView from './views/HomeView.vue'
import AboutView from "./views/AboutView.vue";
import NotFound from "./views/NotFound.vue";
import Login from "./views/Login.vue";
import Register from "./views/Register.vue";
import PrivacyPolicy from "./views/PrivacyPolicy.vue";

// Routes
export const routes = [
{ name: "Home", path: "/", component: HomeView }, // Home page
{ name: "About", path: "/about", component: AboutView }, // About page
{name: "Sign In", path: "/login", component: Login}, // Sign In page
{name: "Register", path: "/register", component: Register}, // Register page
{name: "Privacy Policy", path: "/privacy", component: PrivacyPolicy}, // Privacy Policy page
{ name: "404 - Page not found", path: '/404', component: NotFound }, // 404 page
{ path: '/:catchAll(.*)', redirect: '/404' }, // Catch all redirect to 404
Expand Down
6 changes: 4 additions & 2 deletions src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ export default {
username: this.username,
password: this.password
});
if (response.data.token) { // If valid response, store user data and redirect to home
localStorage.setItem('token', response.data.token);
if (response.data.uid) { // If valid response, store user data and redirect to home
localStorage.setItem('uid', response.data.uid);
localStorage.setItem('user', JSON.stringify(response.data.user));
this.router.push('/'); // Use this.router to navigate
} else {
$toast.error('Login failed');
}
} catch (error) {
console.error('Login failed', error);
Expand Down
84 changes: 84 additions & 0 deletions src/views/Register.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<template>
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-12 col-md-6 col-lg-4">
<div class="card p-4">
<div class="card-body">
<h5 class="card-title">Register</h5>
<form @submit.prevent="register">
<div class="form-group mb-3">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" v-model="username" required>
</div>
<div class="form-group mb-3">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" v-model="password" required>
</div>
<div class="form-group mb-3">
<label for="confirm-password">Confirm Password</label>
<input type="password" class="form-control" id="confirm-password" v-model="confirmPassword" required>
</div>
<button type="submit" class="btn btn-primary w-100 mt-3">Sign In</button>
</form>
</div>
</div>
</div>
</div>
</div>
</template>


<script>
import axios from 'axios';
import { useRouter } from 'vue-router'; // Import the router
import {useToast} from 'vue-toast-notification';
import 'vue-toast-notification/dist/theme-sugar.css';
const API_URL = import.meta.env.VITE_APP_API_URL;
const $toast = useToast();
export default {
setup() {
const router = useRouter(); // Initialize the router
return {
router
};
},
data() {
return {
username: '',
password: ''
};
},
methods: {
/**
* Sign in the user using the provided username and password
*/
async register() {
try {
if(this.password !== this.confirmPassword){
$toast.error('Passwords do not match');
return;
}
const response = await axios.post(`${API_URL}/register`, {
username: this.username,
password: this.password
});
if (response.data.uid) { // If valid response, store user data and redirect to home
localStorage.setItem('uid', response.data.uid);
localStorage.setItem('user', JSON.stringify(response.data.user));
this.router.push('/'); // Use this.router to navigate
} else {
$toast.error('Register failed');
}
} catch (error) {
console.error('Register failed', error);
$toast.error('Register failed');
}
}
}
};
</script>

0 comments on commit 66c2ad6

Please sign in to comment.