first commit
This commit is contained in:
		
							
								
								
									
										38
									
								
								src/stores/auth.store.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/stores/auth.store.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | ||||
| import { defineStore } from 'pinia'; | ||||
|  | ||||
| import { fetchWrapper } from '@/helpers'; | ||||
| import { router } from '@/router'; | ||||
|  | ||||
|  | ||||
| const baseUrl = `${import.meta.env.VITE_BASE_URL}/users`; | ||||
|  | ||||
| export const useAuthStore = defineStore({ | ||||
|     id: 'auth', | ||||
|     state: () => ({ | ||||
|         // initialize state from local storage to enable user to stay logged in | ||||
|         user: JSON.parse(localStorage.getItem('user')), | ||||
|         returnUrl: null | ||||
|     }), | ||||
|     actions: { | ||||
|         async login(username, password) { | ||||
|  | ||||
|             const user = await fetchWrapper.post(`${baseUrl}/authenticate`, | ||||
|                 { username, password }); | ||||
|  | ||||
|             // update pinia state | ||||
|             this.user = user; | ||||
|  | ||||
|             // store user details and jwt in local storage to keep user logged in between page refreshes | ||||
|             localStorage.setItem('user', JSON.stringify(user)); | ||||
|  | ||||
|             // redirect to previous url or default to home page | ||||
|             router.push(this.returnUrl || '/'); | ||||
|  | ||||
|         }, | ||||
|         logout() { | ||||
|             this.user = null; | ||||
|             localStorage.removeItem('user'); | ||||
|             router.push('/account/login'); | ||||
|         } | ||||
|     } | ||||
| }); | ||||
							
								
								
									
										64
									
								
								src/stores/users.store.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/stores/users.store.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| import { defineStore } from 'pinia'; | ||||
|  | ||||
| import { fetchWrapper } from '@/helpers'; | ||||
| import { useAuthStore } from '@/stores'; | ||||
|  | ||||
| const baseUrl = `${import.meta.env.VITE_BASE_URL}/auth`; | ||||
|  | ||||
| export const useUsersStore = defineStore({ | ||||
|     id: 'users', | ||||
|     state: () => ({ | ||||
|         users: {}, | ||||
|         user: {} | ||||
|     }), | ||||
|     actions: { | ||||
|         async register(user) { | ||||
|             await fetchWrapper.post(`${baseUrl}/register`, user); | ||||
|         }, | ||||
|         async getAll() { | ||||
|             this.users = { loading: true }; | ||||
|             try { | ||||
|                 this.users = await fetchWrapper.get(baseUrl); | ||||
|             } catch (error) { | ||||
|                 this.users = { error }; | ||||
|             } | ||||
|         }, | ||||
|         async getById(id) { | ||||
|             this.user = { loading: true }; | ||||
|             try { | ||||
|                 this.user = await fetchWrapper.get(`${baseUrl}/${id}`); | ||||
|             } catch (error) { | ||||
|                 this.user = { error }; | ||||
|             } | ||||
|         }, | ||||
|         async update(id, params) { | ||||
|             await fetchWrapper.put(`${baseUrl}/${id}`, params); | ||||
|  | ||||
|             // update stored user if the logged-in user updated their own record | ||||
|             const authStore = useAuthStore(); | ||||
|             if (id === authStore.user.id) { | ||||
|                 // update local storage | ||||
|                 const user = { ...authStore.user, ...params }; | ||||
|                 localStorage.setItem('user', JSON.stringify(user)); | ||||
|  | ||||
|                 // update auth user in pinia state | ||||
|                 authStore.user = user; | ||||
|             } | ||||
|         }, | ||||
|         async delete(id) { | ||||
|             // add isDeleting prop to user being deleted | ||||
|             this.users.find(x => x.id === id).isDeleting = true; | ||||
|  | ||||
|             await fetchWrapper.delete(`${baseUrl}/${id}`); | ||||
|  | ||||
|             // remove user from list after deleted | ||||
|             this.users = this.users.filter(x => x.id !== id); | ||||
|  | ||||
|             // auto logout if the logged in user deleted their own record | ||||
|             const authStore = useAuthStore(); | ||||
|             if (id === authStore.user.id) { | ||||
|                 authStore.logout(); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user