• Patrick Ade's avatar
    Implement authentication store using Pinia; create auth store with login,... · e6e9197f
    Patrick Ade authored
    Implement authentication store using Pinia; create auth store with login, logout, and user fetching functionalities.
    
    Update main entry point to reflect new store structure; change import path for auth store.
    
    Add TypeScript support for Vue components; create shims-vue.d.ts for .vue file declarations.
    
    Create Login and Register views with authentication logic; implement user login and registration forms with error handling.
    
    Refactor HomeView to utilize authentication store; display user information and logout functionality based on authentication state.
    e6e9197f
authold.js 3.54 KiB
import { defineStore } from 'pinia'
export const useAuthStore = defineStore('auth', {
  state: () => {
    const storedState = localStorage.getItem('authState')
    return storedState
      ? JSON.parse(storedState)
      : {
          user: null,
          isAuthenticated: false,
  actions: {
    async setCsrfToken() {
      await fetch('http://localhost:8000/api/set-csrf-token', {
        method: 'GET',
        credentials: 'include',
    async login(email, password, router = null) {
      const response = await fetch('http://localhost:8000/api/login', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'X-CSRFToken': getCSRFToken(),
        body: JSON.stringify({
          email,
          password,
        }),
        credentials: 'include',
      const data = await response.json()
      if (data.success) {
        this.isAuthenticated = true
        this.saveState()
        if (router) {
          await router.push({
            name: 'home',
      } else {
        this.user = null
        this.isAuthenticated = false
        this.saveState()
    async logout(router = null) {
      try {
        const response = await fetch('http://localhost:8000/api/logout', {
          method: 'POST',
          headers: {
            'X-CSRFToken': getCSRFToken(),
          credentials: 'include',
        if (response.ok) {
          this.user = null
          this.isAuthenticated = false
          this.saveState()
          if (router) {
            await router.push({
              name: 'login',
      } catch (error) {
        console.error('Logout failed', error)
throw error } }, async fetchUser() { try { const response = await fetch('http://localhost:8000/api/user', { credentials: 'include', headers: { 'Content-Type': 'application/json', 'X-CSRFToken': getCSRFToken(), }, }) if (response.ok) { const data = await response.json() this.user = data this.isAuthenticated = true } else { this.user = null this.isAuthenticated = false } } catch (error) { console.error('Failed to fetch user', error) this.user = null this.isAuthenticated = false } this.saveState() }, saveState() { /* We save state to local storage to keep the state when the user reloads the page. This is a simple way to persist state. For a more robust solution, use pinia-persistent-state. */ localStorage.setItem( 'authState', JSON.stringify({ user: this.user, isAuthenticated: this.isAuthenticated, }), ) }, }, }) export function getCSRFToken() { /* We get the CSRF token from the cookie to include in our requests. This is necessary for CSRF protection in Django. */ const name = 'csrftoken' let cookieValue = null if (document.cookie && document.cookie !== '') { const cookies = document.cookie.split(';') for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i].trim() if (cookie.substring(0, name.length + 1) === name + '=') { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)) break } } } if (cookieValue === null) { throw 'Missing CSRF cookie.' } return cookieValue }